SQL Server - 光标
我正在尝试使用游标循环遍历表:
DEClARE @ProjectOID as nvarchar (100)
DECLARE @TaskOID as nvarchar (100)
DECLARE TaskOID_Cursor FOR
SELECT TaskOID FROM ProjectOID_Temp
OPEN TaskOID_Cursor
FETCH NEXT FROM TaskOID_Cursor INTO @TaskOID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT t1.OID as taskResourceOID, t2.OID as EvUserOID
FROM (select OID, resourceOID from taskresourcehours
where projecttaskoid = @TaskOID) as t1,
(
select OID, workerOID
from Evuser
where workerOID in
( select resourceOID from taskresourcehours where projecttaskoid = @TaskOID )
) as t2
WHERE t1.resourceOID = t2.workerOID
FETCH NEXT FROM TaskOID_Cursor
INTO @TaskOID
END
CLOSE TaskOID_Cursor
DEALLOCATE TaskOID_Cursor
上面返回 taskResourceOID 和 EvUserOID。如果我需要输出带有 @TaskOID 以及相应的 taskResourceOID 和 EvUserOID 的表,最好的方法是什么?
I am trying to loop through a table using a cursor:
DEClARE @ProjectOID as nvarchar (100)
DECLARE @TaskOID as nvarchar (100)
DECLARE TaskOID_Cursor FOR
SELECT TaskOID FROM ProjectOID_Temp
OPEN TaskOID_Cursor
FETCH NEXT FROM TaskOID_Cursor INTO @TaskOID
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT t1.OID as taskResourceOID, t2.OID as EvUserOID
FROM (select OID, resourceOID from taskresourcehours
where projecttaskoid = @TaskOID) as t1,
(
select OID, workerOID
from Evuser
where workerOID in
( select resourceOID from taskresourcehours where projecttaskoid = @TaskOID )
) as t2
WHERE t1.resourceOID = t2.workerOID
FETCH NEXT FROM TaskOID_Cursor
INTO @TaskOID
END
CLOSE TaskOID_Cursor
DEALLOCATE TaskOID_Cursor
That above returns taskResourceOID and EvUserOID. If I need to output a table with the @TaskOID and the respective taskResourceOID and EvUserOID, what is the best way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用临时表或表变量。
或者甚至更好,不要使用游标(这可以作为选择执行,但我将其留给您...只是想展示如何使用游标光标和表作为返回值)
Use a temporary table or a table variable..
Or even better, don't use a cursor (this can be performed as a select, but I leave this up to you... Just wanted to show how to use a cursor AND a table as return value)
未经测试,这应该可以在不使用游标的情况下工作:
Without having tested, this should work without using a cursor: