Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2004
    Location
    Cergy
    Posts
    65

    Question Unanswered: Variables in query, error in my code ?

    When I want to user variable in the name of the database, I have an error. What's wrong with my code ?

    DECLARE @BASE_SOURCE varchar (30),@BASE_DESTI varchar(30),@TEST varchar(30)
    set @BASE_SOURCE='BASE1'
    set @BASE_DESTI='BASE2'


    select * from @BASE_SOURCE.dbo.FOURNISS

    Msg*170, Niveau*15, État*1, Ligne*4
    Ligne 4 : syntaxe incorrecte vers '.'.

  2. #2
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    Hi

    You can't use variables for objects. You will need to use dynamic SQL (check out BoL - search for "Execute a character string" (in quotes) and\ or look at:
    http://www.sommarskog.se/dynamic_sql.html)

    HTH
    Testimonial:
    pootle flump
    ur codings are working excelent.

  3. #3
    Join Date
    Nov 2002
    Location
    Jersey
    Posts
    10,322
    DECLARE @sql varchar(8000)

    SET @sql = 'select * from '+@BASE_SOURCE+'.dbo.FOURNISS'

    EXEC(@sql)

    However, trying to be too slick in an effort to minimize code is not a good thing
    Brett
    8-)

    It's a Great Day for America everybody!

    dbforums Yak CorralRadio 'Rita
    dbForums Member List
    I'm Good Once as I ever was

    The physical order of data in a database has no meaning.

Posting Permissions

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