Simple Query

  • Hi,

    I know that this is probably very simple to do but I am having trouble.

    I'm trying to get the value from one table and insert it into a new record in another table.

    SELECT val1 AS [@val1] FROM tblTemp1 WHERE rowID = 1

    INSERT INTO tblTemp2

    (RowGUID, ID1, ID2, val1, CreationDate)

    VALUES

    (@RowGUID, @ID1, @ID2, @val1, @RequestDate)

    In the query analyzer I get the value from tblTemp1 but it does not insert it into tblTemp2 (It inserts a NULL value)

    What am I missing?

    I did not include the declarations for the @variants

    Thanks in advance

    Rick

  • You were really close...:

    
    
    SELECT @val1 = val1 FROM tblTemp1 WHERE rowID = 1

    INSERT INTO tblTemp2
    (RowGUID, ID1, ID2, val1, CreationDate)
    VALUES
    (@RowGUID, @ID1, @ID2, @val1, @RequestDate)
  • You need to assign the value

    SELECT @val1 = val1 FROM tblTemp1 WHERE rowID = 1

    Guarddata-

  • Just as an explanation, the AS clause allows you to alias a column while the equals operator assign the column's value to the indicated variable. So, in your first example, you were actually saying select var1 but call it "@var1" from a table and don't assign any value to the variable called @var1...

  • It worked great!

    Thank you!

    Rick

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply