【发布时间】:2016-01-20 01:24:13
【问题描述】:
是否有可能在不使用游标的情况下以某种方式为选择的每一行执行一些代码?
就我而言: 我有一个临时表来存储复杂脚本的一些数据。最后我想把这个表的一些信息(受某些条件限制)输出到输出中。
目前我正在使用带有选择的游标来限制表格的行。在这个光标中,我正在使用
print '...'
生成输出。
必须有更简单的方法来做这些事情......
编辑:
create table #tmpAttributes(AttributeId uniqueidentifier, Value float, ValueString nvarchar(max), ActionId uniqueidentifier)
insert into #tmpAttributes (AttributeId, Value, ValueString, ActionId)
select ID,..... -- in this select i'm doing some value conversions, if conversion is not possible i'm using -1
insert into ActionAttribute (ActionDefinitionID, Discriminator, ID, ReferredActionID, ValueDate, ValueListID, ValueMoney, ValueString, ValueUserID)
select @defId, 'ActionAttributeMoneyEntity', NEWID(), ActionId, null, null, Value, null, null from #tmpAttributes
-- afterwards there is this cursor where I'm printint all rows where Value = -1
【问题讨论】:
-
信息不足。你到底想达到什么目的?如果您向我们提供完整的场景,您可能会发现有一种完全基于集合的方式来满足您的需求。
-
为什么不在已经创建的“临时表”上多放一个标识列,然后为每一行循环。
-
如果您只想在“最后”打印这些信息,那为什么还需要光标呢?
-
您是否有理由需要
PRINT行,而不是仅仅在Value = -1所在的所有行上执行SELECT? -
我想为用户生成一个输出,向他展示转换失败的位置。但你是对的......这也可能是一个选择 -.-
标签: sql sql-server tsql