SQL server 一次把一列的所有值都取出来保存到一个变量里


SQL server 一次把一列的所有值都取出来保存到一个变量里

---获得数据库wellytest里 好后缀为 _test所有用户表名,以逗号分隔表名保存到一个变量中
---
declare @sqlTblNames nvarchar(1000),@tableNames nvarchar(4000)
set @sqlTblNames='select @tableNames = @tableNames+'',''+cast([name] as varchar(40)) 
                                                from wellytest.dbo.sysobjects
                                                where xtype=''u'' and [name] like ''%_test'' ' 
        exec sp_executesql @sqlTblNames,N'@tableNames nvarchar(4000) output',@tableNames output 
        set @tableNames=stuff(@tableNames,1,1,'') 

原文地址:https://www.cnblogs.com/liuzhuqing/p/7480123.html