2

I have 3 Databases. One of them contains wrong data.
The structure is like:

DB_1:
Tbl:
ID - otherID - Guid

DB_2:
Tbl:
ID - otherID - Guid

DB_3:
Tbl:
ID - otherID

DB_1 Containes the "otherID" that should stored in DB_3.
The "Guid" of DB_1 and BD_2
and the "otherID" of DB_2 and DB_3 match

How to update the DB_3 "oderID" with DB_1 "otherID"?

My first try was:

SELECT A.otherID
FROM [DB_1].dbo.tbl A, [DB_2].dbo.tbl B
WHERE A.Guid = B.Guid;

But then I had some trouble to use it for the update statement.

so the result was like:

UPDATE [BD_3].dbo.Tbl
SET [BD_3].dbo.Tbl.otherID =
    (
        SELECT A.otherID
        FROM [DB_1].dbo.tbl A,
             [DB_2].dbo.tbl B,
             [DB_3].dbo.tbl C
        WHERE C.otherID = B.oterID
            AND A.guid = B.guid
    );

I found something, but that has not really helped me, because I don`t know how to use it for this case.

Community
  • 1
  • 1
Wr4thon
  • 554
  • 1
  • 5
  • 11

1 Answers1

1

Try this one -

UPDATE C
SET otherID = A.otherID
FROM [BD_3].dbo.Tbl C
JOIN [DB_2].dbo.tbl B ON C.otherID = B.oterID
JOIN [DB_1].dbo.tbl A ON A.guid = B.guid

Update:

ALTER TRIGGER [dbo].[T_Akquise_ITrig] 
    ON [dbo].[Akquise] 
    FOR INSERT 
AS BEGIN 

    SET NOCOUNT ON;

    INSERT INTO dbo.LSschalter (Art, ParentId) 
    SELECT 3, i.LfdNr 
    FROM INSERTED i 

    INSERT INTO dbo.LSlisten (Art, ParentId) 
    SELECT 3, i.LfdNr 
    FROM INSERTED i 

    IF 
    (
        SELECT COUNT(1) 
        FROM INSERTED
    ) 
    != 
    (
        SELECT COUNT(1)
        FROM dbo.Adressen a
        JOIN INSERTED i ON a.AdrNr = i.AdrNr
    ) BEGIN 

        RAISERROR('Some Message', 16, 1)
        ROLLBACK TRANSACTION 

    END

END
Devart
  • 119,203
  • 23
  • 166
  • 186
  • I tryed it and got an error: Meldung 102, Ebene 15, Status 1, Prozedur T_Akquise_UTrig, Zeile 21 Incorrect syntax near '44446'. Meldung 102, Ebene 15, Status 1, Prozedur T_Akquise_UTrig, Zeile 21 Incorrect syntax near '44446'. Any Idea what I missed? – Wr4thon Aug 15 '13 at 08:53
  • UPDATE C SET ELO_Id = A.objid FROM [DataBase1].dbo.Akquise C JOIN [DataBase2].dbo.objekte B ON C.ELO_Id = B.objid JOIN [Database3].dbo.objekte A ON A.objguid = B.objguid – Wr4thon Aug 15 '13 at 09:02
  • Sorry, but I don't see any error in your query - `Command(s) completed successfully.` – Devart Aug 15 '13 at 09:04
  • Me either. But the SQL-Server says there is an Error. Thank You Anyway. I will try to find the error and post it when I got it. – Wr4thon Aug 15 '13 at 09:09
  • I got the error. It was the Update-Trigger of the Table. there is an error somewhere in there and so the SQL-Server says Syntax Error. But not in the querry, in the Trigger. – Wr4thon Aug 15 '13 at 14:00
  • Can you provide a trigger body with `DDL` of your tables. – Devart Aug 15 '13 at 15:42
  • sure. Sorry for taking so long to reply. The function is: USE [tblName] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER TRIGGER [dbo].[T_Akquise_ITrig] ON [dbo].[Akquise] FOR INSERT AS SET NOCOUNT ON INSERT INTO LSschalter ( Art, ParentId ) SELECT 3 AS Art, inserted.LfdNr FROM inserted INSERT INTO LSlisten ( Art, ParentId ) SELECT 3 AS Art, inserted.LfdNr FROM inserted IF (SELECT COUNT(*) FROM inserted) != (SELECT COUNT(*) FROM Adressen, inserted WHERE (Adressen.AdrNr = inserted.AdrNr)) BEGIN RAISERROR 44447 'Some Message''.' ROLLBACK TRANSACTION END – Wr4thon Aug 21 '13 at 12:52
  • The comment field was to short so I post my solution below. The systems is based on sql server 2008 (r2). My testing system uses SQL server 2012 express. Since is use an VM with an 2008 r2 express sql server there is no error.
    the line RAISERROR 44447 'Some Message.' contains the error. the number is underlined
    – Wr4thon Aug 21 '13 at 12:52
  • You are using the old-style for `RAISERROR` (old-style is removed in 2012 version). Just try the updated answer. – Devart Aug 21 '13 at 13:40