"SELECT INTO" syntax

  • tblJunk has one record and looks like:

    (a,b)

    I’m trying to retrieve those values into local variables using SELECT INTO:

    select c1, c2 into :v1, :v2 from tblJunk

    When I execute it I get “Line 1: Incorrect syntax near ':'.” I saw this syntax in BOL: “SELECT INTO” > “SELECT INTO” but I can’t make it work. Help!

    TIA,

    Bill

  • Declare @i int

    @j-2 TinyInt

    Select @i=Col1,@j=Col2 from SomeTable

  • Since youre table only contains one records this will work:

    Declare @V1 <datatype for a, in table TblJunk)

    Declare @V2 <database for b, in table TblJunk>

    Select @v1 = a, @v2 = b from TblJunk

    Now if you have multiple records in TblJunk, you will then need to select each record individually to do this. Then I would use a cursor, or a very, very, selective SELECT statement (one that would return only one row). But thats another discussion.

    Gregory Larsen, DBA

    If you looking for SQL Server Examples check out my website at http://www.geocities.com/sqlserverexamples

    Gregory A. Larsen, MVP

  • The colon (:) with a variable (:v1) identifies a 'host variable'. A host variable is a C-program variable. See the Books Online, go to index and look for 'host variables' then select the 'using host variables' option.

    -Bill

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

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