尝试使用ID 2和ID 3加入他们。
因此,从表,ID和查询中获取2列,从表中获取2个不同的ID,然后将它们连接在一起。
SELECT a.ID, a.SQLQuery
FROM (SELECT ID, SQLQuery
FROM dbo.ReportQuery
WHERE (ID = 2)) AS a
INNER JOIN (SELECT ID, SQLQuery
FROM dbo.ReportQuery AS ReportQuery_1
WHERE (ID = 3)) AS b ON a.ID = b.ID
样本数据
以下是一些示例数据。试图获取ID = 2和ID = 3并将它们组合成一个语句。
ID SQLQuery
1 Select * from users where plc_id like @PLC_ID
2 Select * from stepgrouplog where (EndDateTime = '2009')
3 Select * from tmp_kosherStatus where unit=@unit
答案 0 :(得分:1)
你可以写成:
DECLARE @SQLString nvarchar(4000);
SELECT @SQLString = COALESCE(@SQLString + '; ', '') + SQLQuery
FROM table1
where id in (2,3)
select @SQLString