0

I have written an inline TVF and I have this so far. I get this error:incorrect syntax near end. Can someone suggest a workaround for this. Can i actually do it thsi way syntactically, is it correct?

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

Thanks

2
  • 1
    That's not an inline-table-valued-function(ITVF) but a multi-statement table-valued function(MTVF) Commented Sep 21, 2016 at 15:13
  • And you are missing USER_ID in your select statment. Commented Sep 21, 2016 at 15:21

4 Answers 4

2

As Tim already mentioned in a comment this is not an inline table valued function but a multi-statement table valued function. The difference here is performance. The MTVF will generally perform even worse than scalar functions. However, a slight modification and this can easily be converted to 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
        )
Sign up to request clarification or add additional context in comments.

Comments

1

You need to add an alias to your derived query..

INSERT  INTO @tbl
                SELECT  *
                FROM    (SELECT 
                        ) AS T -- alias 
    ;

1 Comment

Thanks. My query works now. Just had to add as alias table name before end.
0

your outer most select is redundant and does not have a required alias for the inner select. Instead of adding an alias I would correct by removing:

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

But as Tim commented this is a Multi Statement Table Valued Function, but because you are simply doing a select you can make it In-Line like so:

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

Comments

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

1 Comment

You would be much better off going with the version of this query found in Sean's answer. His version is truly a Inline TVF and will perform much better.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.