OPENROWSET - SYNTEX ERROR NEAR @CMD

  • -- this works fine

    SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=sql01;Trusted_Connection=yes;', 'SELECT * from syte_APP.DBO.employee') AS a

    -- this code gives me error

    DECLARE @Cmd VarChar(4000)

    SET @CMD = 'SELECT * from syte_APP.DBO.employee'

    SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=sql01;Trusted_Connection=yes;', @cmd ) AS a

    -- Msg 102, Level 15, State 1, Line 4

    -- Incorrect syntax near '@cmd'

  • skb 44459 (3/21/2013)


    -- this works fine

    SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=sql01;Trusted_Connection=yes;', 'SELECT * from syte_APP.DBO.employee') AS a

    -- this code gives me error

    DECLARE @Cmd VarChar(4000)

    SET @CMD = 'SELECT * from syte_APP.DBO.employee'

    SELECT a.* FROM OPENROWSET('SQLNCLI', 'Server=sql01;Trusted_Connection=yes;', @cmd ) AS a

    -- Msg 102, Level 15, State 1, Line 4

    -- Incorrect syntax near '@cmd'

    If you read BoL you will find out that you can only use an object name or a string constant there. You cannot pass a variable into OPENROWSET...

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 2 posts - 1 through 1 (of 1 total)

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