您的位置:首页 > 数据库

SQL游标使用实例

2012-02-24 11:12 393 查看
 declare @vTblName varchar(50)

            , @vExecSql  varchar(8000)
 

declare  csr_GetTblName  cursor

for

select NAME

from sysobjects as O

where XTYPE='U'

   and Exists(select 1 from syscolumns where ID=O.ID and  NAME='列名')  --查询所有表中有相关列名的表

open csr_GetTblName

fetch next from csr_GetTblName into @vTblName

while @@Fetch_status = 0

begin

print  @vTblName

 set   @vExecSql = 

                ' if exists(  select 1  from  '+ @vTblName + ' as A '

                                                                            +'  )  '

               +  '    begin  '

               +  '         要操作的数据'

                  + ' end '

   Exec (@vExecSql)

 

   fetch next from csr_GetTblName into @vTblName

end

close csr_GetTblName

deallocate csr_GetTblName

GO
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql