Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Passing Database as Variable in Update Statement

Posted on 2011-02-22
7
Medium Priority
?
389 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 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
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.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

721 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