question

Kalpana-3650 avatar image
0 Votes"
Kalpana-3650 asked ErlandSommarskog commented

Ways on inserting data using stored procedure when TableName is dynamic

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-generalsql-server-transact-sql
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am not familiar with C#, so I am not sure about your specific needs. If you need dynamic statements to insert data, please provide tables and data.

Echo

0 Votes 0 ·
ErlandSommarskog avatar image
0 Votes"
ErlandSommarskog answered

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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Kalpana-3650 avatar image
0 Votes"
Kalpana-3650 answered ErlandSommarskog commented

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..

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

So rather than doing dynamic SQL in T-SQL, you did it in C#. Given that is all a matter of string manipulation, C# is a better tool. I hope that you are using parameterised statements!

But I still suspect that the real solution is to have a single table.

0 Votes 0 ·