Use db script

  • Hi Gurus,

    I have written a small piece of code to get the database name in QA whenever I execute it. Now I specified northwind, so in QA it shude becum northwind. The code executes successfully, but the database does not change to the database mentioned.

    declare @DB nvarchar(10)

    declare @DB1 nvarchar(20)

    set @DB = 'northwind'

    set @db1 = 'use ' + @Db

    print @db1

    exec (@db1)

    Any ideas ?

    --Kishore

     

  • Without a [GO] you would need to include any additional commands in @db1 seperated by [;]. It's context did switch but returns to your current db after execution.

  • I dont understand your solution can u send a modified code with what u are trying to suggest us.

  • use pubs

    go

    select top 10 * from northwind..customers

    declare @DB nvarchar(10)

    declare @DB1 nvarchar(2000) -- INCREASED SIZE

    set @DB = N'northwind'

    set @db1 = N'use ' + rtrim(@Db)+';  update customers set ContactName = rtrim(ContactName)+'' XXX'' where CustomerID = ''ALFKI'''  -- anything you want to do here

    -- otherwise you'll have to switch to the correct db in your script prior to going dynamic

    -- the [GO] will drop any previously defined variables

    -- and those are multiple single quotes not double quotes 

    print @db1

    exec (@db1)

    select top 10 * from northwind..customers

Viewing 4 posts - 1 through 3 (of 3 total)

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