I can assign database name from the variable in T-SQL MS SQL 2005 ?

Is there any way I can assign database name from the variable in T-SQL ?
What other alernative ?
My code look like this
SET @dbRS = 'RS' + LTRIM(RTRIM(@StoreID)) --Database Name

if NOT EXISTS(SELECT * FROM sys.databases WHERE NAME = @dbRS)
      EXEC sp_attach_db @dbname = @dbRS,
      @filename1 = @dbFilePath,
      @filename2 = @dbLogFilePath
      --HERE @dbRS variable contain the attached database name
      -- I Want to run this in a loop and update all the databases finds in the table
                     -- Which ofcourse have the same table structures
                    Update  @dbRS..Customer
      Set @dbRS..Customer.CustomerName = btr..Customer.CustomerName
      FROM @dbRS.Customer JOIN BTR..Customer
      ON @dbRS..Customer.CustomerID= btr..Customer.CustomerID
kghiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
only with dynamic sql:
SET @dbRS = 'RS' + LTRIM(RTRIM(@StoreID)) --Database Name 
if NOT EXISTS(SELECT * FROM sys.databases WHERE NAME = @dbRS)
      EXEC sp_attach_db @dbname = @dbRS, 
      @filename1 = @dbFilePath, 
      @filename2 = @dbLogFilePath 
 DECLARE @sql VARCHAR(MAX)
 SET @SQL = '
     Update c
      Set CustomerName = b.CustomerName
      FROM [' + @dbRS + '] Customer c
      JOIN BTR..Customer b
      ON c.CustomerID= b.CustomerID
  '
  EXEC(@SQL)
 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kghiAuthor Commented:
Thanks angellll !
It worked!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.