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)
 loop through each record of table value paramter

Author  Topic 

kamii47
Constraint Violating Yak Guru

353 Posts

Posted - 2012-03-07 : 04:58:36
I have table value parameter @tvpMyKeys.I am filling it through a select statement.
After filling it I have to loop through each entry of my table value parameter tvpMyKeys and perform some deletion.
How can I loop through all records of a table value parameter

Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-07 : 05:07:47
You can delete from a table with joining @tvpMyKeys.
Think set based!


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

kamii47
Constraint Violating Yak Guru

353 Posts

Posted - 2012-03-07 : 05:38:27
problem solved

Kamran Shahid
Principle Engineer Development
(MCSD.Net,MCPD.net)

Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-03-07 : 05:38:30
Something like this but you have to work out what to do in case of error / rollback and so on...

while exists(select * from @YourTablevar)
begin
begin transaction
set @MyID = (select top 1 @YourTablevar.Column from @YourTablevar)
delete from table1 where id=@MyID
...
commit transaction
delete from @YourTablevar where @YourTablevar.Column = @MyID
end



No, you're never too old to Yak'n'Roll if you're too young to die.

edit: For those who can't understand why I have posted this kind of "solution": it was a direct answer to a post which is now deleted, done by kamii47
Go to Top of Page
   

- Advertisement -