Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Help With Insert To Multiple Tables _SP

Author  Topic 

besadmin
Posting Yak Master

116 Posts

Posted - 2011-05-09 : 11:54:02
Hey Friends!

I am trying to make a stored proc that will insert into about 15 different tables. So Is there a way to pass in all those values in each mode or something, rather than having to declare like 100 parameters for each??

Like this kind of...

IF @Mode = Table1
insert into table 1
var 1
, var 2
values
var 1
, var 2

ELSE IF @Mode = Table2
insert into table 2
var 1
, var 2
values
var 1
, var 2


So, how should i split those up? make a different SP for each table? using a mode somehow? or must i declare @@ variables at the top? which seems crazy becuase each insert will only have about 5-10 columns, but that would require like 100 variables to be declared at the top??

Any help is greatly appreciated as always freinds!!

Thanks so much in advance!!

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2011-05-09 : 13:25:27
You should not have a general all purpose stored procedure for this. Can't you use a stored procedure generator to write these out for you?

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2011-05-09 : 13:28:34
Sniped - but I'll proceed anyway :)

Good practice is to have an SP for each target table with well typed parameters. You definitely don't want to have a single master sp that performs all your database interaction. It becomes unruly, hard to read, and hard to maintain, execution plans will often be wrong, etc...

Be One with the Optimizer
TG
Go to Top of Page

besadmin
Posting Yak Master

116 Posts

Posted - 2011-05-10 : 12:05:52
Yes, I guess using the mode is better for different procs and 1 table or query.

I will use the separate sp's.

Thanks a ton for you quick responses guys!

Always appreciated so much!

Thanks again!
Go to Top of Page
   

- Advertisement -