1

I have a query that is not doing what I want, I am not sure how to solve this:

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

THIS is the DemoRoomMap table when I run it:

TargetRoom  SourceRoom
    332 2
    333 3
    334 4
    335 5
    336 6
    337 9
    338 10

The result when I run the above query:

TargetRoom  SourceRoom
NULL    1942
NULL    1943
NULL    1944
NULL    1945
NULL    1946
2025    NULL
2026    NULL
2027    NULL
2028    NULL

As you can see, there are NULL values which I really do not want to insert! How can I get rid of them?

1
  • Saying that the query is "not doing what you want" is not much of an information. Please explain what do you want query to do, provide some sample data and the expected results. Commented Jul 31, 2013 at 14:08

2 Answers 2

2

You can get rid of them by inserting a value into them. See, this statement doesn't insert a value into TargetRoom:

INSERT INTO @RoomMap
(SourceRoom, SourceSiteID)
SELECT tblControls_Rooms.ID, @origSiteID
FROM tblControls_Rooms
WHERE SiteID = @OrigSiteID

Further, the subsequent statement doesn't insert values into SourceRoom:

INSERT INTO @RoomMap
(TargetRoom, TargetSiteID)
SELECT tblControls_Rooms.ID, @NewSiteID
FROM tblControls_Rooms
WHERE SiteID = @newSiteID

So, when you're done, you get a set of rows without TargetRoom and a set of rows without SourceRoom.

Sign up to request clarification or add additional context in comments.

2 Comments

I am still not sure how to do it.
@HotlansyHttlandy, I can understand that. But from my perspective I answered the question at hand. I don't really know what you want because that's not clear from your question.
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

1 Comment

I want to the TargetRoom and SourceRoom into DemoRoomMap but I want them distinct! one source room, one targer room in the query above I am getting the following:1942 2025 1943 2025 1944 2025 1945 2025 1946 2025

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.