2010年6月2日 星期三

SQL T-SQL 觸發即時同步Table

有時候會用到Table要一模一樣 但是卻要寫兩次
這樣又很麻煩
所以弄了個T-SQL指令 可以同步讓兩個Table同步化
只要在觸發增加下面指令就行了
當然裡面表格跟寫法要自己設定一下
新增修改刪除下面都有 ^^


CREATE TRIGGER [dbo].[datacopy]
ON [dbo].[data]
AFTER INSERT
AS

BEGIN
--SET @name = SELECT a FROM inserted
DECLARE @n01 nvarchar(50),
@n02 nvarchar(50),
@n03 nvarchar(50),
@n04 nvarchar(50),
@n05 nvarchar(50),
@n06 nvarchar(50),
@n07 nvarchar(50),
@n08 nvarchar(50),
@n09 nvarchar(50),
@n10 nvarchar(50),
@n11 nvarchar(50),
@n12 nvarchar(50),
@n13 nvarchar(50),
@n14 nvarchar(50),
@n15 nvarchar(50),
@n16 nvarchar(50),
@n17 nvarchar(50),
@n18 nvarchar(50),
@n19 nvarchar(50),
@n20 nvarchar(50),
@n21 nvarchar(50),
@n22 nvarchar(50),
@n23 nvarchar(50),
@n24 nvarchar(50),
@n25 nvarchar(50),
@n26 nvarchar(50),
@n27 nvarchar(50),
@n28 nvarchar(50),
@n29 nvarchar(50),
@n30 nvarchar(50),
@n31 nvarchar(50),
@n32 nvarchar(50),
@n33 nvarchar(50),
@n34 nvarchar(50),
@n35 nvarchar(50),
@n36 nvarchar(50),
@n37 nvarchar(50),
@n38 nvarchar(50),
@n39 nvarchar(50),
@n40 nvarchar(50),
@n41 nvarchar(50),
@n42 nvarchar(50),
@n43 nvarchar(50),
@n44 nvarchar(50),
@n45 nvarchar(50),
@n46 nvarchar(50),
@n47 nvarchar(50),
@n48 nvarchar(50),
@n49 nvarchar(50),
@n50 nvarchar(50)
select @n01 = i.[n01],
@n02 = i.[n02],
@n03 = i.[n03],
@n04 = i.[n04],
@n05 = i.[n05],
@n06 = i.[n06],
@n07 = i.[n07],
@n08 = i.[n08],
@n09 = i.[n09],
@n10 = i.[n10],
@n11 = i.[n11],
@n12 = i.[n12],
@n13 = i.[n13],
@n14 = i.[n14],
@n15 = i.[n15],
@n16 = i.[n16],
@n17 = i.[n17],
@n18 = i.[n18],
@n19 = i.[n19],
@n20 = i.[n20],
@n21 = i.[n21],
@n22 = i.[n22],
@n23 = i.[n23],
@n24 = i.[n24],
@n25 = i.[n25],
@n26 = i.[n26],
@n27 = i.[n27],
@n28 = i.[n28],
@n29 = i.[n29],
@n30 = i.[n30],
@n31 = i.[n31],
@n32 = i.[n32],
@n33 = i.[n33],
@n34 = i.[n34],
@n35 = i.[n35],
@n36 = i.[n36],
@n37 = i.[n37],
@n38 = i.[n38],
@n39 = i.[n39],
@n40 = i.[n40],
@n41 = i.[n41],
@n42 = i.[n42],
@n43 = i.[n43],
@n44 = i.[n44],
@n45 = i.[n45],
@n46 = i.[n46],
@n47 = i.[n47],
@n48 = i.[n48],
@n49 = i.[n49],
@n50 = i.[n50]

from inserted i
--SELECT a FROM inserted

INSERT INTO [test2].[dbo].[data3]([n01],[n02],[n03],[n04],[n05],[n06],[n07],[n08],[n09],[n10],[n11],[n12],[n13],[n14],[n15],[n16],[n17],[n18],[n19],[n20],[n21],[n22],[n23],[n24],[n25],[n26],[n27],[n28],[n29],[n30],[n31],[n32],[n33],[n34],[n35],[n36],[n37],[n38],[n39],[n40],[n41],[n42],[n43],[n44],[n45],[n46],[n47],[n48],[n49],[n50]) VALUES(@n01,@n02,@n03,@n04,@n05,@n06,@n07,@n08,@n09,@n10,@n11,@n12,@n13,@n14,@n15,@n16,@n17,@n18,@n19,@n20,@n21,@n22,@n23,@n24,@n25,@n26,@n27,@n28,@n29,@n30,@n31,@n32,@n33,@n34,@n35,@n36,@n37,@n38,@n39,@n40,@n41,@n42,@n43,@n44,@n45,@n46,@n47,@n48,@n49,@n50)
END


XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXxx
CREATE TRIGGER [dbo].[datacopydel]
ON [dbo].[data]
AFTER DELETE
AS

BEGIN
--SET @name = SELECT a FROM inserted
DECLARE @n01 nvarchar(50),
@n02 nvarchar(50),
@n03 nvarchar(50),
@n04 nvarchar(50),
@n05 nvarchar(50),
@n06 nvarchar(50),
@n07 nvarchar(50),
@n08 nvarchar(50),
@n09 nvarchar(50),
@n10 nvarchar(50),
@n11 nvarchar(50),
@n12 nvarchar(50),
@n13 nvarchar(50),
@n14 nvarchar(50),
@n15 nvarchar(50),
@n16 nvarchar(50),
@n17 nvarchar(50),
@n18 nvarchar(50),
@n19 nvarchar(50),
@n20 nvarchar(50),
@n21 nvarchar(50),
@n22 nvarchar(50),
@n23 nvarchar(50),
@n24 nvarchar(50),
@n25 nvarchar(50),
@n26 nvarchar(50),
@n27 nvarchar(50),
@n28 nvarchar(50),
@n29 nvarchar(50),
@n30 nvarchar(50),
@n31 nvarchar(50),
@n32 nvarchar(50),
@n33 nvarchar(50),
@n34 nvarchar(50),
@n35 nvarchar(50),
@n36 nvarchar(50),
@n37 nvarchar(50),
@n38 nvarchar(50),
@n39 nvarchar(50),
@n40 nvarchar(50),
@n41 nvarchar(50),
@n42 nvarchar(50),
@n43 nvarchar(50),
@n44 nvarchar(50),
@n45 nvarchar(50),
@n46 nvarchar(50),
@n47 nvarchar(50),
@n48 nvarchar(50),
@n49 nvarchar(50),
@n50 nvarchar(50)
select @n01 = d.[n01],
@n02 = d.[n02],
@n03 = d.[n03],
@n04 = d.[n04],
@n05 = d.[n05],
@n06 = d.[n06],
@n07 = d.[n07],
@n08 = d.[n08],
@n09 = d.[n09],
@n10 = d.[n10],
@n11 = d.[n11],
@n12 = d.[n12],
@n13 = d.[n13],
@n14 = d.[n14],
@n15 = d.[n15],
@n16 = d.[n16],
@n17 = d.[n17],
@n18 = d.[n18],
@n19 = d.[n19],
@n20 = d.[n20],
@n21 = d.[n21],
@n22 = d.[n22],
@n23 = d.[n23],
@n24 = d.[n24],
@n25 = d.[n25],
@n26 = d.[n26],
@n27 = d.[n27],
@n28 = d.[n28],
@n29 = d.[n29],
@n30 = d.[n30],
@n31 = d.[n31],
@n32 = d.[n32],
@n33 = d.[n33],
@n34 = d.[n34],
@n35 = d.[n35],
@n36 = d.[n36],
@n37 = d.[n37],
@n38 = d.[n38],
@n39 = d.[n39],
@n40 = d.[n40],
@n41 = d.[n41],
@n42 = d.[n42],
@n43 = d.[n43],
@n44 = d.[n44],
@n45 = d.[n45],
@n46 = d.[n46],
@n47 = d.[n47],
@n48 = d.[n48],
@n49 = d.[n49],
@n50 = d.[n50]

from deleted d
--SELECT a FROM inserted
DELETE [test2].[dbo].[data3] WHERE [n01] = @n01
END



XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXx
CREATE TRIGGER [dbo].[datacopyupdate]
ON [dbo].[data]
AFTER UPDATE
AS

BEGIN
--SET @name = SELECT a FROM inserted
DECLARE @n01 nvarchar(50),
@n02 nvarchar(50),
@n03 nvarchar(50),
@n04 nvarchar(50),
@n05 nvarchar(50),
@n06 nvarchar(50),
@n07 nvarchar(50),
@n08 nvarchar(50),
@n09 nvarchar(50),
@n10 nvarchar(50),
@n11 nvarchar(50),
@n12 nvarchar(50),
@n13 nvarchar(50),
@n14 nvarchar(50),
@n15 nvarchar(50),
@n16 nvarchar(50),
@n17 nvarchar(50),
@n18 nvarchar(50),
@n19 nvarchar(50),
@n20 nvarchar(50),
@n21 nvarchar(50),
@n22 nvarchar(50),
@n23 nvarchar(50),
@n24 nvarchar(50),
@n25 nvarchar(50),
@n26 nvarchar(50),
@n27 nvarchar(50),
@n28 nvarchar(50),
@n29 nvarchar(50),
@n30 nvarchar(50),
@n31 nvarchar(50),
@n32 nvarchar(50),
@n33 nvarchar(50),
@n34 nvarchar(50),
@n35 nvarchar(50),
@n36 nvarchar(50),
@n37 nvarchar(50),
@n38 nvarchar(50),
@n39 nvarchar(50),
@n40 nvarchar(50),
@n41 nvarchar(50),
@n42 nvarchar(50),
@n43 nvarchar(50),
@n44 nvarchar(50),
@n45 nvarchar(50),
@n46 nvarchar(50),
@n47 nvarchar(50),
@n48 nvarchar(50),
@n49 nvarchar(50),
@n50 nvarchar(50)
select @n01 = i.[n01],
@n02 = i.[n02],
@n03 = i.[n03],
@n04 = i.[n04],
@n05 = i.[n05],
@n06 = i.[n06],
@n07 = i.[n07],
@n08 = i.[n08],
@n09 = i.[n09],
@n10 = i.[n10],
@n11 = i.[n11],
@n12 = i.[n12],
@n13 = i.[n13],
@n14 = i.[n14],
@n15 = i.[n15],
@n16 = i.[n16],
@n17 = i.[n17],
@n18 = i.[n18],
@n19 = i.[n19],
@n20 = i.[n20],
@n21 = i.[n21],
@n22 = i.[n22],
@n23 = i.[n23],
@n24 = i.[n24],
@n25 = i.[n25],
@n26 = i.[n26],
@n27 = i.[n27],
@n28 = i.[n28],
@n29 = i.[n29],
@n30 = i.[n30],
@n31 = i.[n31],
@n32 = i.[n32],
@n33 = i.[n33],
@n34 = i.[n34],
@n35 = i.[n35],
@n36 = i.[n36],
@n37 = i.[n37],
@n38 = i.[n38],
@n39 = i.[n39],
@n40 = i.[n40],
@n41 = i.[n41],
@n42 = i.[n42],
@n43 = i.[n43],
@n44 = i.[n44],
@n45 = i.[n45],
@n46 = i.[n46],
@n47 = i.[n47],
@n48 = i.[n48],
@n49 = i.[n49],
@n50 = i.[n50]

from inserted i
--SELECT a FROM inserted
UPDATE [test2].[dbo].[data3] SET [n02] = @n02,[n03] = @n03,[n04] = @n04,[n05] = @n05,[n06] = @n06,[n07] = @n07,[n08] = @n08,[n09] = @n09,[n10] = @n10,[n11] = @n11,[n12] = @n12,[n13] = @n13,[n14] = @n14,[n15] = @n15,[n16] = @n16,[n17] = @n17,[n18] = @n18,[n19] = @n19,[n20] = @n20,[n21] = @n21,[n22] = @n22,[n23] = @n23,[n24] = @n24,[n25] = @n25,[n26] = @n26,[n27] = @n27,[n28] = @n28,[n29] = @n29,[n30] = @n30,[n31] = @n31,[n32] = @n32,[n33] = @n33,[n34] = @n34,[n35] = @n35,[n36] = @n36,[n37] = @n37,[n38] = @n38,[n39] = @n39,[n40] = @n40,[n41] = @n41,[n42] = @n42,[n43] = @n43,[n44] = @n44,[n45] = @n45,[n46] = @n46,[n47] = @n47,[n48] = @n48,[n49] = @n49,[n50] = @n50 WHERE [n01] = @n01
END


[N01_OBSYSTEM].[dbo].[TB_Product_D]

沒有留言:

張貼留言