【发布时间】:2016-06-15 11:22:30
【问题描述】:
我有个小问题,我有一张这样的桌子
ref_id |ref_name | ref_Type | ref_debit
1 |BVPG Corporate 2M Spain | |
2 |BVPG Small 5M 54FZE | |
3 |BVPG Corporate 1M Spain | |
我想自动填充列 ref_type 和 ref debit。
我手动做了一个子字符串(只是我的许多查询的一部分)
UPDATE t
SET t.ref_type = (substring(t.ref_name,6,10))
FROM ref_table t
INNER JOIN ref_table t1 on t1.ref_id = t.ref_id AND t1.ref_name LIKE'BVBG C%'
如果是 BVPG Corporate 或 Small 我有类似的东西
ref_id |ref_name | ref_Type | ref_debit
1 |BVPG Corporate 2M Spain | Corporate | 2M
2 |BVPG Small 5M 54FZE | Small | 5M
3 |BVPG Corporate 1M Spain | Corporate | 1M
这就是我想要的,我知道该怎么做。
但是当我有一个新行时,我想自动生成这个结果。
我考虑过触发器,但我正在使用 SQL Server Management Studio 我不知道该怎么做,我什至不知道触发器是否是最好的解决方案。
我尝试了类似的方法,但它不起作用(所有行都会更新,即使它以 'BVPG C' 开头)
if exists(
select ref_name
from inserted
where ref_name like 'BVPG S%')
then update
【问题讨论】:
标签: tsql stored-procedures triggers