日期:2014-05-18  浏览次数:20660 次

求助高手
能不能用缺省實現插入數據時對一個列自動生成,格式是PQ+0001...

------解决方案--------------------
drop table table1;
go
CREATE TABLE [dbo].[Table1] (
[sn] [int] IDENTITY (1, 1) NOT NULL ,
[col1] AS ( 'PQ ' + replicate( '0 ',10-len(sn))+cast(sn as varchar)),
[col2] [char] (10)
)
go
--create trigger tr_1 on table1 for insert as update table1 set col3= 'PQ ' + replicate( '0 ',(10 - len(table1.sn))) from inserted where table1.sn=inserted.sn
go
insert into table1(col2)
select 'b '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
union all select 'c '
go
select * from table1;