码迷,mamicode.com
首页 > 数据库 > 详细

sqlserver 游标

时间:2016-01-25 14:47:39      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

DECLARE ChangeInvCodeCursor CURSOR
FOR SELECT A.name AS tablecolumn,C.name AS tablename FROM sys.columns A LEFT JOIN sys.types B ON A.user_type_id = B.user_type_id RIGHT JOIN SYS.tables C ON C.object_id=A.object_id WHERE A.name =‘CINVCODE‘OR A.name=‘INVCODE‘ ORDER BY C.NAME
--打开游标
OPEN ChangeInvCodeCursor
DECLARE @tablecolumn1 nvarchar(120)
DECLARE @tablename1 nvarchar(120)
DECLARE @strsql nvarchar(2048)
DECLARE @BeforeCode nvarchar(120)
DECLARE @afterCode nvarchar(120)
FETCH NEXT FROM ChangeInvCodeCursor INTO @tablecolumn1,@tablename1
WHILE @@FETCH_STATUS =0
BEGIN
--变化前存货编码
set @BeforeCode=‘0111‘
--变化后存货编码
set @afterCode=‘0222‘
set @strsql=‘update ‘+@tablename1+‘ set ‘+@tablecolumn1+‘=‘‘‘+@afterCode+‘‘‘ where ‘+@tablecolumn1+‘=‘‘‘+@BeforeCode+‘‘‘‘
exec(@strsql)
FETCH NEXT FROM ChangeInvCodeCursor INTO @tablecolumn1,@tablename1
END
--关闭游标
CLOSE ChangeInvCodeCursor
DEALLOCATE ChangeInvCodeCursor

sqlserver 游标

标签:

原文地址:http://www.cnblogs.com/passerlee/p/5157154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!