Posts
83
Comments
600
Trackbacks
40
September 2004 Entries
Bi-Directional Updates - No Thanks

So who really wins?

 

Well I guess the answer would be no one (or the last one, or the one who bribes the dba...)

 

In any case, it was an exercise I alway contemplated, and always thought wasn't really a good idea, but always seems to come up.  I guess I should start a list of PROS and CONS for this type of operation.  Anyway the code was fun, and I hope it helps.  Not rocket science, and I betcha there could be a bunch of problems.  I tried to take in to account if the tables got out of sync or not.  Anyway, this was all spawned from this thread.

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=40203

 

Here's the code:

 

SET NOCOUNT ON
GO

USE Northwind
GO
CREATE TABLE myTable99 (
   Col1 int NOT NULL PRIMARY KEY
 , Col2 char(1)
 , ModifiedBy varchar(25) NOT NULL
 , ModifiedDate Datetime NOT NULL DEFAULT GetDate())
GO

USE Pubs
GO
CREATE TABLE myTable99 (
   Col1 int NOT NULL PRIMARY KEY
 , Col2 char(1)
 , ModifiedBy varchar(25) NOT NULL
 , ModifiedDate Datetime NOT NULL DEFAULT GetDate())
GO

CREATE TRIGGER myTrigger99 ON myTable99
FOR INSERT, UPDATE, DELETE
AS

IF EXISTS (SELECT * FROM inserted) AND NOT EXISTS (SELECT * FROM deleted) -- INSERT
  BEGIN
  -- This should never execute if the tables are in synch
      UPDATE l
  SET Col2 = r.Col2, ModifiedBy = 'TRIGGER', ModifiedDate = GetDate()
        FROM Northwind.dbo.myTable99 l
        JOIN inserted r
   ON l.Col1 = r.Col1
  AND r.ModifiedBy <> 'TRIGGER'

  -- The need for the JOIN should be not required in the tables are in synch
 INSERT INTO Northwind.dbo.myTable99(Col1, Col2, ModifiedBy)
      SELECT l.Col1, l.Col2, 'TRIGGER' FROM inserted l
   LEFT JOIN Northwind.dbo.myTable99 r
   ON l.Col1 = r.Col1
       WHERE r.Col1 IS NULL
  AND l.ModifiedBy <> 'TRIGGER'
  END

IF NOT EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted) -- DELETE
  BEGIN

 DELETE Northwind.dbo.myTable99
 WHERE Col1 IN (SELECT Col1 FROM deleted
--   WHERE ModifiedBy <> 'TRIGGER'
        )
  END

IF EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted) -- UPDATE
  BEGIN
 UPDATE l
    SET Col2 = r.Col2, ModifiedBy = 'TRIGGER', ModifiedDate = GetDate()
  FROM Northwind.dbo.myTable99 l
  JOIN inserted r
    ON l.Col1 = r.Col1
   AND r.ModifiedBy <> 'TRIGGER'
  END
GO

USE Northwind
GO

CREATE TRIGGER myTrigger99 ON myTable99
FOR INSERT, UPDATE, DELETE
AS

IF EXISTS (SELECT * FROM inserted) AND NOT EXISTS (SELECT * FROM deleted) -- INSERT
  BEGIN
 UPDATE l
    SET Col2 = r.Col2, ModifiedBy = 'TRIGGER', ModifiedDate = GetDate()
  FROM Pubs.dbo.myTable99 l
  JOIN inserted r
    ON l.Col1 = r.Col1
   AND r.ModifiedBy <> 'TRIGGER'
 
 INSERT INTO Pubs.dbo.myTable99(Col1, Col2, ModifiedBy)
      SELECT l.Col1, l.Col2, 'TRIGGER' FROM inserted l
       LEFT JOIN Pubs.dbo.myTable99 r
       ON l.Col1 = r.Col1
    WHERE r.Col1 IS NULL
             AND l.ModifiedBy <> 'TRIGGER'
  END

IF NOT EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted) -- DELETE
  BEGIN
 DELETE Pubs.dbo.myTable99
  WHERE Col1 IN (SELECT Col1 FROM deleted
--    WHERE ModifiedBy <> 'TRIGGER'
   )
  END

IF EXISTS (SELECT * FROM inserted) AND EXISTS (SELECT * FROM deleted) -- UPDATE
  BEGIN
 UPDATE l
    SET Col2 = r.Col2, ModifiedBy = 'TRIGGER', ModifiedDate = GetDate()
  FROM Pubs.dbo.myTable99 l
  JOIN inserted r
    ON l.Col1 = r.Col1
   AND r.ModifiedBy <> 'TRIGGER'
  END
GO

INSERT INTO Northwind.dbo.myTable99(Col1, Col2, ModifiedBy)
SELECT 1, 'A', 'Brett' UNION ALL
SELECT 2, 'B', 'Brett' UNION ALL
SELECT 3, 'C', 'Brett'
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

UPDATE Northwind.dbo.myTable99 SET Col2='X', ModifiedBy = 'BrettK', ModifiedDate = GetDate()
 WHERE Col1 = 2
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

INSERT INTO Pubs.dbo.myTable99(Col1, Col2, ModifiedBy)
SELECT 4, 'D', 'Brett'
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

DELETE FROM Pubs.dbo.myTable99 WHERE Col1 = 1
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

DELETE FROM Northwind.dbo.myTable99 WHERE Col1 = 4
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

UPDATE Northwind.dbo.myTable99 SET Col2='X', ModifiedBy = 'BrettX', ModifiedDate = GetDate()
 WHERE Col1 = 3
GO

SELECT * FROM Northwind.dbo.myTable99
SELECT * FROM Pubs.dbo.myTable99
GO

USE Northwind
DROP TRIGGER myTrigger99
DROP TABLE myTable99
GO
USE Pubs
DROP TRIGGER myTrigger99
DROP TABLE myTable99
GO
SET NOCOUNT OFF
GO

 

 

posted @ Tuesday, September 21, 2004 1:20 PM | Feedback (3)
The Official XML Ranting Site

XML RANT

EDIT:  1/12/2005 Hey Look!  An Official  He-Man  XML Haters Club Thread

 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=44577

Until someone can post some reason why XML in SQL Server is a good thing, I'm starting this Official XML Ranting Post.

I will start a list of why I dislike XML in SQL Server.  My Ignorance is unbounded.

"Let's take the nice relational model and add to it hierarchical "capabilities" But let's make sure the data is stored in a text column, so they can get it out in chuncks, so they can the use these "tools" to parse it out into relational format, where it can be stored, and more easily accessed....

Did I mention I hate XML“


1.  Because a Rant was requested

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=39339

2.  Because My Ignorance is showing...how do you get xml out of sql server?  Was having a conversation here ...interesting database...tried all the version of code snipets....no joy, expect to the QA result pane...what a PAIN

http://www.dbforums.com/showthread.php?p=3771308&posted=1#post3771308

USE Northwind
GO

CREATE VIEW myView99
AS
SELECT 1                    as Tag,
         NULL                 as Parent,
         Customers.CustomerID as [Customer!1!CustomerID],
         NULL                 as [Order!2!OrderID]
FROM Customers
UNION ALL
SELECT 2,
         1,
         Customers.CustomerID,
         Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID
ORDER BY [Customer!1!CustomerID], [Order!2!OrderID]
FOR XML EXPLICIT
GO

CREATE TABLE myTable99(Col1 varchar(8000))
GO

INSERT INTO myTable99(Col1)
SELECT * FROM (
SELECT 1                    as Tag,
         NULL                 as Parent,
         Customers.CustomerID as [Customer!1!CustomerID],
         NULL                 as [Order!2!OrderID]
FROM Customers
UNION ALL
SELECT 2,
         1,
         Customers.CustomerID,
         Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID
ORDER BY [Customer!1!CustomerID], [Order!2!OrderID]
FOR XML EXPLICIT) AS XXX
GO

CREATE PROC mySproc99
AS
SELECT 1                    as Tag,
         NULL                 as Parent,
         Customers.CustomerID as [Customer!1!CustomerID],
         NULL                 as [Order!2!OrderID]
FROM Customers
UNION ALL
SELECT 2,
         1,
         Customers.CustomerID,
         Orders.OrderID
FROM Customers, Orders
WHERE Customers.CustomerID = Orders.CustomerID
ORDER BY [Customer!1!CustomerID], [Order!2!OrderID]
FOR XML EXPLICIT
GO

EXEC mySproc99

INSERT INTO myTable99 EXEC mySproc99
GO

DROP TABLE myTable99
DROP PROC mySproc99
GO

 

posted @ Wednesday, September 01, 2004 5:01 PM | Feedback (40)