我的查询没有做我想要的,我不知道如何解决这个问题:
DECLARE @RoomMap TABLE
(ID int IDENTITY(1,1),
SourceRoom int,--> SourceRoomID
SourceSiteID int,
TargetRoom int, -->DemoRoomID
TargetSiteID int
)
INSERT INTO @RoomMap
(SourceRoom, SourceSiteID)
SELECT tblControls_Rooms.ID, @origSiteID
FROM tblControls_Rooms
WHERE SiteID = @OrigSiteID
INSERT INTO @RoomMap
(TargetRoom, TargetSiteID)
SELECT tblControls_Rooms.ID, @NewSiteID
FROM tblControls_Rooms
WHERE SiteID = @newSiteID
INSERT INTO DemoRoomMap
(DemoRoomID, SourceRoomID)
SELECT TargetRoom, SourceRoom
FROM @RoomMap
当我运行时,这是DemoRoomMap表:
TargetRoom SourceRoom
332 2
333 3
334 4
335 5
336 6
337 9
338 10
运行上述查询时的结果:
TargetRoom SourceRoom
NULL 1942
NULL 1943
NULL 1944
NULL 1945
NULL 1946
2025 NULL
2026 NULL
2027 NULL
2028 NULL
如您所见,有NULL
个值我真的不想插入!我该怎样摆脱它们?
答案 0 :(得分:2)
您可以通过在其中插入值来摆脱它们。请注意,此语句不会在TargetRoom
中插入值:
INSERT INTO @RoomMap
(SourceRoom, SourceSiteID)
SELECT tblControls_Rooms.ID, @origSiteID
FROM tblControls_Rooms
WHERE SiteID = @OrigSiteID
此外,后续声明不会将值插入SourceRoom
:
INSERT INTO @RoomMap
(TargetRoom, TargetSiteID)
SELECT tblControls_Rooms.ID, @NewSiteID
FROM tblControls_Rooms
WHERE SiteID = @newSiteID
因此,当您完成后,您将获得一组没有TargetRoom
的行和一组没有SourceRoom
的行。
答案 1 :(得分:0)
DECLARE @RoomMap TABLE
(ID int IDENTITY(1,1),
SourceRoom int,--> SourceRoomID
SourceSiteID int,
TargetRoom int, -->DemoRoomID
TargetSiteID int
)
INSERT INTO @RoomMap
(SourceRoom, SourceSiteID, TargetRoom TargetSiteID )
SELECT S.ID, @origSiteID, T.ID, @NewSiteID
FROM
(
SELECT ID FROM tblControls_Rooms WHERE SiteID = @OrigSiteID
) S
CROSS JOIN
(
SELECT ID FROM tblControls_Rooms WHERE SiteID = @NewSiteID
) T