Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Passing Database as Variable in Update Statement

Posted on 2011-02-22
7
Medium Priority
?
394 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
  • 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 2000 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
Industry Leaders: 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!

 

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

926 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