Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.databases.ms-sqlserver > #88
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!feeder.news-service.com!85.214.198.2.MISMATCH!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| 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 | Fri, 01 Apr 2011 09:24:39 +0200 |
| Organization | Erland Sommarskog |
| Lines | 40 |
| Message-ID | <Xns9EBA5FBB7B613Yazorman@127.0.0.1> (permalink) |
| References | <2s0ap6l564deoh3kp46dh92brn1g3kr1m0@4ax.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252 |
| Content-Transfer-Encoding | 8bit |
| Injection-Info | mx01.eternal-september.org; posting-host="DD6dU+BfJNjsjSP4/K/V7w"; logging-data="18372"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+xxda6n/KZycm+/ddzSPEP" |
| User-Agent | Xnews/2006.08.24 Mime-proxy/2.1.c.0 (Win32) |
| Cancel-Lock | sha1:5ZcL1dBVdJaexDLegRwIUN36jJI= |
| Xref | x330-a1.tempe.blueboxinc.net comp.databases.ms-sqlserver:88 |
Cross-posted to 2 groups.
Followups directed to: comp.databases.ms-sqlserver
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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