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


Groups > comp.databases.ms-sqlserver > #87 > unrolled thread

Adding Multiple Rows via (?)Stored Procedure

Started byGene Wirchenko <genew@ocis.net>
First post2011-03-31 15:53 -0700
Last post2011-04-01 23:37 +0200
Articles 4 — 3 participants

Back to article view | Back to comp.databases.ms-sqlserver


Contents

  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

#87 — Adding Multiple Rows via (?)Stored Procedure

FromGene Wirchenko <genew@ocis.net>
Date2011-03-31 15:53 -0700
SubjectAdding Multiple Rows via (?)Stored Procedure
Message-ID<2s0ap6l564deoh3kp46dh92brn1g3kr1m0@4ax.com>
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

[toc] | [next] | [standalone]


#88

FromErland Sommarskog <esquel@sommarskog.se>
Date2011-04-01 09:24 +0200
Message-ID<Xns9EBA5FBB7B613Yazorman@127.0.0.1>
In reply to#87
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

[toc] | [prev] | [next] | [standalone]


#89

From--CELKO-- <jcelko212@earthlink.net>
Date2011-04-01 07:09 -0700
Message-ID<c51b3355-b1b8-41a2-97df-57fb968326a4@d28g2000yqf.googlegroups.com>
In reply to#87
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.

[toc] | [prev] | [next] | [standalone]


#90

FromErland Sommarskog <esquel@sommarskog.se>
Date2011-04-01 23:37 +0200
Message-ID<Xns9EBAF0567124Yazorman@127.0.0.1>
In reply to#89
--CELKO-- (jcelko212@earthlink.net) writes:
> 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@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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.databases.ms-sqlserver


csiph-web