将mySQL触发器转换为TSQL

时间:2017-10-17 05:25:48

标签: mysql sql-server tsql

寻找将mySQL触发器转换为TSQL的帮助。

CREATE TRIGGER IF not exists TF_WATCHLIST_PERSISTENCE_ADD_POSITION 
AFTER INSERT ON TF_WATCHLIST_PERSISTENCE
FOR EACH ROW
WHEN NEW.WATCHLIST_POSITION IS NULL
BEGIN 
    UPDATE TF_WATCHLIST_PERSISTENCE SET
    WATCHLIST_POSITION = (SELECT COUNT(*) - 1 FROM TF_WATCHLIST_PERSISTENCE WHERE WATCHLIST_USER = NEW.WATCHLIST_USER)
    WHERE rowid = NEW.rowid;
END;

3 个答案:

答案 0 :(得分:1)

感谢您的帮助

我最终得到了这个。

CREATE TRIGGER TF_WATCHLIST_PERSISTENCE_ADD_POSITION 
ON TF_WATCHLIST_PERSISTENCE
AFTER INSERT, UPDATE
AS
DECLARE @MyCursor CURSOR;
DECLARE @MyField_USER varchar(250);
DECLARE @MyField_ID varchar(250);
DECLARE @MyField_NewPos int

BEGIN 
SET @MyCursor = CURSOR FOR 
SELECT      WATCHLIST_USER
        ,   WATCHLIST_ID
FROM    inserted
WHERE inserted.WATCHLIST_POSITION is null

OPEN @MyCursor 
FETCH NEXT FROM @MyCursor
INTO @MyField_USER,  @MyField_ID

WHILE @@FETCH_STATUS = 0

BEGIN 
    set @MyField_NewPos = (SELECT MAX(WATCHLIST_POSITION) + 1 FROM TF_WATCHLIST_PERSISTENCE WHERE WATCHLIST_USER = @MyField_USER)
    print @MyField_NewPos

    update TF_WATCHLIST_PERSISTENCE 
        SET WATCHLIST_POSITION = ISNULL(@MyField_NewPos,0)
        WHERE       WATCHLIST_USER = @MyField_USER 
            AND     WATCHLIST_ID = @MyField_ID
            AND     WATCHLIST_POSITION IS NULL 

        Print @MyField_USER 
        Print @MyField_ID
        FETCH NEXT FROM @MyCursor
        INTO @MyField_USER,  @MyField_ID
END;


END;
CLOSE @MyCursor

答案 1 :(得分:0)

试试这个:

CREATE TRIGGER TF_WATCHLIST_PERSISTENCE_ADD_POSITION ON TF_WATCHLIST_PERSISTENCE  AFTER INSERT 
AS
BEGIN 
    WITH DataSource AS
    (
        SELECT DS.WATCHLIST_USER
              ,COUNT(*) - 1 AS WATCHLIST_POSITION
        FROM TF_WATCHLIST_PERSISTENCE DS
        INNER JOIN inserted I
            ON DS.WATCHLIST_USER = I.WATCHLIST_USER
        WHERE inserted.WATCHLIST_POSITION is null
        GROUP BY DS.WATCHLIST_USER
    )
    UPDATE TF_WATCHLIST_PERSISTENCE
    SET WATCHLIST_POSITION = DS.[WATCHLIST_POSITION]
    FROM TF_WATCHLIST_PERSISTENCE DS
    INNER JOIN DataSource I
        ON DS.WATCHLIST_USER = I.WATCHLIST_USER;
END

答案 2 :(得分:0)

我怀疑您正在寻找排名功能,甚至每位用户只需要一个简单的COUNT(*)? MySQL在analyticranking或窗口函数中相当差,因此使用古怪的更新或触发器等技巧来模拟它们。

在SQL Server中,您可以返回与每个行中匹配单个用户的记录数,并在与单个用户匹配的记录上应用COUNT()

SELECT *, COUNT(*) OVER (PARTITION BY WATCHLIST_USER)
FROM TF_WATCHLIST_PERSISTENCE 

无需触发器。您可以将任何聚合与OVER()子句一起使用。

如果您希望 rank 按照某种顺序,您可以使用其中一个排名函数,如ROW_NUMBER()或`RANK()。如果您想基于每个用户计算行位置,例如在创建日期,您可以写:

SELECT *, ROW_NUMBER(*) OVER (PARTITION BY WATCHLIST_USER ORDER BY CreationDate)
FROM TF_WATCHLIST_PERSISTENCE 

如果您有递增ID,则可以使用它来为每个用户生成row position

SELECT *, ROW_NUMBER(*) OVER (PARTITION BY WATCHLIST_USER ORDER BY ID)
FROM TF_WATCHLIST_PERSISTENCE 

大多数情况下,您 需要存储行位置。它很简单,可以为查询添加排名功能。

如果确实需要在表格中存储这样的行位置,可以使用简单的多行触发器

CREATE TRIGGER TF_WATCHLIST_PERSISTENCE_ADD_POSITION 
ON TF_WATCHLIST_PERSISTENCE
AFTER INSERT, UPDATE
AS
    update TF_WATCHLIST_PERSISTENCE 
    set WATCHLIST_POSITION =ROW_NUMBER() OVER (PARTITION BY WATCHLIST_USER ORDER BY ID)
    FROM inserted 
    INNER JOIN TF_WATCHLIST_PERSISTENCE on inserted.ID=TF_WATCHLIST_PERSISTENCE.ID

update TF_WATCHLIST_PERSISTENCE 
set WATCHLIST_POSITION =ROW_NUMBER() OVER (PARTITION BY WATCHLIST_USER ORDER BY ID)
FROM inserted 
INNER JOIN TF_WATCHLIST_PERSISTENCE 
    ON inserted.WATCHLIST_USER =TF_WATCHLIST_PERSISTENCE.WATCHLIST_USER 

如果要为现有用户插入新条目并重新计算所有位置