Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.databases.ms-sqlserver > #88

Re: Adding Multiple Rows via (?)Stored Procedure

From Erland Sommarskog <esquel@sommarskog.se>
Newsgroups comp.databases.ms-sqlserver, microsoft.public.sqlserver.programming
Subject Re: Adding Multiple Rows via (?)Stored Procedure
Followup-To comp.databases.ms-sqlserver
Date 2011-04-01 09:24 +0200
Organization Erland Sommarskog
Message-ID <Xns9EBA5FBB7B613Yazorman@127.0.0.1> (permalink)
References <2s0ap6l564deoh3kp46dh92brn1g3kr1m0@4ax.com>

Cross-posted to 2 groups.

Followups directed to: comp.databases.ms-sqlserver

Show all headers | View raw


Gene Wirchenko (genew@ocis.net) writes:
>      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@sommarskog.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

Back to comp.databases.ms-sqlserver | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Adding Multiple Rows via (?)Stored Procedure Gene Wirchenko <genew@ocis.net> - 2011-03-31 15:53 -0700
  Re: Adding Multiple Rows via (?)Stored Procedure Erland Sommarskog <esquel@sommarskog.se> - 2011-04-01 09:24 +0200
  Re: Adding Multiple Rows via (?)Stored Procedure --CELKO-- <jcelko212@earthlink.net> - 2011-04-01 07:09 -0700
    Re: Adding Multiple Rows via (?)Stored Procedure Erland Sommarskog <esquel@sommarskog.se> - 2011-04-01 23:37 +0200

csiph-web