SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
ALTER procedure FindFieldByOneNvarchar @tablename Nvarchar(20),@OneRowSQL Nvarchar(4000),@InOneNvarchar Nvarchar(20)
as
/*
在一个表中找出与@InOneNvarchar相同值的字段名称
调用示例:Exec FindFieldByOneNvarchar N'default_setting',N'select * from default_setting',2
*/
declare @name nvarchar(100)
declare @StrSQL nvarchar(4000)
declare @Fresult nvarchar(200)
declare Rowsetcur cursor for select name from syscolumns where id=object_id(@tablename,N'Table')
open Rowsetcur
fetch next From Rowsetcur into @name
while @@fetch_status=0
begin
set @StrSQL=N'select @Fresult=cast(a.['+@name+'] as Nvarchar(200)) from ('+ @OneRowSQL+') a '
--print @strSQL
Execute SP_executesql @StrSQL,N'@Fresult Nvarchar(200) out',@Fresult out
--print @Fresult+':'+@InOneNvarchar
if @@error<>0 begin print @name+':error!' break end
if @Fresult=@InOneNvarchar
print @name+';'
fetch next from Rowsetcur into @name
end
close Rowsetcur
deallocate Rowsetcur
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO