开发者

Execute statements for every record in a table

开发者 https://www.devze.com 2022-12-10 16:15 出处:网络
I have a temporary table (or, say, a function which returns a table of values). I want to execute some statements for each record in the table.

I have a temporary table (or, say, a function which returns a table of values).

I want to execute some statements for each record in the table. Ca开发者_开发知识库n this be done without using cursors?

I'm not opposed to cursors, but would like a more elegant syntax\way of doing it.

Something like this randomly made-up syntax:

for (select A,B from @temp) exec DoSomething A,B

I'm using Sql Server 2005.


I dont think what you want to to is that easy.

What i have found is that you can create a scalar function taking the arguments A and B and then from within the function execute an Extended Stored Procedure. This might achieve what you want to do, but it seems that this might make the code even more complex.

I think for readibility and maintainability, you should stick to the CURSOR implementation.


I would look into changing the stored proc so that it can work against a set of data rather than a single row input.


Would CROSS/OUTER APPLY do what you want if you need RBAR processing.

It's elegant, but depends on what processing you need to do

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号