SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Update Main and Details
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

swissivan
Starting Member

30 Posts

Posted - 03/03/2013 :  22:18:22  Show Profile  Reply with Quote
I got two Tables 'Purchase Order' abd 'Purchase Order Products'.

I am using c# program to call store procedure.

I would like to generate id and insert record for 'Purchase Order' first. Then insert records for 'Purchase Order Products' since it includes the ids generated.

If just calling a single store procedure, I can use TRY CATCH and ROLLBACK to make sure updates of two tables in sync. However, I do not know how to pass a list of information (i.e. all records for Purchase Order Product) into a single store procedure.

e.g. pass the following list as parameters
each Product include name, qty, amount
ProductA, 1, $20
ProductB, 3, $28
...

So I assume calling insert product procedure each time for one product, but then it cannot rollback the inserted row of 'Purchase Order' if any errors occur in inserting 'Products'.

Is there any practice that can solve my situation so I could make sure all records (Order and Products) will be added or no records will be added and error is known?







Edited by - swissivan on 03/03/2013 22:26:12

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/03/2013 :  22:27:11  Show Profile  Reply with Quote
you're multiple ways to do this

see

http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 03/03/2013 :  22:37:55  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

you're multiple ways to do this

see

http://vyaskn.tripod.com/passing_arrays_to_stored_procedures.htm

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Thanks, it is a solution for 2d array.

My situation is more complicated as 3d array and I got many fields.
ProductA, 1, $20, Adescription, ... etc
ProductB, 3, $28, Bdescription, ... etc
...

I need to pass many info for a lot of records, so the total number of values to be passed in store procedure may be more than 100 and which is unknown.
Any enhancement or tricks for the solution that can help? thanks,



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/03/2013 :  22:42:35  Show Profile  Reply with Quote
what do you mean 3d array? A table has only 2 dimensions- row and column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 03/03/2013 :  22:56:20  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

what do you mean 3d array? A table has only 2 dimensions- row and column

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





Thank and sorry I got typing mistake, it is a solution for 1d array.

My situation is more complicated as 2d array and I got many fields.
ProductA, 1, $20, Adescription, ... etc
ProductB, 3, $28, Bdescription, ... etc
...

I need to pass many info for a lot of records, so the total number of values to be passed in store procedure may be more than 100 and which is unknown.
Any enhancement or tricks for the solution that can help? thanks,
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/03/2013 :  22:59:44  Show Profile  Reply with Quote
tell us your scenario first. i didnt understand whats the issue with methods in posted link

whatever be number of values you can always use a method like XML to pass them and then use XML functions like nodes, query to shred the data onto the tables.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

swissivan
Starting Member

30 Posts

Posted - 03/04/2013 :  05:25:33  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

tell us your scenario first. i didnt understand whats the issue with methods in posted link

whatever be number of values you can always use a method like XML to pass them and then use XML functions like nodes, query to shred the data onto the tables.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/





After more research, xml seems a good direction and I would explore more to see how can it apply to my application.

Thanks.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 03/04/2013 :  06:02:35  Show Profile  Reply with Quote
ok...cool... you can do like

<Node>
<Field1></Field1>
<Field2></Field2>
...
</Node>

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000