Results 1 to 3 of 3
  1. #1
    Join Date
    Sep 2003
    Location
    Brazil
    Posts
    3

    Unanswered: Declare dynamicly variable

    I'd like to know, if there is a possibility reference declare SQLServer dynamically?

    For example, I've tried execute this query(under), but I received this message (Server: Msg 137, Level 15, State 1, Line 6
    Must declare the variable '@t1'.):

    declare @Frase varchar(50)
    set @Frase = 'declare @t1 varchar(10), @t2 int'
    exec (@Frase)
    select @t1 = 'AAAAA'
    select @t2 = 1000
    Print @t1
    Print @t2

    Thanks

  2. #2
    Join Date
    Sep 2003
    Location
    KY
    Posts
    146

    Probably

    May be because Dynamic SQL is executed/Parsed last by the query Parser and the variable declared are within the statement which is limited to the "exec" and is considered a seperate stored procedure outside of main query

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    The EXEC statement executes within its own scope, outside of the procedure that calls it. Therefore, EXEC cannot share variables with its calling procedure. As soon as EXEC completes, the variables go out of scope and "poof", they disappear. Temporary tables, however, are connection specific and can be referenced within EXEC statements.

    blindman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •