1.数据库是SQLServer2005
2.首先定义了一个表值函数 ,主要信息为:(需要传递2个参数)
alter FUNCTION getAirportCoupleMonthStat
(
@deptID varchar(50),
@year varchar(4)
)
RETURNS @temp_tbl_airportCouple TABLE
(
-- Add the column definitions for the TABLE variable here
takeoffID varchar(50),
arrivedID varchar(50),
airportCouple varchar(100),
num int
)
特别声明,函数getAirportCoupleMonthStat是正确的,直接调用有返回结果
3.再定义一个调用该表值函数的表值函数
ALTER FUNCTION getAirCoupTop5MonthStat
(
-- Add the parameters for the function here
@deptID varchar(20),
@yearMonth varchar(4)
)
RETURNS
@temp_AirCoupTop5Month TABLE
(
-- Add the column definitions for the TABLE variable here
takeoffID varchar(20),
arrivedID varchar(20),
airportCouple varchar(40),
num int
)
AS
BEGIN
--我担心是参数名称相冲突,故多此一举,声明不同名字的变量,再传递给被调用函数
--但实际上是:两者都没有返回返回结果
declare @paramDept varchar(20)
set @paramDept = @deptID
declare @paramMonth varchar(20)
set @paramMonth = @yearMonth
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat (@paramDept, @paramMonth)
order by num desc
/*同样没有查询结果
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat (@deptID, @yearMonth)
order by num desc
*/
/*直接写上参数,有查询结果
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat ('V00989b3bV', '2009-07')
order by num desc
*/
RETURN
END然后,调用函数:
SELECT takeoffID,arrivedID,airportCouple,num
FROM dbo.getAirCoupTop5MonthStat('V00989b3bV','2009-07')查询结果为空.
在函数中,直接写上参数,能够查询到结果。请问,我应该如何传递参数呢?
还是SQLserver不支持?
2.首先定义了一个表值函数 ,主要信息为:(需要传递2个参数)
alter FUNCTION getAirportCoupleMonthStat
(
@deptID varchar(50),
@year varchar(4)
)
RETURNS @temp_tbl_airportCouple TABLE
(
-- Add the column definitions for the TABLE variable here
takeoffID varchar(50),
arrivedID varchar(50),
airportCouple varchar(100),
num int
)
特别声明,函数getAirportCoupleMonthStat是正确的,直接调用有返回结果
3.再定义一个调用该表值函数的表值函数
ALTER FUNCTION getAirCoupTop5MonthStat
(
-- Add the parameters for the function here
@deptID varchar(20),
@yearMonth varchar(4)
)
RETURNS
@temp_AirCoupTop5Month TABLE
(
-- Add the column definitions for the TABLE variable here
takeoffID varchar(20),
arrivedID varchar(20),
airportCouple varchar(40),
num int
)
AS
BEGIN
--我担心是参数名称相冲突,故多此一举,声明不同名字的变量,再传递给被调用函数
--但实际上是:两者都没有返回返回结果
declare @paramDept varchar(20)
set @paramDept = @deptID
declare @paramMonth varchar(20)
set @paramMonth = @yearMonth
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat (@paramDept, @paramMonth)
order by num desc
/*同样没有查询结果
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat (@deptID, @yearMonth)
order by num desc
*/
/*直接写上参数,有查询结果
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat ('V00989b3bV', '2009-07')
order by num desc
*/
RETURN
END然后,调用函数:
SELECT takeoffID,arrivedID,airportCouple,num
FROM dbo.getAirCoupTop5MonthStat('V00989b3bV','2009-07')查询结果为空.
在函数中,直接写上参数,能够查询到结果。请问,我应该如何传递参数呢?
还是SQLserver不支持?
@yearMonth ?
alter PROCEDURE TestParamTrans
-- Add the parameters for the stored procedure here
@dept varchar(20),
@yearMonth varchar(7)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
select @dept
select @yearMonth -- Insert statements for procedure here
SELECT *
from dbo.getAirportCoupleYearStat(@dept, @yearMonth)
END
GOexec TestParamTrans 'V00989b3bV','2009-07'有结果。难道如2楼所说,不能函数嵌套?
嵌套用户定义函数
用户定义函数可以嵌套;也就是说,用户定义函数可相互调用。被调用函数开始执行时,嵌套级别将增加;被调用函数执行结束后,嵌套级别将减少。用户定义函数的嵌套级别最多可达 32 级。如果超出最大嵌套级别数,整个调用函数链将失败。 注意:
从 Transact-SQL 用户定义函数对托管代码的任何引用都将计入 32 级嵌套限制的一个级别。从托管代码内部调用的方法不根据此限制进行计数。 可以嵌套
那你为什么传递年度和月份 呢?
感觉没必要用两个函数,一个足够了。或者改为存储过程吧。
returns table
as
return
select @i*2 as num
go
/*
select * from fn_001(8)
-----------------------
num
-----------
16(1 行受影响)
*/
create function fn_002(@i int)
returns @t table (num2 int)
as
begin
insert @t
select num+@i from fn_001(8)
return
end
goselect * from fn_002(4)
/*
num2
-----------
20(1 行受影响)
*/表值函数可以嵌套
returns int
as
begin
return (@i*2)
end
gocreate function fn_002(@i int)
returns @t table (num2 int)
as
begin
insert @t
values(dbo.fn_001(8)+@i)
return
end
goselect * from fn_002(4)
---
20
随手测试下
我试了。但是,我现在想传递给函数的也是一个参数;
例如
create function fn_002(@i int)
returns @t table (num2 int)
as
begin
insert @t
select num+@i from fn_001(@i)
--而不是select num+@i from fn_001(8)
return
end
goselect * from fn_002(4)我还没试这个DEMO,我感觉不行
ALTER FUNCTION FUN1(@A INT)
RETURNS INT
AS
BEGIN
RETURN @A
END
GO
ALTER FUNCTION FUN2(@B INT,@A INT)
RETURNS INT
AS
BEGIN
RETURN @B+DBO.FUN1(@A)
END
GO
DECLARE @x INT
SET @x=1
DECLARE @y INT
SET @Y=2
SELECT DBO.FUN2(@x,@y)这样好像都行
set QUOTED_IDENTIFIER ON
goALTER function [dbo].[fn_001](@i int)
returns table
as
return
select @i*2 as numALTER function [dbo].[fn_002](@i int)
returns @t table (num2 int)
as
begin
insert @t
select num+@i from fn_001(@i)
return
endselect * from fn_002(4) 返回12,好使。但我的方法,却没有结果。
我的方法各自都是正确的,
不使用变量做参数,而是写明
insert @temp_AirCoupTop5Month
select top 5 takeoffID,arrivedID,airportCouple,num
from dbo.getAirportCoupleMonthStat ('V00989b3bV', '2009-07')
order by num desc 也是有返回结果的,
但是变成变量就没结果了。
郁闷
(
@deptID varchar(50),
@year varchar(4)
)
getAirportCoupleMonthStat ('V00989b3bV', '2009-07')
应该是这里的长度问题了,改成@year varchar(7)
你是正确的。对不起啊,各位,我太粗心了。因为还有一个年统计,用的是年。
粘贴错了。改成varchar(7)就好使了。谢谢。原来varchar 不是自动变长的?
另外十分,我给htl258和阿迪
我知道你们都旨在这份数。谢谢htl258和阿迪,教会我,让我知道我可以写小DEMO验证。
谢谢SQL77帮我解决问题。谢谢各位大侠朋友的关注。