带有触发器的SQL Server Split逗号(插入,更新,删除)

时间:2016-02-20 20:12:25

标签: sql sql-server triggers split separator

首先抱歉我的英语不好 我需要一些有关插入,更新,删除触发器的分割逗号的帮助

我有两张桌子。第一个表名为VideoList,第二个表名为VideoCategoriJoin


VideoList表

ID CategoriID

5 14,21,28,41

在添加记录时首先显示此数据



如何创建VideoCategoriJoin表行

VideoCategoriJoin表

ID VideoID的CategoriID

1 5 14
2个5 21
3个5 28
4个5 41


谢谢大家。

2 个答案:

答案 0 :(得分:0)

Hamza selam,

Önceliklebirfonksiyonaihtiyacınvar'14,21,28,41'değerinisplit eden。

ALTER FUNCTION [dbo].[fnSplit]( @sInputList VARCHAR(8000), @sDelimiter VARCHAR(8000) = ',') RETURNS @List TABLE (item VARCHAR(8000))

BEGIN     DECLARE @sItem VARCHAR(8000)     什么是CHARINDEX(@ sDelimiter,@ sInputList,0)<> 0     开始         选择         @ sItem = RTRIM(LTRIM(SUBSTRING(@ sInputList,1,CHARINDEX(@ sDelimiter,@ sInputList,0)-1))),         @ sInputList = RTRIM(LTRIM(SUBSTRING(@ sInputList,CHARINDEX(@ sDelimiter,@ sInputList,0)+ LEN(@sDelimiter),LEN(@sInputList))))

    IF LEN(@sItem) > 0
        INSERT INTO @List SELECT @sItem
END
IF LEN(@sInputList) > 0
    INSERT INTO @List SELECT @sInputList

RETURN END

Sonrasındaaşağıdakisorguyabenzer birsorguiziinigörecektir。

drop table #VideoList 删除表#result 选择5作为Id,'12,32,33'作为CategoryId进入#VideoList 选择1作为Id,将1选为VideoId,将1作为CategoryId选择为#result

截断表#result 声明@Id int while(从#VideoList中选择count(1))> 0 开始 设置@Id =(从#VideoList中选择前1个Id)

插入#result 选择ROW_NUMBER()结束(由@Id排序)作为Id,@ Id作为VideoId,选择项目作为CategoryId来自[dbo] .fnSplit 删除#VideoList,其中Id = @Id 结束

从#result

中选择*

答案 1 :(得分:0)

Hocam merhaba,

Fonksiyonkullanmadanağağıdakişekildehallettim。 Umarımbiryanlışlıkyapmamışımdır。 Hiçbirsorunolmadançalışıyorşuan。
Teşekkürettimyardımlarınıziçin。

嗨朋友们,

此代码试图在没有功能的情况下成功。我希望这会有用 感谢你所做的一切

CREATE TRIGGER [dbo].[Eslesme_Add_Update] ON [dbo].[VideoListesi] 
   AFTER INSERT, UPDATE
AS 
BEGIN
DECLARE @Trg_VideoID int
DECLARE @Trg_KategoriID VARCHAR(250)
DECLARE @tmpstr VARCHAR(250)
DECLARE @likedeger VARCHAR(250)

DECLARE @Trg_EskiVideoID int   /* VideoiID old value */
DECLARE @eskitmpstr VARCHAR(250) 
DECLARE @Trg_EskiKategoriID VARCHAR(250) /* KategoriiID old value */


SELECT @Trg_KategoriID=KategoriID, @Trg_VideoID=id from inserted
set @Trg_KategoriID=@Trg_KategoriID+','


Select @Trg_EskiKategoriID=KategoriID, @Trg_EskiVideoID=id from deleted
Set @Trg_EskiKategoriID=@Trg_EskiKategoriID+','


WHILE CHARINDEX(',', @Trg_EskiKategoriID) > 0 
BEGIN
SET @eskitmpstr = SUBSTRING(@Trg_EskiKategoriID, 1, ( CHARINDEX(',', @Trg_EskiKategoriID) - 1 ))

delete from KategoriVideoEslesmesi where KategoriID=@eskitmpstr and VideoID=@Trg_EskiVideoID
SET @Trg_EskiKategoriID = SUBSTRING(@Trg_EskiKategoriID, CHARINDEX(',', @Trg_EskiKategoriID) + 1, LEN(@Trg_EskiKategoriID))
END


WHILE CHARINDEX(',', @Trg_KategoriID) > 0 
BEGIN
SET @tmpstr = SUBSTRING(@Trg_KategoriID, 1, ( CHARINDEX(',', @Trg_KategoriID) - 1 ))
SET @likedeger = '%'+ @tmpstr +'%'

INSERT INTO kategorivideoeslesmesi values(@tmpstr,@Trg_VideoID)
SET @Trg_KategoriID = SUBSTRING(@Trg_KategoriID, CHARINDEX(',', @Trg_KategoriID) + 1, LEN(@Trg_KategoriID))

END
END