最近做程序,要把某些字段的批量值修改一下,例如性别代码和id转换,于是写了个函数,来调用。很简单贻笑大方了。
CREATE FUNCTION F_GettableNameList (@inColumnname as varchar(50))
returns varchar(500)
begin
--内部变量
declare @Column varchar(50),@table varchar(50),@stemp varchar(500)
--声明一个游标
set @stemp=''
declare mycursor cursor for
select distinct a.name as column_name,b.name as table_name from syscolumns a,sysobjects b where a.id=b.id and b.xtype='U' and a.name=''+@inColumnname+''
--打开一个游标
open mycursor
--提取数据
fetch next from mycursor
into @Column,@table
--如果提取数据成功
while @@fetch_status = 0
begin
set @stemp = @stemp + @table + ','
fetch next from mycursor
into @Column,@table
end
--去除多余逗号
if(len(@stemp)>1)
begin
set @stemp=substring(@stemp,1,len(@stemp)-1)
end
--print @stemp
--关闭游标,释放游标
close mycursor
deallocate mycursor
return(@stemp)
end
本文地址:http://com.8s8s.com/it/it43011.htm