dbTalk Databases Forums  

Adding Multiple Rows via (?)Stored Procedure

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Adding Multiple Rows via (?)Stored Procedure in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Gene Wirchenko
 
Posts: n/a

Default Adding Multiple Rows via (?)Stored Procedure - 03-31-2011 , 05:53 PM






Dear SQLers:

So now I know how to get the batch's id. I could code something
like:

insert into Batches ('Example Batch')

declare @BatchNr int
select @BatchNr=scope_identity()

insert into Transactions
(TrnDt,BatchNr,Account,Amount)
values
('20110331',@BatchNr,'Income',-500),
('20110331',@BatchNr,'Reserves',200),
('20110331',@BatchNr,'Income Tax S/A',200)

This is, of course, not something that I want in app code. I
would create a stored procedure.

Is the correct approach to in app code create a cursor variable,
load it with the Transactions-to-be rows, and call the stored
procedure with the parameters BatchName and the cursor? Is there a
better way?

(You can post code if you want, but I really just want to know
the correct approach. I would probably do better to try to do it
myself.)

Sincerely,

Gene Wirchenko

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Adding Multiple Rows via (?)Stored Procedure - 04-01-2011 , 02:24 AM






Gene Wirchenko (genew (AT) ocis (DOT) net) writes:
Quote:
So now I know how to get the batch's id. I could code something
like:

insert into Batches ('Example Batch')

declare @BatchNr int
select @BatchNr=scope_identity()

insert into Transactions
(TrnDt,BatchNr,Account,Amount)
values
('20110331',@BatchNr,'Income',-500),
('20110331',@BatchNr,'Reserves',200),
('20110331',@BatchNr,'Income Tax S/A',200)

This is, of course, not something that I want in app code. I
would create a stored procedure.

Is the correct approach to in app code create a cursor variable,
load it with the Transactions-to-be rows, and call the stored
procedure with the parameters BatchName and the cursor? Is there a
better way?
With SQL 2008, and if your client API supports it, the correct approach
is to pass the transactions in a table-valued parameter. Else you can
use XML to send all data at once.

See http://www.sommarskog.se/arrays-in-sql-2008.html for an introduction
to TVPs and http://www.sommarskog.se/arrays-in-sql-2005.html#XML for how
to use XML.


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
  #3  
Old   
--CELKO--
 
Posts: n/a

Default Re: Adding Multiple Rows via (?)Stored Procedure - 04-01-2011 , 09:09 AM



You can load a table parameter. That locks you into proprietary code
and makes it harder to check that debits and credits balance (your
example did not, if i am reading it correctly).

You can use a long parameter list and do the math and validations in
the same procedure. This will be portable code.

Reply With Quote
  #4  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Adding Multiple Rows via (?)Stored Procedure - 04-01-2011 , 04:37 PM



--CELKO-- (jcelko212 (AT) earthlink (DOT) net) writes:
Quote:
You can use a long parameter list and do the math and validations in
the same procedure. This will be portable code.
Most of all it will be completely unusable garbage, and SQL Server the
performance penalty is severe. It's an awfully bad suggestion.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.