查找某個字段名在某些表

最近做程序,要把某些字段的批量值修改一下,例如性別代碼和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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章