我如何从C#传递参数以在SQL中触发查询 [英] How I pass parameter from C# to trigger query in SQL

查看:184
本文介绍了我如何从C#传递参数以在SQL中触发查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要先做一些事情或代替更新,我创建此触发器,



i want do something before or instead of update , i create this trigger ,

create trigger CarTriggerBeforeUpdate
on dbo.Cars
INSTEAD OF UPDATE
AS
BEGIN
   insert into CarTriggerUpdateDelete(Car_id, Car_No, Car_No_2)
   select Car_id, Car_No, Car_No_2 from Cars
   where Car_id = @Value //i want pass value here 
END





我尝试了什么:



我是怎么做的,我想从表中插入行更新之前的另一个表



What I have tried:

how i do that i want insert row from table to another table before update it

推荐答案

Triggers cannot pass parameters - you have to operate within the database operation. Therefore, unless the Cars table has a CreateBy or LastUpdateBy referencing the id of the user making the change, you would need to wrap the INSERT into a stored procedure that would perform that check before performing the INSERT.

If by chance your Cars table has a CreateBy that contains the id of the user performing the change, then a trigger such as this below should work:







CREATE TRIGGER tg_Cars_Register ON dbo.Cars
AFTER INSERT AS
BEGIN
    IF EXISTS (SELECT * FROM INSERTED WHERE CreateBy <> 1110) BEGIN
        RAISERROR('Unauthorized parking', 16, 1)
        ROLLBACK
        RETURN
    END
END
GO


这篇关于我如何从C#传递参数以在SQL中触发查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆