Ways on inserting data using stored procedure when TableName is dynamic

Kalpana 286 Reputation points
2021-03-29T01:08:20.37+00:00

Hi

I have got a C# application that inserts data to different tables in the database, the table names are set dynamically, even the tables are created dynamically using stored procedure( which I managed to do). The table columns are fixed for all these tables.

Should I use dynamic sql statement to do insert or are there other ways I can get this done.
Advice and feedback appreciated

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,486 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,536 questions
{count} votes

Accepted answer
  1. Erland Sommarskog 100.1K Reputation points MVP
    2021-03-29T22:09:58.003+00:00

    You should probably go back to the drawing board. In a relational database, a table is supposed to model a unique entity, with a unique set of attributes. So dynamically created tables kind goes across that idea.

    You say that all these tables have the same column. So I supposed there is something in the name that identifies the table. That something should probably be a key column in your one and single table.

    And your life will be a lot easier when you play by the rules.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Kalpana 286 Reputation points
    2021-03-31T22:09:18.523+00:00

    Hi
    I think my questions were not clear..so anyway, I did not proceed with the dynamic sql insert into statements, I decided to write the statements in C# application, get it triggered by a button click. I find this a better approach..as otherwise I would have to dynamically enter sql statements..