declare E1Cursor cursor
for select [name] from dbo.sysobjects where objectproperty(ID,N'IsTable')=1 and type='U' and [name]<>'dtproperties'
GO
open E1Cursor
declare @author varchar(50)
Fetch next from E1Cursor into @author
while(@@fetch_status=0)
begin
if exists(select * from syscolumns where id=object_id(@author) and [name]='MONTHd')
begin
update @author set MONTHd='2008-8-1';这句不行,为什么?说是“必须声明变量 '@author'。” -- update sysobjects set crdate='2008-8-1' where [name]=@author and type='U';这句可以
end
fetch next from E1Cursor into @author
endClose E1Cursor
Deallocate E1Cursor我这个的目的是,对一个库里面的全部用户表操作:
如果那个表有一个日期时间字段"MONTHd",那就改为'2008-8-1'
如果没有日期时间字段"MONTHd",就不用操作
就是这么简单
但是现在那个表名没有办法在update @author 这里使用请问怎么才能在那句话里面引用用户表名?谢谢先!
for select [name] from dbo.sysobjects where objectproperty(ID,N'IsTable')=1 and type='U' and [name]<>'dtproperties'
GO
open E1Cursor
declare @author varchar(50)
Fetch next from E1Cursor into @author
while(@@fetch_status=0)
begin
if exists(select * from syscolumns where id=object_id(@author) and [name]='MONTHd')
begin
update @author set MONTHd='2008-8-1';这句不行,为什么?说是“必须声明变量 '@author'。” -- update sysobjects set crdate='2008-8-1' where [name]=@author and type='U';这句可以
end
fetch next from E1Cursor into @author
endClose E1Cursor
Deallocate E1Cursor我这个的目的是,对一个库里面的全部用户表操作:
如果那个表有一个日期时间字段"MONTHd",那就改为'2008-8-1'
如果没有日期时间字段"MONTHd",就不用操作
就是这么简单
但是现在那个表名没有办法在update @author 这里使用请问怎么才能在那句话里面引用用户表名?谢谢先!
update @author set MONTHd='2008-8-1';这句不行,为什么?说是“必须声明变量 '@author'。”exec('update ['+@author+'] set MONTHd=''2008-8-1'')--改為動態
exec('update '+@author+' set MONTHd=''2008-8-1''')
改成
exec('update '+@author+' set MONTHd=''2008-8-1''');
sp_msforeachtable
'if exists(select 1 from syscolumns where id=object_id(''?'') and [name]=''MONTHd'')
exec(''update ? set MONTHd=''''2008-8-1'''''')
'
你的需求没必要定义成表变量,你只是动态更新表(动态组合表名)而已.定义表变量的语法如下:declare @t table(id int) ---定义了1个表变量,包含了一个INT的ID字段