Solved

Passing Database as Variable in Update Statement

Posted on 2011-02-22
7
384 Views
Last Modified: 2012-05-11
DECLARE @Databasename NameType
DECLARE @CurrentDB NameType

SELECT @Databasename = 'Dbase'
SELECT @CurrentDB = db_name()

UPDATE l_a
SET l_a.mrb_flag = l.mrb_flag
FROM [@CurrentDB].dbo.location_all l_a
INNER JOIN [@Databasename].dbo.location_all l ON l.loc = l_a.loc
Where l_a.loc ='Test1_sa'

Error : - Invalid object name '@CurrentDB.dbo.location_all'.
0
Comment
Question by:SaiLoveAll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 8

Expert Comment

by:rushShah
ID: 34953872
try this
DECLARE @Databasename NameType
DECLARE @CurrentDB NameType

SELECT @Databasename = 'Dbase'
SELECT @CurrentDB = db_name()

DECLARE @SQL VARCHAR(MAX)

SET @SQL = 'UPDATE l_a
SET l_a.mrb_flag = l.mrb_flag
FROM [' + @CurrentDB + '].dbo.location_all l_a
INNER JOIN [@Databasename].dbo.location_all l ON l.loc = l_a.loc
Where l_a.loc =''Test1_sa'''

EXEC (@SQL)

Open in new window

0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34953926

You can not use a variable as a database name or table name in a from clause or join, you have to use dynamic sql in such a case
DECLARE @Databasename NameType
DECLARE @CurrentDB NameType
DECLARE @SQL varchar(max)

SELECT @Databasename = 'Dbase'
SELECT @CurrentDB = db_name()

set @SQL = 'UPDATE l_a '+ 
           'SET l_a.mrb_flag = l.mrb_flag ' +
           'FROM [' + @CurrentDB + '].dbo.location_all l_a ' +
           'INNER JOIN [' + @Databasename + '].dbo.location_all l ON l.loc = l_a.loc' +
           'Where l_a.loc = ''' + 'Test1_sa'''
EXEC(@SQL)

Open in new window

0
 
LVL 32

Accepted Solution

by:
Ephraim Wangoya earned 500 total points
ID: 34953947
need space after 1_a.loc
DECLARE @Databasename NameType
DECLARE @CurrentDB NameType
DECLARE @SQL varchar(max)

SELECT @Databasename = 'Dbase'
SELECT @CurrentDB = db_name()

set @SQL = 'UPDATE l_a '+ 
           'SET l_a.mrb_flag = l.mrb_flag ' +
           'FROM [' + @CurrentDB + '].dbo.location_all l_a ' +
           'INNER JOIN [' + @Databasename + '].dbo.location_all l ON l.loc = l_a.loc ' +
           'Where l_a.loc = ''' + 'Test1_sa'''
EXEC(@SQL)

Open in new window

0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

Author Closing Comment

by:SaiLoveAll
ID: 34954587
Thanks a Lot. It worked
0
 

Author Comment

by:SaiLoveAll
ID: 34954835
Hi -

I was trying to do the same in a trigger after Insert. Where I am gettting Incorrect Syntax near i.

Can you figure out what is wrong at i ?

set @SQL ='UPDATE l_a '+
           'SET l_a.mrb_flag = l.mrb_flag ' +
           'FROM [' + @CurrentDB + '].dbo.location_all l_a ' +
           'INNER JOIN [' + @Databasename + '].dbo.location_all l ON l.RowPointer = l_a.RowPointer ' +
           'INNER JOIN inserted i ON l_a.RowPointer = i.RowPointer'+
               'WHERE i.site_ref = l.site_ref and i.loc = l.loc'
EXEC(@SQL)
0
 
LVL 32

Expert Comment

by:Ephraim Wangoya
ID: 34954892

need a space after rowpointer

set @SQL ='UPDATE l_a '+
           'SET l_a.mrb_flag = l.mrb_flag ' +
           'FROM [' + @CurrentDB + '].dbo.location_all l_a ' +
           'INNER JOIN [' + @Databasename + '].dbo.location_all l ON l.RowPointer = l_a.RowPointer ' +
           'INNER JOIN inserted i ON l_a.RowPointer = i.RowPointer  '+
           'WHERE i.site_ref = l.site_ref and i.loc = l.loc'
EXEC(@SQL)
0
 

Author Comment

by:SaiLoveAll
ID: 34955571
Now, It says :-

Invalid Object Name inserted.

But How can I Join without using Inserted , to get the Inserted row pulled in a After Insert Trigger.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question