首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

创建表的SQL语句

2012-01-12 
求一个创建表的SQL语句createTABLETUser([ID]intidentity(1,1),[User_ID]varchar(250)defaultUSE +replace

求一个创建表的SQL语句
create   TABLE   TUser
(
[ID]   int   identity(1,1)   ,
[User_ID]   varchar(250)   default   'USE '+replace(replace(replace(CONVERT(varchar(250),   getdate(),   120   ), '- ', ' '), '   ', ' '), ': ', ' ')+四位流水码
[User_Name]   varchar(250),
State   int   default   0,
)
请问怎么才能实现上面的四位流水码
要求是用默认值来实现
而不是用约束来实现
因为用约束来实现的话添加以后就不能修该了

[解决办法]
Id, FormatId, F1 ,F2
Id序号我设了自动加一,FormatId我想他也象这样 "SL000001 ",
当Insert时就加1,FormatId我想他也能自动加一 "SL000001 ", "SL000002 "...
能用一条sql什么办法实现.最好不要用中间表。有什么好方法?
谢谢!


create table #test
(id int identity,
FormatId as 'SL '+right(10000000+id,6),
F1 varchar(50))
go
insert #test(F1) select '1 '
union all select '2 '


select * from #test

drop table #test


/*
id FormatId F1
----------- -------------- -----
1 SL000001 1
2 SL000002 2

(所影响的行数为 2 行)
*/
[解决办法]
触发器这东西实现是很容易,但是不推荐

热点排行