Sql server 如何在SQL Server中按行打印表?
我创建了一个输出数字表的函数,下面是我的UDF标量函数:Sql server 如何在SQL Server中按行打印表?,sql-server,tsql,stored-functions,Sql Server,Tsql,Stored Functions,我创建了一个输出数字表的函数,下面是我的UDF标量函数: create function fntable(@a int) returns varchar(250) as begin declare @b int, @c varchar(250),@e varchar(50)='' set @b=1 while (@b<=10) begin set @c=@a*@b set @e=@e+@c set @b=@b+1 end return @e end 创建函数表(@a int) 返回var
create function fntable(@a int)
returns varchar(250)
as begin
declare @b int, @c varchar(250),@e varchar(50)=''
set @b=1
while (@b<=10)
begin
set @c=@a*@b
set @e=@e+@c
set @b=@b+1
end
return @e
end
创建函数表(@a int)
返回varchar(250)
作为开始
声明@b int,@c varchar(250),@e varchar(50)=”
设置@b=1
while(@b您需要一个表值函数。按原样使用您的代码,这大致可以转换为:
create function fntable(@a int)
returns @e table(c int)
as begin
declare @b int = 1
while (@b <= 10)
begin
insert into @e values (@a * @b)
set @b += 1
end
return
end
改变
到
是的,我可以,但如果可能的话,我想根据我的代码显示。
create function fntable(@a int)
returns table as
return (
select @a * 1 as c
union all
select @a * 2 as c
union all
select @a * 3 as c
union all
select @a * 4 as c
union all
select @a * 5 as c
union all
select @a * 6 as c
union all
select @a * 7 as c
union all
select @a * 8 as c
union all
select @a * 9 as c
union all
select @a * 10 as c
)
set @e=@e+@c
set @e=@e+@c + CHAR(10)