DECLARE @id varchar(50),@message varchar(100)DECLARE test_cursor CURSOR FOR 
select id from t1OPEN test_cursorFETCH NEXT FROM test_cursor 
INTO @id...???PRINT @messageCLOSE test_cursor
DEALLOCATE test_cursor
GOselect id from t1 的查询结果是
1
2
3
...我想通过游标动态创建Update语句,得到结果样式是在信息显示的样式是:UPDATE T1 SET ID = 1 WHERE ... 
UPDATE T1 SET ID = 2 WHERE ...
UPDATE T1 SET ID = 3 WHERE ...
...

解决方案 »

  1.   

    ...
    fetch next from test_cursor into @id
    while @@fetch_status = 0
    begin
      set @message = 'update t1 set id = ' + @id + 'where ...'
      print @message
      fetch next from test_sursor into @id
    end
    close test_cursor
    ...
      

  2.   

    create table t1([id] int)
    insert into t1 select 1
    insert into t1 select 2
    insert into t1 select 3
    godeclare @sql varchar(8000)
    set @sql=''
    select @sql=@sql+char(13)+'update t1 set id='+cast([id] as varchar)+' where 条件' from t1
    print @sqldrop table t1 
    --这样?