这是我的触发器:
ALTER TRIGGER DONORINFO_INSERT
ON [dbo].[DONORINFO] INSTEAD OF INSERT
AS
DECLARE @sequence AS VARCHAR(50) = ''
DECLARE @tranLen VARCHAR(10)
SET @sequence = (SELECT TOP 1 SUBSTRING([DONORID], 3, 8)
FROM [dbo].[DONORINFO]
ORDER BY [DONORID] DESC)
IF (@sequence IS NULL OR @sequence = '')
BEGIN
SELECT @sequence = REPLICATE('0', 7 ) + '1'
END
ELSE
BEGIN
SELECT @tranLen = LEN(@sequence)
SELECT @sequence = @sequence + 1
SELECT @tranLen = ABS(@tranLen - LEN(CAST(@sequence AS INT)))
SELECT @sequence = REPLICATE('0', @tranLen) + @sequence
END
DECLARE @DONORID AS [nvarchar](50) = 'DN' + CONVERT(VARCHAR, @sequence)
INSERT INTO [dbo].[DONORINFO] ([DONORID], [DONORNAME])
SELECT @DONORID, inserted.DONORNAME
FROM inserted
在脚本的第一行中,我正在阅读DONORINFO
表,在该表中检查了唯一ID是否存在。之后,我将记录插入该表中。我第一次测试,插入到选择脚本工作,但第二次,它失败并发送和违反主键错误。
但是,如果我逐行插入测试,它就可以工作。
这是一行一行的插入脚本。
INSERT INTO [dbo].[DONORINFO] ([DONORID], [DONORNAME])
VALUES ('DN00000001', 'test')
如果我运行两次,记录将是这样的:
DONORID DONORNAME
---------------------
DN00000001 test
DN00000002 test
这是插入到选择脚本中不起作用:
INSERT INTO [dbo].[DONORINFO] ([DONORID], [DONORNAME])
SELECT
'',
[NameOfDonor]
FROM
[dbo].[_TEMPENDOWMENTFUND] AS ENDF
WHERE
[ENDF].[NameOfDonor] NOT IN (SELECT [DONORNAME]
FROM [dbo].[DONORINFO])
_TEMPDOWMENTFUND
是我创建的一个表,用于存储从Excel工作表迁移的数据,触发器的目的是为每个插入的工作表生成一个唯一的DONORID
DONORINFO
表。
现在我的问题是,我想在select语句中执行插入,这是一个多行插入,但是我很难搞清楚我创建的触发器出了什么问题。
任何帮助将不胜感激。感谢。
答案 0 :(得分:0)
让@sequence
完全int
(并可能将其重命名为last_id
),最后添加字符。
要编号行,请在ROW_NUMBER()
的最终选择中使用INSERTED
:
INSERT INTO [dbo].[DONORINFO] ([DONORID], [DONORNAME])
SELECT
'DN' + REPLICATE('0', ABS(@len_you_need - LEN(t.generated_id))) + CAST (t.generated_id as varchar(100)),
t.DONORNAME
FROM
(
SELECT
i.DONORNAME,
@sequence+ROW_NUMBER()OVER(ORDER BY i.DONORNAME) as generated_id
FROM inserted i
) t
@len_you_need
- 是您需要的DONORID
长度。我想这可能是8个字符的常量。在你的来源中,你在这里计算:
SELECT @tranLen = LEN(@sequence)
t.rn
是在上面给出的子查询中生成的“序列”值,它具有t
别名。为清楚起见,将其重命名为generated_id
。
这个区块:
BEGIN
SELECT @tranLen = LEN(@sequence)
SELECT @sequence = @sequence + 1
SELECT @tranLen = ABS(@tranLen - LEN(CAST(@sequence AS INT)))
SELECT @sequence = REPLICATE('0', @tranLen) + @sequence
END
不再需要了。
答案 1 :(得分:0)
这是完整的解决方案@ ivan-starostin帮助我回答。
ALTER TRIGGER DONORINFO_INSERT ON [dbo].[DONORINFO]
INSTEAD OF INSERT, UPDATE
AS
DECLARE @sequence AS VARCHAR(50) = ''
DECLARE @tranLen VARCHAR(10)
SET @sequence = (SELECT TOP 1 SUBSTRING([DONORID], 3, 8) FROM [dbo].[DONORINFO] ORDER BY [DONORID] DESC)
IF (@sequence IS NULL OR @sequence = '')
BEGIN
SELECT @sequence = REPLICATE('0', 7 )
END
ELSE
BEGIN
SELECT @tranLen = LEN(@sequence)
SELECT @sequence = @sequence + 1
SELECT @tranLen = ABS(@tranLen - LEN(CAST(@sequence AS INT)))
SELECT @sequence = REPLICATE('0', @tranLen) + @sequence
END
INSERT INTO [dbo].[DONORINFO] ([DONORID], [DONORNAME])
SELECT
'DN' + REPLICATE('0', ABS(8 - LEN(t.generated_id))) + CAST (t.generated_id as varchar(100)),
t.DONORNAME
FROM
(
SELECT
i.DONORNAME,
@sequence+ROW_NUMBER()OVER(ORDER BY i.DONORNAME) as generated_id
FROM inserted i
) t
所以如果我在下面运行那两个不同的insert into select
......
INSERT INTO [dbo].[DONORINFO] ([DONORNAME])
SELECT
[NameOfDonor]
FROM [dbo].[_TEMPENDOWMENTFUND] AS ENDF
WHERE [ENDF].[NameOfDonor] NOT IN (SELECT [DONORNAME] FROM [dbo].[DONORINFO])
INSERT INTO [dbo].[DONORINFO] ([DONORNAME])
SELECT
[NameOfDonor]
FROM [dbo].[_TEMPENDOWED] AS ENDF
WHERE [ENDF].[NameOfDonor] NOT IN (SELECT [DONORNAME] FROM [dbo].[DONORINFO])
donorid
迭代将是这样的两个不同的表源(由于机密性,我已经省略了捐赠者名称)。
DONORID DONORNAME (from _TEMPENDOWMENTFUND)
------------------------
DN00000001 test
DN00000002 test
DN00000003 test
DN00000004 test
DN00000005 test
DN00000006 test
DN00000007 test
DONORID DONORNAME (from _TEMPENDOWED)
------------------------
DN00000007 test
DN00000008 test
DN00000009 test
DN00000010 test
DN00000011 test
DN00000012 test
DN00000013 test