SQL Query抓取2个不同的Ids相同的表,加入和查询

时间:2013-10-24 10:16:22

标签: sql sql-server join

尝试使用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

1 个答案:

答案 0 :(得分:1)

你可以写成:

DECLARE @SQLString nvarchar(4000);
SELECT @SQLString = COALESCE(@SQLString + '; ', '') + SQLQuery 
FROM table1
where id in (2,3)

select @SQLString