SQL Server加入两个表的结果

时间:2013-07-30 21:45:50

标签: sql sql-server

我想将两个表的结果组合在一起,因为当我单独执行它时,我得到了NULLS

这是我的代码

DECLARE @origSiteID int = 836,
    @NewSiteID int = 838


SELECT tblControls_Rooms.ID, @origSiteID
FROM tblControls_Rooms
WHERE SiteID = @OrigSiteID

SELECT tblControls_Rooms.ID, @NewSiteID
FROM tblControls_Rooms
WHERE SiteID = @newSiteID

我跑的时候:

ID  (No column name)
1942    836
1943    836
1944    836
1945    836
1946    836

ID  (No column name)
2025    838
2026    838
2027    838
2028    838

我希望结果是:

ID  (No column name)
1942    836
1943    836
1944    836
1945    836
1946    836
2025    838
2026    838
2027    838
2028    838

2 个答案:

答案 0 :(得分:2)

Union会工作,你也可以使用......

SELECT ID, SiteID
FROM tblControls_Rooms
WHERE SiteID IN (@OrigSiteID, @NewSiteID)

答案 1 :(得分:0)

我认为这对你有用:

SELECT DISTINCT(tblControls_Rooms.ID), tblControls_Rooms.SiteID
FROM tblControls_Rooms
WHERE SiteID = @OrigSiteID
OR SiteID = @newSiteID

您可能不需要DISTINCT