如何使用sql查询在sql表中按顺序插入数字 [英] how to insert numbers in sequence in sql table using sql query

查看:158
本文介绍了如何使用sql查询在sql表中按顺序插入数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨专家,



我有一个名为SerialNoInfo的表,在这个表中我想通过使用sql将数字从0001到10000插入列(Serialno)查询。



例如:



0001

0002

0003

0004

0005

0006















$



请帮帮我。

谢谢。

解决方案

尝试:

  DECLARE   @ Start   int  
DECLARE @ End int
选择 @ Start = 1,@ End = 10000

; WITH 序列(数字) AS

SELECT @开始 AS 编号
UNION ALL
SELECT 数字+ 1
FROM 序列
WHERE 数字< @ End

INSERT INTO 表_2(Val)
SELECT * FROM 序列 选项(MaxRecursion 10000


如果你访问主表



没有循环



  INSERT   INTO 表_2(Val)
SELECT distinct number
FROM master。 .spt_values
其中编号 1 1000


Hi Experts,

I have a table with name SerialNoInfo, In this table I want to Insert Numbers from 0001 to 10000 into Column(Serialno) by using sql query.

for example as:

0001
0002
0003
0004
0005
0006
.
.
.
.
.
.
.
10000

Please help me.
Thanks.

解决方案

Try:

DECLARE @Start int
DECLARE @End int
Select @Start=1, @End=10000
 
;WITH Sequence( Number ) AS
(
    SELECT @start AS Number
    UNION ALL
    SELECT Number + 1
        FROM Sequence
        WHERE Number < @End
)
INSERT INTO Table_2 (Val)
SELECT * FROM Sequence Option (MaxRecursion 10000)


If you access to master table

With out the loop

INSERT INTO Table_2 (Val)
SELECT distinct number
        FROM master..spt_values
        where number between 1 and 1000


这篇关于如何使用sql查询在sql表中按顺序插入数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆