SQL提数语句,如何书写

SQL提数语句,如何书写,第1张

select usercode,name,comcode from B where usercode = (select floor(comcode/100) comcode2 form B) and name=(select name from B) (成立条件:comcode,usercode都是数字型)

create table aaa (num1 int,num2 int)

insert into aaa values (1,3)

insert into aaa values (6,9)

--竖版

create function dbo.func_d (@a int)

RETURNS @table table(id int) as

begin

declare @num1 int,@num2 int

declare mycursor cursor for

select * from aaa

open mycursor

fetch mycursor into @num1,@num2

while @@fetch_status=0

begin

while @num1<=@num2

begin

insert into @table values (@num1)

set @num1=@num1+1

end

fetch mycursor into @num1,@num2

end

return

end

select * from dbo.func_d(1)

--结果

id

1

2

3

6

7

8

9

----------------------------------

--横版

create function dbo.func_d2 (@a int)

RETURNS @table table(string varchar(4000)) as

begin

declare @num1 int,@num2 int

insert into @table values ('a')

declare mycursor cursor for

select * from aaa

open mycursor

fetch mycursor into @num1,@num2

while @@fetch_status=0

begin

while @num1<=@num2

begin

update @table set string=string+','+convert(varchar,@num1)

set @num1=@num1+1

end

fetch mycursor into @num1,@num2

end

update @table set string=stuff(string,1,2,N'')

return

end

select * from dbo.func_d2(1)

--结果:

string

1,2,3,6,7,8,9


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7503608.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-06
下一篇 2023-04-06

发表评论

登录后才能评论

评论列表(0条)

保存