Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2004
    Location
    Melbourne
    Posts
    144

    Unanswered: How do i use a variable in a select query

    How do i use a variable in a select query
    ---------------
    Declare @host varchar(20)
    set @host = '"Fab"'

    Declare @DB_name varchar(25)
    set @DB_name = 'config_mpl_18May2005'

    Declare @Full_Path varchar(60)
    set @Full_Path = @host + '.' + @DB_name + '.dbo.cfg_dn'
    Print '@Full_Path = ' + @Full_Path

    ------ cfg_dn ----------

    Print 'Starting on CFG DN'

    select * from @Full_Path

    where dbid = 5441 or dbid = 5389 or dbid = 562


    Error:

    Server: Msg 137, Level 15, State 2, Line 16

    Must declare the variable '@Full_Path'.

  2. #2
    Join Date
    Jun 2003
    Posts
    269

    Thumbs up Re

    --Use dynamic query,try this,

    Declare @host varchar(20)
    declare @query nvarchar(4000)
    set @host = '"Fab"'

    Declare @DB_name varchar(25)
    set @DB_name = 'config_mpl_18May2005'

    Declare @Full_Path varchar(60)
    set @Full_Path = @host + '.' + @DB_name + '.dbo.cfg_dn'
    Print '@Full_Path = ' + @Full_Path

    Print 'Starting on CFG DN'

    set @query='select * from ' +@Full_Path+' where dbid = 5441 or dbid = 5389 or dbid = 562'
    exec (@query)

    ----cheers,
    ----http://mallier.blogspot.com

Posting Permissions

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