我已经写了一个内联TVF,到目前为止我有这个。我收到此错误:结尾的语法不正确。有人可以为此建议解决方法。我是否可以通过语法方式实现,是否正确?
CREATE FUNCTION [hsip].[Question_Dtl_Table_Id]
(
-- Add the parameters for the function here
@cRegion CHAR(2) ='00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL
)
RETURNS @tbl TABLE ( Region CHAR(2),
State_Code CHAR(2),
FY NUMERIC(4,0),
REPORT_ID NUMERIC(2,0),
SECTION_ID NUMERIC(2,0),
SUBSECTION_ID NUMERIC(2,0),
DISPLAY_NUMBER NUMERIC(38,0),
QUESTION_NUMBER NUMERIC(38,0),
QUESTION_PART_NUMBER NUMERIC(38,0),
USER_ID VARCHAR(25))
AS
--RETURN
begin
INSERT INTO @tbl
select * from (SELECT qi.REGION, qi.STATE_CODE, qi.FY, qi.REPORT_ID, qi.SECTION_ID, qi.SUBSECTION_ID, qi.DISPLAY_NUMBER,
qi.QUESTION_NUMBER, qd.QUESTION_PART_NUMBER from
FY_ST_QUESTION_INFO qi
inner join
FY_ST_QUESTION_DETAIL qd
on qi.FY_ST_QUESTION_INFO_TABLE_ID= qd.FY_ST_QUESTION_INFO_TABLE_ID
where qi.region= @cRegion
and qi.fy= @nFY
and qi.REPORT_ID = @nREPORT_ID
and qi.SECTION_ID = @nSECTION_ID
and qi.SUBSECTION_ID = @nSUBSECTION_ID
and qi.DISPLAY_NUMBER =@nDISPLAY_NUMBER
and (qi.REPORTER_ID = @sUSER_ID or
qi.DELEGATE_ID= @sUSER_ID or
qi.SUB_DELEGATE_ID = @sUSER_ID));
return;
end
由于
答案 0 :(得分:2)
正如Tim在评论中已经提到的,这不是内联表值函数,而是多语句表值函数。这里的不同之处在于性能。 MTVF通常比标量函数表现更差。但是,稍作修改即可轻松转换为ITVF。
CREATE FUNCTION [hsip].[Question_Dtl_Table_Id]
(
-- Add the parameters for the function here
@cRegion CHAR(2) ='00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL
) RETURNS TABLE AS RETURN
SELECT qi.REGION
, qi.STATE_CODE
, qi.FY
, qi.REPORT_ID
, qi.SECTION_ID
, qi.SUBSECTION_ID
, qi.DISPLAY_NUMBER
, qi.QUESTION_NUMBER
, qd.QUESTION_PART_NUMBER
from FY_ST_QUESTION_INFO qi
inner join FY_ST_QUESTION_DETAIL qd on qi.FY_ST_QUESTION_INFO_TABLE_ID = qd.FY_ST_QUESTION_INFO_TABLE_ID
where qi.region = @cRegion
and qi.fy = @nFY
and qi.REPORT_ID = @nREPORT_ID
and qi.SECTION_ID = @nSECTION_ID
and qi.SUBSECTION_ID = @nSUBSECTION_ID
and qi.DISPLAY_NUMBER = @nDISPLAY_NUMBER
and
(
qi.REPORTER_ID = @sUSER_ID
or
qi.DELEGATE_ID = @sUSER_ID
or
qi.SUB_DELEGATE_ID = @sUSER_ID
)
答案 1 :(得分:1)
您需要为派生查询添加别名..
INSERT INTO @tbl
SELECT *
FROM (SELECT
) AS T -- alias
;
答案 2 :(得分:0)
您的最外层选择是多余的,并且没有内部选择所需的别名。我不会添加别名,而是删除:
CREATE FUNCTION [hsip].[Question_Dtl_Table_Id]
(
-- Add the parameters for the function here
@cRegion CHAR(2) ='00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL
)
RETURNS @tbl TABLE ( Region CHAR(2),
State_Code CHAR(2),
FY NUMERIC(4,0),
REPORT_ID NUMERIC(2,0),
SECTION_ID NUMERIC(2,0),
SUBSECTION_ID NUMERIC(2,0),
DISPLAY_NUMBER NUMERIC(38,0),
QUESTION_NUMBER NUMERIC(38,0),
QUESTION_PART_NUMBER NUMERIC(38,0),
USER_ID VARCHAR(25))
AS
--RETURN
begin
INSERT INTO @tbl
SELECT
qi.REGION, qi.STATE_CODE, qi.FY, qi.REPORT_ID, qi.SECTION_ID, qi.SUBSECTION_ID, qi.DISPLAY_NUMBER,
qi.QUESTION_NUMBER, qd.QUESTION_PART_NUMBER
from
FY_ST_QUESTION_INFO qi
inner join FY_ST_QUESTION_DETAIL qd
on qi.FY_ST_QUESTION_INFO_TABLE_ID = qd.FY_ST_QUESTION_INFO_TABLE_ID
where
qi.region= @cRegion
and qi.fy= @nFY
and qi.REPORT_ID = @nREPORT_ID
and qi.SECTION_ID = @nSECTION_ID
and qi.SUBSECTION_ID = @nSUBSECTION_ID
and qi.DISPLAY_NUMBER =@nDISPLAY_NUMBER
and (qi.REPORTER_ID = @sUSER_ID or
qi.DELEGATE_ID= @sUSER_ID or
qi.SUB_DELEGATE_ID = @sUSER_ID);
return;
end
但正如蒂姆评论的那样,这是一个多语句表值函数,但是因为你只是做一个选择,你可以像在线那样使它成为直线:
CREATE FUNCTION [hsip].[Question_Dtl_Table_Id]
(
-- Add the parameters for the function here
@cRegion CHAR(2) ='00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL
)
RETURNS TABLE
AS
RETURN
(
SELECT
qi.REGION, qi.STATE_CODE, qi.FY, qi.REPORT_ID, qi.SECTION_ID, qi.SUBSECTION_ID, qi.DISPLAY_NUMBER,
qi.QUESTION_NUMBER, qd.QUESTION_PART_NUMBER
from
FY_ST_QUESTION_INFO qi
inner join FY_ST_QUESTION_DETAIL qd
on qi.FY_ST_QUESTION_INFO_TABLE_ID = qd.FY_ST_QUESTION_INFO_TABLE_ID
where
qi.region= @cRegion
and qi.fy= @nFY
and qi.REPORT_ID = @nREPORT_ID
and qi.SECTION_ID = @nSECTION_ID
and qi.SUBSECTION_ID = @nSUBSECTION_ID
and qi.DISPLAY_NUMBER =@nDISPLAY_NUMBER
and (qi.REPORTER_ID = @sUSER_ID or
qi.DELEGATE_ID= @sUSER_ID or
qi.SUB_DELEGATE_ID = @sUSER_ID)
)
GO
答案 3 :(得分:0)
use [HSIP]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
--
ALTER FUNCTION [hsip].[Question_Dtl_Table_Id]
(
@cRegion CHAR(2) ='00',
@cState_Code CHAR(2) = '00',
@nFY NUMERIC(4,0) = 0,
@nREPORT_ID NUMERIC(2,0) = 0,
@nSECTION_ID NUMERIC(2,0) = 0,
@nSUBSECTION_ID NUMERIC(2,0) = 0,
@nDISPLAY_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_NUMBER NUMERIC(38,0) = 0,
@nQUESTION_PART_NUMBER NUMERIC(38,0) = 0,
@sUSER_ID VARCHAR(25) = NULL
)
RETURNS @tbl TABLE ( Region CHAR(2),
State_Code CHAR(2),
FY NUMERIC(4,0),
REPORT_ID NUMERIC(2,0),
SECTION_ID NUMERIC(2,0),
SUBSECTION_ID NUMERIC(2,0),
DISPLAY_NUMBER NUMERIC(38,0),
QUESTION_NUMBER NUMERIC(38,0),
QUESTION_PART_NUMBER NUMERIC(38,0))
AS
begin
INSERT INTO @tbl
select * from (SELECT qi.REGION, qi.STATE_CODE, qi.FY, qi.REPORT_ID, qi.SECTION_ID, qi.SUBSECTION_ID, qi.DISPLAY_NUMBER,
qi.QUESTION_NUMBER, qd.QUESTION_PART_NUMBER from
FY_ST_QUESTION_INFO qi
inner join
FY_ST_QUESTION_DETAIL qd
on qi.FY_ST_QUESTION_INFO_TABLE_ID= qd.FY_ST_QUESTION_INFO_TABLE_ID
where qi.region= @cRegion
and qi.fy= @nFY
and qi.REPORT_ID = @nREPORT_ID
and qi.SECTION_ID = @nSECTION_ID
and qi.SUBSECTION_ID = @nSUBSECTION_ID
and qi.DISPLAY_NUMBER =@nDISPLAY_NUMBER
and (qi.REPORTER_ID = @sUSER_ID or
qi.DELEGATE_ID= @sUSER_ID or
qi.SUB_DELEGATE_ID = @sUSER_ID)
and qd.QUESTION_PART_NUMBER =@nQUESTION_PART_NUMBER) as t;
--return @tbl;
return;
END
GO