不用order by,第一次写存储过程,选了简单地选择排序法练练手。不足之处恳请斧正,还望海涵。
思路是输入一串有分隔符的数字字符串,加上id将其存入表中。再根据id循环比较。
1.插入数据1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19CREATE PROC f_splite
@s VARCHAR(100),
@split VARCHAR(10)
AS
BEGIN
DECLARE @splitlen INT
DECLARE @i INT = 1
SET @splitlen = LEN(@split + 'a') - 2
WHILE CHARINDEX(@split, @s) > 0
BEGIN
INSERT INTO dbo.num_order
VALUES ( @i,LEFT(@s, CHARINDEX(@split, @s) - 1) )
SET @s = STUFF(@s, 1, CHARINDEX(@split, @s) + @splitlen, '')
SET @i = @i +1
END
--INSERT into dbo.num_order
--VALUES (@i,@s)
END
GO
1 | DECLARE @str NVARCHAR(200)='5,4,3,2,1,' |
2.选择排序
1 | /*选择排序法*/ |
3.执行
1 | delete from num_order |