Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top