俩游标不能嵌套用么。

马猴烧酒123 2017-09-25 12:21:55
 DECLARE @sql varchar(2000),@id varchar(2000)  --申明变量
--申明一个游标
DECLARE authors_cursor CURSOR FOR
select tablename,PropertyName from EntityTable
--打开游标
OPEN authors_cursor
--取出值
FETCH NEXT FROM authors_cursor INTO @sql ,@id
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
------------ 第二层
DECLARE @sql2 varchar(2000) --申明变量
--申明一个游标
DECLARE authors_cursor2 CURSOR FOR
exec('select ' +@id +' as id from '+ @sql)
--打开游标
OPEN authors_cursor2
--取出值
FETCH NEXT FROM authors_cursor INTO @sql2
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
exec pwf_Del @sql2
FETCH NEXT FROM authors_cursor2
INTO @sql2
END
CLOSE authors_cursor2 --关闭游标
DEALLOCATE authors_cursor2 --释放游标
--------------
FETCH NEXT FROM authors_cursor
INTO @sql,@id
END
CLOSE authors_cursor --关闭游标
DEALLOCATE authors_cursor --释放游标



-------------------------------------------------------------------------------------------------------------------
exec('select ' +@id +' as id from '+ @sql)
这一行执行出现错误 直接红错无法执行-- 关键字 'exec' 附近有语法错误。
我换成 select @id as id from @sql
会提示我 --必须声明表变量 "@sql"。
--------------------------------------------------------------------------------------------------------------------

EntityTable 表存的一些表名和对应的主键字段。
结果如
select 'tba'tablename ,'id' PropertyName
union all
select 'tba2' ,'guid'


然后想在第二层,查询出来的结果调用
select id from tba
结果如
select '1231' id
union all
select '2356'

再 exec pwf_Del 这个 之前写的 存储过程,参数就是 数据的主键

exec pwf_Del '1231'
exec pwf_Del '2356'


...全文
308 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
听雨停了 2017-09-25
  • 打赏
  • 举报
回复

DECLARE @sql2 varchar(2000)  --申明变量
--申明一个游标
DECLARE authors_cursor2 CURSOR FOR 
exec('select ' +@id +' as id from '+ @sql)
--打开游标
OPEN authors_cursor2
--取出值
FETCH NEXT FROM authors_cursor INTO @sql2 
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
 exec pwf_Del @sql2
  FETCH NEXT FROM authors_cursor2 
  INTO @sql2
END
CLOSE authors_cursor2 --关闭游标
DEALLOCATE authors_cursor2 --释放游标
把上面这一段内容改成下面这样

DECLARE @authors_cursor2 VARCHAR(max) --最上面定义一个变量(把执行游标2的所有字符都拼接到里面)
SET @authors_cursor2=                                           
N'DECLARE @sql2 varchar(2000) '+
N'DECLARE authors_cursor2 CURSOR FOR '+
N'select ' +@id +' as id from '+ @sql+
N' OPEN authors_cursor2'+
N'FETCH NEXT FROM authors_cursor INTO @sql2 '+
N'WHILE @@FETCH_STATUS = 0 '+
N'BEGIN '+
N'exec pwf_Del @sql2 '+
  N'FETCH NEXT FROM authors_cursor2 '+
  N'INTO @sql2 '+
N'END '+
N'CLOSE authors_cursor2 '+
N'DEALLOCATE authors_cursor2 ' 
PRINT @authors_cursor2 
EXEC (@authors_cursor2) 
这样试试看行不行
听雨停了 2017-09-25
  • 打赏
  • 举报
回复
引用 3 楼 wp243173236 的回复:
会提示这个。 每一句都提示 消息 16924,级别 16,状态 1,第 1 行 Cursorfetch: INTO 列表中声明的变量数目必须与所选列的数目相同。
DECLARE @sql2 varchar(2000) DECLARE authors_cursor2 CURSOR FOR select ID as id from DFN_ZTH_FBS OPEN authors_cursor2 FETCH NEXT FROM authors_cursor INTO @sql2 WHILE @@FETCH_STATUS = 0 BEGIN exec pwf_Del @sql2 FETCH NEXT FROM authors_cursor2 INTO @sql2 END CLOSE authors_cursor2 DEALLOCATE authors_cursor2 上面红色地方少了个2,应该是authors_cursor2 用下面这个试试

DECLARE @authors_cursor2 VARCHAR(max) --最上面定义一个变量(把执行游标2的所有字符都拼接到里面)
SET @authors_cursor2=                                           
N'DECLARE @sql2 varchar(2000) '+
N'DECLARE authors_cursor2 CURSOR FOR '+
N'select ' +@id +' as id from '+ @sql+
N' OPEN authors_cursor2'+
N'FETCH NEXT FROM authors_cursor2 INTO @sql2 '+
N'WHILE @@FETCH_STATUS = 0 '+
N'BEGIN '+
N'exec pwf_Del @sql2 '+
  N'FETCH NEXT FROM authors_cursor2 '+
  N'INTO @sql2 '+
N'END '+
N'CLOSE authors_cursor2 '+
N'DEALLOCATE authors_cursor2 ' 
PRINT @authors_cursor2 
EXEC (@authors_cursor2)
OwenZeng_DBA 2017-09-25
  • 打赏
  • 举报
回复
引用 楼主 wp243173236 的回复:
 DECLARE @sql varchar(2000),@id varchar(2000)  --申明变量
--申明一个游标
DECLARE authors_cursor CURSOR FOR 
 select  tablename,PropertyName from EntityTable  
--打开游标
OPEN authors_cursor
--取出值
FETCH NEXT FROM authors_cursor INTO @sql ,@id
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
------------ 第二层
DECLARE @sql2 varchar(2000)  --申明变量
--申明一个游标
DECLARE authors_cursor2 CURSOR FOR 
 exec('select ' +@id +' as id from '+ @sql)
--打开游标
OPEN authors_cursor2
--取出值
FETCH NEXT FROM authors_cursor INTO @sql2 
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
 exec pwf_Del @sql2
  FETCH NEXT FROM authors_cursor2 
  INTO @sql2
END
CLOSE authors_cursor2 --关闭游标
DEALLOCATE authors_cursor2 --释放游标
--------------
  FETCH NEXT FROM authors_cursor 
  INTO @sql,@id
END
CLOSE authors_cursor --关闭游标
DEALLOCATE authors_cursor --释放游标
------------------------------------------------------------------------------------------------------------------- exec('select ' +@id +' as id from '+ @sql) 这一行执行出现错误 直接红错无法执行-- 关键字 'exec' 附近有语法错误。 我换成 select @id as id from @sql 会提示我 --必须声明表变量 "@sql"。 -------------------------------------------------------------------------------------------------------------------- EntityTable 表存的一些表名和对应的主键字段。 结果如 select 'tba'tablename ,'id' PropertyName union all select 'tba2' ,'guid' 然后想在第二层,查询出来的结果调用 select id from tba 结果如 select '1231' id union all select '2356' 再 exec pwf_Del 这个 之前写的 存储过程,参数就是 数据的主键 exec pwf_Del '1231' exec pwf_Del '2356'
可以嵌套使用。你这个问题主要是变量的作用域的问题。
  • 打赏
  • 举报
回复
你这个应该是 动态游标的用法,oracle是可以的 我不太清除 sqlserver 支不支持
马猴烧酒123 2017-09-25
  • 打赏
  • 举报
回复
引用 1 楼 qq_37170555 的回复:

DECLARE @sql2 varchar(2000)  --申明变量
--申明一个游标
DECLARE authors_cursor2 CURSOR FOR 
exec('select ' +@id +' as id from '+ @sql)
--打开游标
OPEN authors_cursor2
--取出值
FETCH NEXT FROM authors_cursor INTO @sql2 
--循环取出游标的值
WHILE @@FETCH_STATUS = 0
BEGIN
 exec pwf_Del @sql2
  FETCH NEXT FROM authors_cursor2 
  INTO @sql2
END
CLOSE authors_cursor2 --关闭游标
DEALLOCATE authors_cursor2 --释放游标
把上面这一段内容改成下面这样

DECLARE @authors_cursor2 VARCHAR(max) --最上面定义一个变量(把执行游标2的所有字符都拼接到里面)
SET @authors_cursor2=                                           
N'DECLARE @sql2 varchar(2000) '+
N'DECLARE authors_cursor2 CURSOR FOR '+
N'select ' +@id +' as id from '+ @sql+
N' OPEN authors_cursor2'+
N'FETCH NEXT FROM authors_cursor INTO @sql2 '+
N'WHILE @@FETCH_STATUS = 0 '+
N'BEGIN '+
N'exec pwf_Del @sql2 '+
  N'FETCH NEXT FROM authors_cursor2 '+
  N'INTO @sql2 '+
N'END '+
N'CLOSE authors_cursor2 '+
N'DEALLOCATE authors_cursor2 ' 
PRINT @authors_cursor2 
EXEC (@authors_cursor2) 
这样试试看行不行
PRINT倒是能打印出来数据
  DECLARE @sql2 varchar(2000) 
  DECLARE authors_cursor2 CURSOR FOR 
  select ID as id from DFN_ZTH_FBS 
  OPEN authors_cursor2 
  FETCH NEXT FROM authors_cursor INTO @sql2  
 WHILE @@FETCH_STATUS = 0  
 BEGIN  
 exec pwf_Del   @sql2  
 FETCH NEXT FROM authors_cursor2  
 INTO @sql2 
  END  
  CLOSE authors_cursor2 
   DEALLOCATE authors_cursor2  
会提示这个。 每一句都提示 消息 16924,级别 16,状态 1,第 1 行 Cursorfetch: INTO 列表中声明的变量数目必须与所选列的数目相同。
二月十六 2017-09-25
  • 打赏
  • 举报
回复
俩游标嵌套没有问题,楼主的主要问题是给游标赋值的问题,不能直接execsql语句,可以换成临时表试试:
CREATE TABLE #temp ( id INT )
INSERT  INTO #temp
        EXEC ( 'select ' + @id + ' as id from ' + @sql
            )
DECLARE authors_cursor2 CURSOR
FOR
    SELECT  *
    FROM    #temp
DROP TABLE #temp

27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧