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.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Optimizing SP

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 like

create type tbType the table (col1 int, col2 int)
create newProc (@tbPar tbType)
as
..
select
from @tbPar
join othertable
....



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

Go to Top of Page

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 Code

quote:
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 goto
http://stackoverflow.com/questions/2209771/sql-server-improve-procedure-without-using-cursor


paul Tech
Go to Top of Page

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
Go to Top of Page

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 Code

quote:
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 goto
http://stackoverflow.com/questions/2209771/sql-server-improve-procedure-without-using-cursor


paul Tech


are you aware of problems in using NOLOCK hint?

see
http://visakhm.blogspot.com/2010/02/avoiding-deadlocks-using-new.html

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

Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2011-10-31 : 14:34:41
Obviously, it's agood to know about the different isolation levels. But, the isolation level to use can depend on many factors. I realize this is a bit off topic, but here is a link to an article that talks about some "issues" that people who do not fully understand the isolation levels might have with RCSI.

http://blogs.msdn.com/b/sqlcat/archive/2011/03/03/comparing-different-results-with-rcsi-amp-read-committed.aspx
Go to Top of Page
   

- Advertisement -