27,579
社区成员
发帖
与我相关
我的任务
分享
[quote=引用 9 楼 wufeng4552 的回复:] [quote=引用 6 楼 fredrickhu 的回复:] 不过在实际开发中不建议使用 master..[spt_values]表。
[quote=引用 6 楼 fredrickhu 的回复:] 不过在实际开发中不建议使用 master..[spt_values]表。
自定义个函数 功能:学号自增 然后将学号列的默认值设置为这个自定义函数 函数的代码自己搜下吧
能给解释一下代码的意思吗? [quote=引用 2 楼 fredrickhu 的回复:] insert into student(stu_No) SELECT RIGHT(1000+number,3) FROM master..[spt_values] WHERE [type]='p' AND number BETWEEN 1 AND 100
不过在实际开发中不建议使用 master..[spt_values]表。
-- 建表
create table student(stu_No varchar(10))
-- 插入100行
declare @x int
select @x=1
while(@x<=100)
begin
insert into student(stu_No)
values(replicate('0',3-len(@x))+rtrim(@x))
select @x=@x+1
end
-- 结果
select stu_No from student
/*
stu_No
----------
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
(100 row(s) affected)
*/
能给解释一下代码的意思吗? insert into student(stu_No) SELECT RIGHT(1000+number,3) FROM master..[spt_values] WHERE [type]='p' AND number BETWEEN 1 AND 100
insert into student(stu_No) SELECT RIGHT(1000+number,3) FROM master..[spt_values] WHERE [type]='p' AND number BETWEEN 1 AND 100
为什么不设为int类型呢?