Solved

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

Posted on 2011-02-23
4
684 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
[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
  • 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 500 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …

738 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