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