Ways on inserting data using stored procedure when TableName is dynamic

Kalpana 291 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

Developer technologies Transact-SQL
SQL Server Other
{count} votes

Accepted answer
  1. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    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 291 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..


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.