积极答复者
触发器问题

问题
答案
-
declare @t table([id] int,[roadName] varchar(1),[roadCode] NVARCHAR(10)) insert @t select 1,'a',null union all select 2,'b',null union all select 3,'c',null update @t set [roadCode]='DL'+right('0000'+ltrim(id),4) select * from @t /* id roadName roadCode ----------- -------- ---------- 1 a DL0001 2 b DL0002 3 c DL0003 */
- 已编辑 maco wangModerator 2012年12月19日 10:05
- 已标记为答案 小欢乐丶 2012年12月19日 10:13
全部回复
-
declare @t table([id] int,[roadName] varchar(1),[roadCode] NVARCHAR(10)) insert @t select 1,'a',null union all select 2,'b',null union all select 3,'c',null update @t set [roadCode]='DL'+right('0000'+ltrim(id),4) select * from @t /* id roadName roadCode ----------- -------- ---------- 1 a DL0001 2 b DL0002 3 c DL0003 */
- 已编辑 maco wangModerator 2012年12月19日 10:05
- 已标记为答案 小欢乐丶 2012年12月19日 10:13
-
如果是用TRIGGER的話,可以參考下列的做法:
use tempdb go if object_id('t') is not null drop table t go create table t([id] int,[roadName] char(1),[roadCode] nvarchar(10)) go --建立trigger create trigger tr_insert_t on t for insert as update t set roadCode = 'DL' + right(replicate('0',5) + convert(varchar(5),id),5) where id = (select id from inserted) go insert t(id,[roadName]) values (1,'a') insert t(id,[roadName]) values (2,'b') insert t(id,[roadName]) values (3,'c') go select * from t
以上說明若有錯誤請指教,謝謝。