all groups > sql server programming > august 2003 >
You're in the

sql server programming

group:

Is there any way to loop a temp table or table variable without cursor?



Is there any way to loop a temp table or table variable without cursor? hong_lok NO[at]SPAM yahoo.com.au
8/8/2003 8:42:48 PM
sql server programming: Hi!

Is there anyway to loop through the records on a temp table or a table
variable without using a cursor? Because of performance and memory
issue, one of the requirement for my current project is that we are
not allow to use cursor. I am trying to archive the following task:


For each rows within TEMPTABLE (OR TABLEVARIABLE)

EXEC STOREPRCOEDURE(row.id)

Loop

Thanks in advance!
Re: Is there any way to loop a temp table or table variable without cursor? oj
8/8/2003 8:55:01 PM
You add an identity column to the table and loop through it.

e.g.
declare @tb table(id int identity,title varchar(80))
insert @tb select title from pubs..titles

declare @i int, @txt varchar(255)
set @i=1
while 1=1
begin
select @txt='i: '+cast(@i as varchar)+' title: '+title from @tb where id=@i
if @@rowcount=0 break
print(@txt)
set @i=@i+1
end


--
-oj
RAC v2.2 & QALite!
http://www.rac4sql.net



[quoted text, click to view]

Re: Is there any way to loop a temp table or table variable without cursor? Dan Guzman
8/8/2003 10:53:29 PM
You can't execute a stored procedure 'for each row' without a cursor.
However, depending on the procedure details, you may be able to join to
the temp table within the stored procedure in order to perform set
processing.

--
Hope this helps.

Dan Guzman
SQL Server MVP

-----------------------
SQL FAQ links (courtesy Neil Pike):

http://www.ntfaq.com/Articles/Index.cfm?DepartmentID=800
http://www.sqlserverfaq.com
http://www.mssqlserver.com/faq
-----------------------

[quoted text, click to view]

Re: Is there any way to loop a temp table or table variable without cursor? Steve Kass
8/9/2003 1:12:30 AM
Lawrence,

If this is a one-shot deal and not part of production
code, you might consider the undocumented procedure
xp_execresultset. Here's an example of its use:

use tempdb
go

create proc p (
@i int
) as
print @i
go

declare @sql nvarchar(4000)
set @sql = '
select ''exec p '' + rtrim(OrderID)
from Northwind..Orders
'
exec master..xp_execresultset @sql, N'tempdb'
go

drop proc p

-- Steve Kass
-- Drew University
-- Ref: B0549F05-9856-4800-B1A3-F02D6A763857

[quoted text, click to view]
Re: Is there any way to loop a temp table or table variable without cursor? Joe Celko
8/9/2003 1:25:08 PM
[quoted text, click to view]
a table variable without using a cursor? <<

Records are not rows; failure to understand this is probably why someone
is still writing record-at-a-time code. That is not the right question,
so the only answers you will get are proprietary kludges. Most of the
time (99.9%), you can re-write the code and make it set-oriented.

The only exception that comes to mind are NP-complete optimization
problems for which one near-optimial answer will do. You do not run
into many of those outisde of a computer science department ...

--CELKO--
===========================
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in your
schema are.

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button