SQL触发器不会被触发

时间:2015-06-10 07:45:44

标签: sql sql-server triggers

所以我必须在ASP.NET中导入CSV文件,之后需要在右表中进行排序。

导入工作并且我使用触发器来进一步整理数据,但是当我运行代码时它不会被触发。我有点新的SQL所以请温柔:) 这是我的代码:

    CREATE TRIGGER sortTables
    ON CSVtussenTabel
    AFTER INSERT
    AS
    declare @titel varchar(100);
    declare @artiest varchar(100);
    declare @jaar int;
    declare @positie int;
    declare @eindzenddatum datetime;
    declare @van float;
    declare @tot float;

    declare @artiestTeller int;
    declare @songTeller int;

    set @titel = (SELECT Titel FROM CSVtussenTabel);
    set @artiest = (SELECT Artiest FROM CSVtussenTabel);
    set @jaar = (SELECT Jaar FROM CSVtussenTabel);
    set @positie = (SELECT Positie FROM CSVtussenTabel);
    set @eindzenddatum = (SELECT Eindzenddatum FROM CSVtussenTabel);
    set @van = (SELECT Van FROM CSVtussenTabel);
    set @tot = (SELECT Tot FROM CSVtussenTabel);

    set @artiestTeller = (SELECT COUNT(*) FROM Artiest) + 1;
    set @songTeller = (SELECT COUNT(*) FROM Song) + 1;
    IF((SELECT COUNT(*) FROM Artiest WHERE @artiest IN (SELECT naam FROM         Artiest)) = 0)
    BEGIN
        INSERT INTO Artiest (naam)VALUES ( @artiest);
    END

    IF((SELECT COUNT(*) FROM Song WHERE @titel IN (SELECT titel FROM Song)) = 0)
    BEGIN
        INSERT INTO Song (songid, artiestid, titel, jaar) VALUES (@songTeller, @artiestTeller, @titel, @jaar);
    END

    IF((SELECT COUNT(*) FROM Lijst WHERE DATEPART(YEAR, @eindzenddatum) IN (SELECT top2000jaar FROM Lijst)) = 0)
    BEGIN
        INSERT INTO Lijst (songid, top2000jaar, positie, uitzenddatum, van, tot)VALUES (@songteller, DATEPART(YEAR, @eindzenddatum), @positie, @eindzenddatum, @van, @tot);
    END
    GO

0 个答案:

没有答案