id aa bb cc dd
1 1 2 3 4
2 5 4 3 5
3 5 6 7 8
4 9 10 11 12
select id , dbo.MyMax(aa,bb,cc,dd) as 最大值, dbo.MyMin(aa,bb,cc,dd)as 最小值
id 最大值 最小值
1 4 1
2 5 3
3 8 5
4 12 9
现在小函数:返回最大值函数MyMax和返回最小值函数MyMin如何写?? 要求参数个数不限,即 能MyMax(a,b,c),也能 MyMax(a,b)
13 个解决方案
#1
保证4个参数吗?
#2
保证4个参数吗
create function MyMax(
@a int,
@b int,
@c int,
@d int
)
returns int
as
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
return @r
go
create function MyMax(
@a int,
@b int,
@c int,
@d int
)
returns int
as
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
return @r
go
#3
min也差不多
#4
to
Haiwer(海阔天空) ,谢谢关注!
参数个数不限
Haiwer(海阔天空) ,谢谢关注!
参数个数不限
#5
--这样行吗?
create table tb(id int,aa int,bb int,cc int,dd int)
insert into tb select 1,1,2,3,4
union all select 2,5,4,3,5
union all select 3,5,6,7,8
union all select 4,9,10,11,12
go
create function mymin(@id int)
returns int
as
begin
return(select min(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
create function mymax(@id int)
returns int
as
begin
return(select max(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
select id,dbo.mymax(id) as [max],dbo.mymin(id) as [min] from tb group by id
drop table tb
drop function mymin,mymax
create table tb(id int,aa int,bb int,cc int,dd int)
insert into tb select 1,1,2,3,4
union all select 2,5,4,3,5
union all select 3,5,6,7,8
union all select 4,9,10,11,12
go
create function mymin(@id int)
returns int
as
begin
return(select min(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
create function mymax(@id int)
returns int
as
begin
return(select max(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
select id,dbo.mymax(id) as [max],dbo.mymin(id) as [min] from tb group by id
drop table tb
drop function mymin,mymax
#6
用户定义函数的参数。CREATE FUNCTION 语句中可以声明一个或多个参数。函数最多可以有 1,024 个参数。函数执行时每个已声明参数的值必须由用户指定,除非该参数的默认值已经定义。 如果函数的参数有默认值,在调用该函数时必须指定"default"关键字才能获得默认值。这种行为不同于存储过程中有默认值的参数,在存储过程中省略参数也意味着使用默认值。
使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个函数的参数仅用于该函数本身;相同的参数名称可以用在其它函数中。参数只能代替常量;而不能用于代替表名、列名或其它数据库对象的名称。
所以就算用默认参数,调用也很麻烦
使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个函数的参数仅用于该函数本身;相同的参数名称可以用在其它函数中。参数只能代替常量;而不能用于代替表名、列名或其它数据库对象的名称。
所以就算用默认参数,调用也很麻烦
#7
select 执行外部函数时可能对性能影响较大,所以设计使函数有较好的性能也至关重要
#8
alter function MyMax(
@a int,
@b int,
@c int=-2147483648,
@d int=-2147483648,
@e int=-2147483648,
@f int=-2147483648,
@g int=-2147483648,
@h int=-2147483648,
@i int=-2147483648
)
returns int
as
begin
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
if @e>@r
set @r=@e
if @f>@r
set @r=@f
if @g>@r
set @r=@g
if @h>@r
set @r=@h
if @i>@r
set @r=@i
return @r
end
go
--调用
select dbo.MyMax(1,2,3,default,default,default,default,default,default)
--你说函数的默认参数是不是不如不默认
@a int,
@b int,
@c int=-2147483648,
@d int=-2147483648,
@e int=-2147483648,
@f int=-2147483648,
@g int=-2147483648,
@h int=-2147483648,
@i int=-2147483648
)
returns int
as
begin
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
if @e>@r
set @r=@e
if @f>@r
set @r=@f
if @g>@r
set @r=@g
if @h>@r
set @r=@h
if @i>@r
set @r=@i
return @r
end
go
--调用
select dbo.MyMax(1,2,3,default,default,default,default,default,default)
--你说函数的默认参数是不是不如不默认
#9
关键是:是否有一种办法来解决参数不限的问题,当然不会超过1024
#10
sql server 2000的函数比较尴尬,不知道2005有没有加强
#11
多谢海阔天空,如果不要那么多default就好了...
#12
create table t(id int identity(1,1),aa int,bb int,cc int,dd int)
insert t select 1,2,3,4
union all select 5,4,3,5
union all select 5,6,7,8
union all select 9,10,11,12
GO
CREATE function dbo.MyMax(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @temp_ret>@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
CREATE function dbo.MyMin(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @ret=0
set @ret=@temp_ret
if @temp_ret<@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
select id , dbo.MyMax(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
) as 最大值,
dbo.MyMin(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
)as 最小值
from t
drop function MyMax
drop function MyMin
drop table t
/*
id 最大值 最小值
----------- ----------- -----------
1 4 1
2 5 3
3 8 5
4 12 9
*/
insert t select 1,2,3,4
union all select 5,4,3,5
union all select 5,6,7,8
union all select 9,10,11,12
GO
CREATE function dbo.MyMax(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @temp_ret>@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
CREATE function dbo.MyMin(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @ret=0
set @ret=@temp_ret
if @temp_ret<@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
select id , dbo.MyMax(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
) as 最大值,
dbo.MyMin(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
)as 最小值
from t
drop function MyMax
drop function MyMin
drop table t
/*
id 最大值 最小值
----------- ----------- -----------
1 4 1
2 5 3
3 8 5
4 12 9
*/
#13
dutguoyi(新鲜鱼排) 无法传递字段值
#1
保证4个参数吗?
#2
保证4个参数吗
create function MyMax(
@a int,
@b int,
@c int,
@d int
)
returns int
as
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
return @r
go
create function MyMax(
@a int,
@b int,
@c int,
@d int
)
returns int
as
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
return @r
go
#3
min也差不多
#4
to
Haiwer(海阔天空) ,谢谢关注!
参数个数不限
Haiwer(海阔天空) ,谢谢关注!
参数个数不限
#5
--这样行吗?
create table tb(id int,aa int,bb int,cc int,dd int)
insert into tb select 1,1,2,3,4
union all select 2,5,4,3,5
union all select 3,5,6,7,8
union all select 4,9,10,11,12
go
create function mymin(@id int)
returns int
as
begin
return(select min(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
create function mymax(@id int)
returns int
as
begin
return(select max(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
select id,dbo.mymax(id) as [max],dbo.mymin(id) as [min] from tb group by id
drop table tb
drop function mymin,mymax
create table tb(id int,aa int,bb int,cc int,dd int)
insert into tb select 1,1,2,3,4
union all select 2,5,4,3,5
union all select 3,5,6,7,8
union all select 4,9,10,11,12
go
create function mymin(@id int)
returns int
as
begin
return(select min(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
create function mymax(@id int)
returns int
as
begin
return(select max(col) from(
select col=aa from tb where id=@id union all
select bb from tb where id=@id union all
select cc from tb where id=@id union all
select dd from tb where id=@id)a)
end
go
select id,dbo.mymax(id) as [max],dbo.mymin(id) as [min] from tb group by id
drop table tb
drop function mymin,mymax
#6
用户定义函数的参数。CREATE FUNCTION 语句中可以声明一个或多个参数。函数最多可以有 1,024 个参数。函数执行时每个已声明参数的值必须由用户指定,除非该参数的默认值已经定义。 如果函数的参数有默认值,在调用该函数时必须指定"default"关键字才能获得默认值。这种行为不同于存储过程中有默认值的参数,在存储过程中省略参数也意味着使用默认值。
使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个函数的参数仅用于该函数本身;相同的参数名称可以用在其它函数中。参数只能代替常量;而不能用于代替表名、列名或其它数据库对象的名称。
所以就算用默认参数,调用也很麻烦
使用 @ 符号作为第一个字符来指定参数名称。参数名称必须符合标识符的规则。每个函数的参数仅用于该函数本身;相同的参数名称可以用在其它函数中。参数只能代替常量;而不能用于代替表名、列名或其它数据库对象的名称。
所以就算用默认参数,调用也很麻烦
#7
select 执行外部函数时可能对性能影响较大,所以设计使函数有较好的性能也至关重要
#8
alter function MyMax(
@a int,
@b int,
@c int=-2147483648,
@d int=-2147483648,
@e int=-2147483648,
@f int=-2147483648,
@g int=-2147483648,
@h int=-2147483648,
@i int=-2147483648
)
returns int
as
begin
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
if @e>@r
set @r=@e
if @f>@r
set @r=@f
if @g>@r
set @r=@g
if @h>@r
set @r=@h
if @i>@r
set @r=@i
return @r
end
go
--调用
select dbo.MyMax(1,2,3,default,default,default,default,default,default)
--你说函数的默认参数是不是不如不默认
@a int,
@b int,
@c int=-2147483648,
@d int=-2147483648,
@e int=-2147483648,
@f int=-2147483648,
@g int=-2147483648,
@h int=-2147483648,
@i int=-2147483648
)
returns int
as
begin
declare @r int
set @r=@a
if @b>@r
set @r=@b
if @c>@r
set @r=@c
if @d>@r
set @r=@d
if @e>@r
set @r=@e
if @f>@r
set @r=@f
if @g>@r
set @r=@g
if @h>@r
set @r=@h
if @i>@r
set @r=@i
return @r
end
go
--调用
select dbo.MyMax(1,2,3,default,default,default,default,default,default)
--你说函数的默认参数是不是不如不默认
#9
关键是:是否有一种办法来解决参数不限的问题,当然不会超过1024
#10
sql server 2000的函数比较尴尬,不知道2005有没有加强
#11
多谢海阔天空,如果不要那么多default就好了...
#12
create table t(id int identity(1,1),aa int,bb int,cc int,dd int)
insert t select 1,2,3,4
union all select 5,4,3,5
union all select 5,6,7,8
union all select 9,10,11,12
GO
CREATE function dbo.MyMax(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @temp_ret>@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
CREATE function dbo.MyMin(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @ret=0
set @ret=@temp_ret
if @temp_ret<@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
select id , dbo.MyMax(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
) as 最大值,
dbo.MyMin(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
)as 最小值
from t
drop function MyMax
drop function MyMin
drop table t
/*
id 最大值 最小值
----------- ----------- -----------
1 4 1
2 5 3
3 8 5
4 12 9
*/
insert t select 1,2,3,4
union all select 5,4,3,5
union all select 5,6,7,8
union all select 9,10,11,12
GO
CREATE function dbo.MyMax(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @temp_ret>@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
CREATE function dbo.MyMin(@str varchar(8000))
returns int
as
begin
declare @ret int,@temp_ret int
--set @str='1,5,10,20'+','
select @str=@str+',',@ret=0
while charindex(',',@str)>0
begin
set @temp_ret=convert(int,substring(@str,0,charindex(',',@str)))
if @ret=0
set @ret=@temp_ret
if @temp_ret<@ret
set @ret=@temp_ret
set @str=substring(@str,charindex(',',@str)+1,Len(@str)-charindex(',',@str))
end
return @ret
end
GO
select id , dbo.MyMax(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
) as 最大值,
dbo.MyMin(
convert(varchar,aa)+','+convert(varchar,bb)+','+convert(varchar,cc)+','+convert(varchar,dd)
)as 最小值
from t
drop function MyMax
drop function MyMin
drop table t
/*
id 最大值 最小值
----------- ----------- -----------
1 4 1
2 5 3
3 8 5
4 12 9
*/
#13
dutguoyi(新鲜鱼排) 无法传递字段值