sql数列
1. 如何用T-sql 产生1到100的数列
1、用循环做
WHILE @i <- 100
BEGIN
...
END
2、批量自插入
A)建一个表TableX(ID, Code),其中ID字段设置为IDENTITY(1, 1)
B)插入一条记录
C)反复自插入(使用下述SQL),7次后超过100条了
INSERT INTO TableX
SELECT ...
FROM TableX
D)查询此表的ID<=100的数据
SELECT ID
FROM TableX
WHERE ID <= 100
2. 用SQL server写一个存储过程,输出数列1 1 2 3 5 8 13
第一步:点击数据库下的“可编程性”,选择“存储过程”,点击鼠标右键,选择“新建存储过程”
第二步:在create PROCEDURE 后 输入存储过程的名字,紧跟着的就是定义存储过程的参数,接下来就可以去编写自己所需要组装的存储过程语句了
注意,怕写的不对,可以执行下,想验证sql语句是否正确,就使用print输出下
第三步:点击上面的执行,存储过程就写好了,要怎么调用呢,在sqlserver的语句查询框中,输入exec 存储过程名 参数,执行就可以了。
3. sqlserver中递增数列标识的问题
dbcc checkident(‘表名’,reseed,0)
或者
SET IDENTITY_INSERT [ database_name . [ schema_name ] . ] table { ON | OFF }
set identity_insert talbeName on
这样可以手动更改标识列
也可以
Alter table talbeName Drop Column ColumnName
Alter table talbeName Add ColumnName Int IDENTITY(1,1)
4. 使用sql语句写一个函数计算一个数列的累计和
统计一个班的学生成绩
select student_no,sum(grade) from student group by student_no
5. sql自动生成自然数列怎么写
select a.col*1000+b.col*100+c.col*10+d.col+1 as col
from
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)a
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)b
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)c
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)d
order by col