Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. T-SQL USE with a variable

T-SQL USE with a variable

Scheduled Pinned Locked Moved Database
databasetoolsquestion
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TheComputerMan
    wrote on last edited by
    #1

    DECLARE @ventyxdbname varchar(50) SET @ventyxdbname = 'OUTLOG_PACE' /* */ USE @ventyxdbname; This gives: Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '@ventyxdbname'. Is it not possible to use a variable in a script for the USE command?

    B 1 Reply Last reply
    0
    • T TheComputerMan

      DECLARE @ventyxdbname varchar(50) SET @ventyxdbname = 'OUTLOG_PACE' /* */ USE @ventyxdbname; This gives: Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '@ventyxdbname'. Is it not possible to use a variable in a script for the USE command?

      B Offline
      B Offline
      Blue_Boy
      wrote on last edited by
      #2

      use nvarchar datatype instead of varchar


      I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

      T 1 Reply Last reply
      0
      • B Blue_Boy

        use nvarchar datatype instead of varchar


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        T Offline
        T Offline
        TheComputerMan
        wrote on last edited by
        #3

        Hi, Changed it to nvarchar, but still the same message. Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '@ventyxdbname'. It definitely does not seem to like a variable with USE. Could it be that tis is like the CREATE TABLE command where you cannot use a variable directly?

        B 1 Reply Last reply
        0
        • T TheComputerMan

          Hi, Changed it to nvarchar, but still the same message. Msg 102, Level 15, State 1, Line 4 Incorrect syntax near '@ventyxdbname'. It definitely does not seem to like a variable with USE. Could it be that tis is like the CREATE TABLE command where you cannot use a variable directly?

          B Offline
          B Offline
          Blue_Boy
          wrote on last edited by
          #4

          declare @DBName varchar(20)
          declare @Str varchar(100)

          set @DBName = databasename
          set @Str = 'use ' + @DBName
          exec (@Str)


          I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

          T 1 Reply Last reply
          0
          • B Blue_Boy

            declare @DBName varchar(20)
            declare @Str varchar(100)

            set @DBName = databasename
            set @Str = 'use ' + @DBName
            exec (@Str)


            I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

            T Offline
            T Offline
            TheComputerMan
            wrote on last edited by
            #5

            Many thanks for trying! I am afraid that does not work either.

            declare @DBName varchar(20)
            declare @Str varchar(100)

            set @DBName = 'OUTLOG_PACE'
            set @Str = 'use ' + @DBName
            exec (@Str)

            select * from dbo.OUT_tbl_Request

            -------------- Error message: Msg 208, Level 16, State 1, Line 8 Invalid object name 'dbo.OUT_tbl_Request'. The table name is correct, but this is the result if I start the query in master. Not to worry, there are only 4 places in the script where the database name needs to be changed. I was just trying to be lazy as it is quite a long script and I did not want to miss one. I will use Ctrl+H !!

            D 1 Reply Last reply
            0
            • T TheComputerMan

              Many thanks for trying! I am afraid that does not work either.

              declare @DBName varchar(20)
              declare @Str varchar(100)

              set @DBName = 'OUTLOG_PACE'
              set @Str = 'use ' + @DBName
              exec (@Str)

              select * from dbo.OUT_tbl_Request

              -------------- Error message: Msg 208, Level 16, State 1, Line 8 Invalid object name 'dbo.OUT_tbl_Request'. The table name is correct, but this is the result if I start the query in master. Not to worry, there are only 4 places in the script where the database name needs to be changed. I was just trying to be lazy as it is quite a long script and I did not want to miss one. I will use Ctrl+H !!

              D Offline
              D Offline
              DoctorMick
              wrote on last edited by
              #6

              What happens if you put a go statement after the exec? For some reason I remember something similar causing me problems and adding a go statement fixed it. Could be completely wrong as I don't have SQL Server in front of me.

              T 1 Reply Last reply
              0
              • D DoctorMick

                What happens if you put a go statement after the exec? For some reason I remember something similar causing me problems and adding a go statement fixed it. Could be completely wrong as I don't have SQL Server in front of me.

                T Offline
                T Offline
                TheComputerMan
                wrote on last edited by
                #7

                I think I have found the answer - it don't work!!! :( From Microsoft Using EXECUTE with a Character String In earlier versions of SQL Server, character strings are limited to 8,000 bytes. This requires concatenating large strings for dynamic execution. In SQL Server 2005, the varchar(max) and nvarchar(max) data types can be specified that allow for character strings to be up to 2 gigabytes of data. Changes in database context last only until the end of the EXECUTE statement. For example, after the EXEC in this following statement is run, the database context is master. USE master; EXEC ('USE AdventureWorks; SELECT EmployeeID, Title FROM HumanResources.Employee;'); Thanks very much for trying anyway! :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups