[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Update a column in one database from another (Dynamic) database

Posted on 2011-02-23
4
Medium Priority
?
694 Views
Last Modified: 2012-05-11
A row gets inserted into 2 DB tables at same time, but one of the coulmn remains null on one DB and gets value on another. No my task is :-  
I have to update a column in one database from another database After Insert,
to get the Inserted row pulled and update the record I am using a After Insert Trigger.

ALTER TRIGGER [Update_Trg]
   ON [dbo].[location_all]
   AFTER INSERT
AS
BEGIN

DECLARE @Databasename NameType
DECLARE @CurrentDB NameType
DECLARE @SQL varchar(max)

SELECT TOP 1
@site_ref = i.site_ref
, @loc = i.loc  
, @RowPointer =i.RowPointer    
FROM INSERTED i

SELECT @Databasename = Databasename
FROM dbo._Site_DatabaseNames
Where Site = @site_ref

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.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)

Invalid Object Name inserted.

What is wrong in this script ? Can anybody help me find a solution. Thanks.
0
Comment
Question by:SaiLoveAll
  • 2
  • 2
4 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34963599
inside the dynamic sql, INSERTED is no longer valid, unfortunately.
so, you need to refer to the real table instead, using the ID value you need to fetch from inserted and add such a condition into the @sql explicitely

something
ALTER TRIGGER [Update_Trg]
   ON [dbo].[location_all] 
   AFTER INSERT
AS 
BEGIN

DECLARE @Databasename NameType
DECLARE @CurrentDB NameType
DECLARE @SQL varchar(max)

SELECT TOP 1 
@site_ref = i.site_ref
, @loc = i.loc   
, @RowPointer =i.RowPointer    
FROM INSERTED i 

SELECT @Databasename = Databasename
FROM dbo._Site_DatabaseNames
Where Site = @site_ref

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.RowPointer = l_a.RowPointer  
             and l.row_pointer = ''' + @RowPointer  + ''' '
EXEC(@SQL)

Open in new window

0
 

Author Comment

by:SaiLoveAll
ID: 34963989
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  
             and l.row_pointer = ''' + @RowPointer  + ''' '

When I do so, I get an ERROR Message :-
The data types nvarchar and uniqueidentifier are incompatible in the add operator.

The variable '@RowPointer ' is a uniqueidentifier.  What could have caused the incomplatability ?

In trigger
DECLARE @RowPointer RowPointerType  

Table column
[RowPointer] [dbo].[RowPointerType] NOT NULL CONSTRAINT [DF_location_all_RowPointer]  DEFAULT (newid())
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 34964794
change:

  and l.row_pointer = ''' + @RowPointer  + ''' '

into:

  and l.row_pointer = ''' +cast( @RowPointer as varchar(100))  + ''' '
0
 

Author Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

834 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