Click here to Skip to main content
15,883,950 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

Suppose I have many tables and I want to insert data in every table. So, how can I use a single stored procedure to insert data in every table, instead of using one stored procedure for each table? Every time my table name and parameters will vary. If its possible, then please give me some basic idea about how to do this. If any extra information required, please ask.

Thanks and regards,
Rizwan Gazi.
Posted

1 solution

It's pretty simple.Create a single procedure and include your separate insert query

For eg:
SQL
Create procedure procedurename
@parameter1 bigint =0 ,
@parameter2 nvarchar(max)= null,
@parameter3 nvarchar(max) = null,
@type int
as 
begin

if(@type=1)
insert query

else if(@type=2)
another insert query 

----and so on

end


and in the front end, mention the query no which you want to execute

C#
cmd.CommandType = CommandType.StoredProcedure;

cmd.CommandText = "Procedurename";
cmd.Parameters.Add(new SqlParameter("@parametername", SqlDbType.VarChar, 50));
cmd.Parameters["@type"].Value = 1;//query no
 
Share this answer
 
v2
Comments
Rizwan Gazi 12-Apr-14 2:10am    
But dear Jas, if suppose I have 100 tables, then am I suppose to write if else 100 times? Can't it be done in a single insert query?
Tom Marvolo Riddle 12-Apr-14 2:14am    
single insert query for 100 tables?I don't think it is possible
Rizwan Gazi 12-Apr-14 2:20am    
But dear, I think the solution you provided, will definitely help me.
Tom Marvolo Riddle 12-Apr-14 2:22am    
will you please accept my answer?Thanx
Rizwan Gazi 12-Apr-14 3:00am    
Yes.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900