根据参数更改值更新其他行 SQL 2012 ASP.NET
Update Additional rows based on parameter change value SQL 2012 ASP.NET
我有以下 SQL 由 ASP.NET C# 形式的更新过程触发的更新查询..
UPDATE
[Data_TEST] SET
[Contact Phone] = @Contact_Phone
, [Contact] = @Contact
, [Contact Info Changed] = @Contact_Info_Changed
WHERE
(([Contact Phone] = @original_Contact_Phone) OR
([Contact Phone] IS NULL AND
@original_Contact_Phone IS NULL)) AND
(([Contact] = @original_Contact) OR
([Contact] IS NULL AND
@original_Contact IS NULL))
(([Contact Info Changed] = @original_Contact_Info_Changed) OR
([Contact Info Changed] IS NULL AND
@original_Contact_Info_Changed IS NULL))
我想将 [联系信息已更改] 的值更改为 'Y' 如果
[联系人 Phone] 或 [联系人] 在更新过程中发生了变化。
你可以创建一个触发器来检查这些值是否更新
所以
create trigger trg_change on Data_Test
after update
as
begin
if(select [Contact Phone] from deleted)!=(select [Contact Phone] from inserted)
or (select Contact from deleted)!=(select Contact from inserted)
begin
update Data_Test
set
[Contact Info Changed]='y'
end
end
如果你有主键或唯一键,那显然你应该有你可以按照我的脚本
IF OBJECT_ID('tempdb..#Data_TEST') IS NOT NULL
DROP TABLE #Data_TEST
create table #Data_TEST
(
ID int,
[Contact Phone] varchar(10),
[Contact] varchar(10),
[Contact Info Changed] char(1)
)
insert into #Data_TEST values
(1, 'Phone1', 'Contact1', 'N'),
(2, 'Phone2', 'Contact2', 'N')
declare @Contact_Phone varchar(10) = 'Phone2'
declare @Contact varchar(10) = 'Contact2'
UPDATE #Data_TEST
SET
[Contact Phone] = @Contact_Phone
, [Contact] = @Contact
, [Contact Info Changed] =
case
when
@Contact = (select [Contact] from #Data_TEST AS dt2 where dt2.ID = #Data_TEST.ID) AND
@Contact_Phone = (select [Contact Phone] from #Data_TEST AS dt2 where dt2.ID = #Data_TEST.ID)
then 'N'
else 'Y'
end
假设您的 table 为 #Data_TEST 并假设您的 Pk/UK 为 ID
此脚本将参数值与您当前在 table
中的值进行比较
更新前数据
+----+---------------+----------+----------------------+
| ID | Contact Phone | Contact | Contact Info Changed |
+----+---------------+----------+----------------------+
| 1 | Phone1 | Contact1 | N |
| 2 | Phone2 | Contact2 | N |
+----+---------------+----------+----------------------+
更新后的数据
+----+---------------+----------+----------------------+
| ID | Contact Phone | Contact | Contact Info Changed |
+----+---------------+----------+----------------------+
| 1 | Phone2 | Contact2 | Y |
| 2 | Phone2 | Contact2 | N |
+----+---------------+----------+----------------------+
我删除了你的 where 子句,因为它有语法错误,但记得将它添加到你的最终脚本中。
我有以下 SQL 由 ASP.NET C# 形式的更新过程触发的更新查询..
UPDATE
[Data_TEST] SET
[Contact Phone] = @Contact_Phone
, [Contact] = @Contact
, [Contact Info Changed] = @Contact_Info_Changed
WHERE
(([Contact Phone] = @original_Contact_Phone) OR
([Contact Phone] IS NULL AND
@original_Contact_Phone IS NULL)) AND
(([Contact] = @original_Contact) OR
([Contact] IS NULL AND
@original_Contact IS NULL))
(([Contact Info Changed] = @original_Contact_Info_Changed) OR
([Contact Info Changed] IS NULL AND
@original_Contact_Info_Changed IS NULL))
我想将 [联系信息已更改] 的值更改为 'Y' 如果 [联系人 Phone] 或 [联系人] 在更新过程中发生了变化。
你可以创建一个触发器来检查这些值是否更新 所以
create trigger trg_change on Data_Test
after update
as
begin
if(select [Contact Phone] from deleted)!=(select [Contact Phone] from inserted)
or (select Contact from deleted)!=(select Contact from inserted)
begin
update Data_Test
set
[Contact Info Changed]='y'
end
end
如果你有主键或唯一键,那显然你应该有你可以按照我的脚本
IF OBJECT_ID('tempdb..#Data_TEST') IS NOT NULL
DROP TABLE #Data_TEST
create table #Data_TEST
(
ID int,
[Contact Phone] varchar(10),
[Contact] varchar(10),
[Contact Info Changed] char(1)
)
insert into #Data_TEST values
(1, 'Phone1', 'Contact1', 'N'),
(2, 'Phone2', 'Contact2', 'N')
declare @Contact_Phone varchar(10) = 'Phone2'
declare @Contact varchar(10) = 'Contact2'
UPDATE #Data_TEST
SET
[Contact Phone] = @Contact_Phone
, [Contact] = @Contact
, [Contact Info Changed] =
case
when
@Contact = (select [Contact] from #Data_TEST AS dt2 where dt2.ID = #Data_TEST.ID) AND
@Contact_Phone = (select [Contact Phone] from #Data_TEST AS dt2 where dt2.ID = #Data_TEST.ID)
then 'N'
else 'Y'
end
假设您的 table 为 #Data_TEST 并假设您的 Pk/UK 为 ID
此脚本将参数值与您当前在 table
中的值进行比较更新前数据
+----+---------------+----------+----------------------+
| ID | Contact Phone | Contact | Contact Info Changed |
+----+---------------+----------+----------------------+
| 1 | Phone1 | Contact1 | N |
| 2 | Phone2 | Contact2 | N |
+----+---------------+----------+----------------------+
更新后的数据
+----+---------------+----------+----------------------+
| ID | Contact Phone | Contact | Contact Info Changed |
+----+---------------+----------+----------------------+
| 1 | Phone2 | Contact2 | Y |
| 2 | Phone2 | Contact2 | N |
+----+---------------+----------+----------------------+
我删除了你的 where 子句,因为它有语法错误,但记得将它添加到你的最终脚本中。