Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
RMiranda
Starting Member
2 Posts |
Posted - 2011-10-29 : 08:37:45
|
Hi, I have a question regarding SQL Server stored procedures.Imagining that there is procedure to wich i only have access to the header:procFoo (par1 int, par2 int); In an application I have a table on which I run this procedure with every row. I Intended to optimize creating a new procedure with the following body:create type tbType the table (col1 int, col2 int)create newProc (@ tbPar tbType)as --What to do here!? (Without using cursor or while)go |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-29 : 09:38:09
|
you can use set based query on table likecreate type tbType the table (col1 int, col2 int)create newProc (@tbPar tbType)as..selectfrom @tbPar join othertable.... ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
paultech
Yak Posting Veteran
79 Posts |
Posted - 2011-10-30 : 06:18:45
|
Hi ,Welcome first to sql Team you can take a look to below idea and apply it on your Codequote: he trick is to introduce a table of values (named, in the example below, MyTableOfIntegers) which contains all the integer values between 1 and (at least) some value (in the case at hand, that would be the biggest possible Quantity value from OrderTransaction table).INSERT INTO #OrderDelivery ([OrderTransactionId], [MediaTypeId]) SELECT OT.OrderTransactionId, P.MediaTypeId FROM #OrderTransaction OT WITH (NOLOCK) INNER JOIN #Product P WITH (NOLOCK) ON OT.ProductId = P.ProductId JOIN MyTableOfIntegers I ON I.Num <= OT.Quantity --WHERE some optional conditions Essentially the extra JOIN on MyTableOfIntegers, produces as many duplicate rows as OT.Quantity, and that seems to be what the purpose of the cursor was: to insert that many duplicated rows in the OrderDelivery table.I didn't check the rest of the logic with the temporary tables and all (I'm assuming these are temp tables for the purpose of checking the logic rather than being part of the process proper), but it seems that the above is the type of construct needed to express the needed logic in declarative fashion only, without any cursor or even any loop.
For further details gotohttp://stackoverflow.com/questions/2209771/sql-server-improve-procedure-without-using-cursorpaul Tech |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2011-10-30 : 06:40:23
|
| Your example is to duplicate a row based on a Quantity column, which seems to have no relevant to the O/P's question - unless I'm missing something?Using WITH (NOLOCK) on a #TEMP table seems to be spurious too |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-31 : 04:52:33
|
quote: Originally posted by paultech Hi ,Welcome first to sql Team you can take a look to below idea and apply it on your Codequote: he trick is to introduce a table of values (named, in the example below, MyTableOfIntegers) which contains all the integer values between 1 and (at least) some value (in the case at hand, that would be the biggest possible Quantity value from OrderTransaction table).INSERT INTO #OrderDelivery ([OrderTransactionId], [MediaTypeId]) SELECT OT.OrderTransactionId, P.MediaTypeId FROM #OrderTransaction OT WITH (NOLOCK) INNER JOIN #Product P WITH (NOLOCK) ON OT.ProductId = P.ProductId JOIN MyTableOfIntegers I ON I.Num <= OT.Quantity --WHERE some optional conditions Essentially the extra JOIN on MyTableOfIntegers, produces as many duplicate rows as OT.Quantity, and that seems to be what the purpose of the cursor was: to insert that many duplicated rows in the OrderDelivery table.I didn't check the rest of the logic with the temporary tables and all (I'm assuming these are temp tables for the purpose of checking the logic rather than being part of the process proper), but it seems that the above is the type of construct needed to express the needed logic in declarative fashion only, without any cursor or even any loop.
For further details gotohttp://stackoverflow.com/questions/2209771/sql-server-improve-procedure-without-using-cursorpaul Tech
are you aware of problems in using NOLOCK hint?seehttp://visakhm.blogspot.com/2010/02/avoiding-deadlocks-using-new.html------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
|
|
|
|
|