sql server而不是触发器插入语句,其中包含插入的变量和值
我有一个替代触发器。我有一个变量值。
我想将值插入到其中一列中并使用插入的字段。这就是我想要实现的目标:
declare @someLocalVariable varchar(9)
set @someLocalVariable = dbo.someLocalUDF()
INSERT myTable (field1, field2, field3)
VALUES (@someLocalVariable, (select field2, field3 from inserted))
I have an instead of trigger. I have a value in a variable.
I would like to insert the value into one of the columns and use the fields from inserted. This is what I am trying to accomplish:
declare @someLocalVariable varchar(9)
set @someLocalVariable = dbo.someLocalUDF()
INSERT myTable (field1, field2, field3)
VALUES (@someLocalVariable, (select field2, field3 from inserted))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请记住,如果这是由批量插入触发的(即 INSERTED 有多个记录),则所有记录也将被插入到目标表中。
(恕我直言,这正是它应该做的,但由于某种原因,人们通常会忘记这一点。)
编辑
OP忘记说出他的问题的真正含义:生成唯一的主键值。因此,在这种情况下,一种方法是这样的:
Just remember that if this was triggered by a batch insert (i.e. INSERTED has more than one record), all records will also be inserted into the target table.
(Which is exactly what it should do IMHO, but for some reason people usually forget about that.)
Edit
The OP has forgotten to say what his question was really about: generating unique primary key values. So, in that case, one way of doing that would be something like this: