27,579
社区成员
发帖
与我相关
我的任务
分享
exec('declare @Sql varchar(50)
set @Sql=''''
select @Sql=@Sql+'',e.[''+QuestName+'']'' from (select QuestName from [动态表] ) as a
')
print(@Sql_row)
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int)
insert [tb]
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8
--以下示例为将一表的字段合并输出,表为变量
declare @id varchar(100)
declare @tb varchar(50)
set @tb='tb' ---这里是表变量
declare @s nvarchar(500)
set @s='select @id=isnull(@Id+'','','''')+ltrim(id) from '+@tb+''
exec sp_executesql @s, N'@id varchar(100) output', @id output
print @id
/*1,2,3,4,5,6,7,8*/
exec('declare @Sql varchar(50)
set @Sql=''''
select @Sql=@Sql+'',e.[''+QuestName+'']'' from (select QuestNamefrom Customers ) as a
select @Sql as QuestNameinto ##temp
')
select * from ##temp