在TVF上无法使用语法

时间:2014-07-11 16:00:08

标签: sql sql-server sql-server-2008 tsql

我对我的生活似乎无法在我的用户定义表值函数的BEGIN语句附近看到语法错误。有人可以告诉我这里缺少的东西......

我每次修改语法时都会遇到同样的问题。

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE FUNCTION F_I_SO_M2M_SOHEXFILT ()
RETURNS TABLE (
    fsono VARCHAR(256)
    ,fcustno VARCHAR(256)
    ,fcompany VARCHAR(256)
    ,fcity VARCHAR(256)
    ,fcustpono VARCHAR(256)
    ,fackdate VARCHAR(256)
    ,fcanc_dt VARCHAR(256)
    ,fccurid VARCHAR(256)
    ,fcfactor VARCHAR(256)
    ,fcfname VARCHAR(256)
    ,fcfromno VARCHAR(256)
    ,fcfromtype VARCHAR(256)
    ,fcontact VARCHAR(256)
    ,fclos_dt VARCHAR(256)
    ,fcountry VARCHAR(256)
    ,fcusrchr1 VARCHAR(256)
    ,fcusrchr2 VARCHAR(256)
    ,fcusrchr3 VARCHAR(256)
    ,fdcurdate VARCHAR(256)
    ,fdisrate VARCHAR(256)
    ,fdistno VARCHAR(256)
    ,fduedate VARCHAR(256)
    ,fduplicate VARCHAR(256)
    ,fdusrdate1 VARCHAR(256)
    ,festimator VARCHAR(256)
    ,ffax VARCHAR(256)
    ,ffob VARCHAR(256)
    ,fnextenum VARCHAR(256)
    ,fnextinum VARCHAR(256)
    ,fnusrqty1 VARCHAR(256)
    ,fnusrcur1 VARCHAR(256)
    ,forderdate VARCHAR(256)
    ,fordername VARCHAR(256)
    ,fordrevdt VARCHAR(256)
    ,fpaytype VARCHAR(256)
    ,fphone VARCHAR(256)
    ,fprint_dt VARCHAR(256)
    ,fprinted VARCHAR(256)
    ,fsalcompct VARCHAR(256)
    ,fsalecom VARCHAR(256)
    ,fshipvia VARCHAR(256)
    ,fshptoaddr VARCHAR(256)
    ,fsocoord VARCHAR(256)
    ,fsoldaddr VARCHAR(256)
    ,fsoldby VARCHAR(256)
    ,fsorev VARCHAR(256)
    ,fstate VARCHAR(256)
    ,fstatus VARCHAR(256)
    ,ftaxcode VARCHAR(256)
    ,ftaxrate VARCHAR(256)
    ,fterm VARCHAR(256)
    ,fterr VARCHAR(256)
    ,fzip VARCHAR(256)
    ,flprofprtd VARCHAR(256)
    ,flprofrqd VARCHAR(256)
    ,fndpstrcvd VARCHAR(256)
    ,fndpstrqd VARCHAR(256)
    ,fdeurodate VARCHAR(256)
    ,feurofctr VARCHAR(256)
    ,fsalescode VARCHAR(256)
    ,fusercode VARCHAR(256)
    ,fncancchrge VARCHAR(256)
    ,flchgpnd VARCHAR(256)
    ,fllasteco VARCHAR(256)
    ,timestamp_column VARCHAR(256)
    ,identity_column VARCHAR(256)
    ,fackmemo VARCHAR(256)
    ,fmstreet VARCHAR(256)
    ,fmusrmemo1 VARCHAR(256)
    ,fndbrmod VARCHAR(256)
    ,fccontkey VARCHAR(256)
    ,flcontract VARCHAR(256)
    ,fccommcode VARCHAR(256)
    ,fpriority VARCHAR(256)
    ,ContractNu VARCHAR(256)
    ,fbilladdr VARCHAR(256)
    ,OpportunNum VARCHAR(256)
    ,CreatedDate VARCHAR(256)
    ,ModifiedDate VARCHAR(256)
    ,OppCrType VARCHAR(256)
    ,QuoteNumber VARCHAR(256)
    )
AS
BEGIN
    DECLARE @intFlag INT = 1
    DECLARE @findtable TABLE (
        dechexval VARCHAR(max)
        ,Occupied BIT
        ,hexval NVARCHAR(max)
        )

    WHILE (@intFlag <= 65535)
    BEGIN
        --PRINT @intFlag
        INSERT INTO @findtable
        SELECT @intFlag
            ,''
            ,right(master.dbo.fn_varbintohexstr(cast(cast(min(@intFlag) AS INT) AS VARBINARY(8))), 4)
        SET @intFlag = @intFlag + 1
    END;
    --select * from @findtable
    SELECT som.*
    FROM m2mdata01.dbo.somast AS som
    INNER JOIN @findtable AS ft ON ft.hexval = right(som.fsono, 4)
    RETURN
END
GO

我几乎不知所措,我还有其他一些遵循相同语法的UDF,但无论出于何种原因,我都无法正确理解这一点。

2 个答案:

答案 0 :(得分:2)

尝试将您的功能更改为更像这样的功能。这比在这里循环更快。

CREATE FUNCTION F_I_SO_M2M_SOHEXFILT ()
RETURNS TABLE WITH SCHEMABINDING AS
 RETURN
WITH
    E1(N) AS (select 1 from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1))dt(n)),
    E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
    E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
    E6(N) as (select 1 from E4, E1), --100,000 rows
    cteTally(N) AS 
    (
        SELECT  ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E6
    ),
    FindTable as
    (
        select N as IntFlag
            , '' as Occupied
            , RIGHT(master.dbo.Fn_varbintohexstr(Cast(Cast(Min(N) AS INT) AS VARBINARY(8))), 4)
        from cteTally
        where N <= 65535
    )

    SELECT fsono,
    fcustno,
    fcompany ,
    fcity,
    fcustpono,
    fackdate ,
    fcanc_dt ,
    fccurid,
    fcfactor ,
    fcfname,
    fcfromno ,
    fcfromtype ,
    fcontact ,
    fclos_dt ,
    fcountry ,
    fcusrchr1,
    fcusrchr2,
    fcusrchr3,
    fdcurdate,
    fdisrate ,
    fdistno,
    fduedate ,
    fduplicate ,
    fdusrdate1 ,
    festimator ,
    ffax ,
    ffob ,
    fnextenum,
    fnextinum,
    fnusrqty1,
    fnusrcur1,
    forderdate ,
    fordername ,
    fordrevdt,
    fpaytype ,
    fphone ,
    fprint_dt,
    fprinted ,
    fsalcompct ,
    fsalecom ,
    fshipvia ,
    fshptoaddr ,
    fsocoord ,
    fsoldaddr,
    fsoldby,
    fsorev ,
    fstate ,
    fstatus,
    ftaxcode ,
    ftaxrate ,
    fterm,
    fterr,
    fzip ,
    flprofprtd ,
    flprofrqd,
    fndpstrcvd ,
    fndpstrqd,
    fdeurodate ,
    feurofctr,
    fsalescode ,
    fusercode,
    fncancchrge,
    flchgpnd ,
    fllasteco,
    timestamp_column ,
    identity_column,
    fackmemo ,
    fmstreet ,
    fmusrmemo1 ,
    fndbrmod ,
    fccontkey,
    flcontract ,
    fccommcode ,
    fpriority,
    ContractNu ,
    fbilladdr,
    OpportunNum,
    CreatedDate,
    ModifiedDate ,
    OppCrType,
    QuoteNumber
    FROM   m2mdata01.dbo.somast AS som
    INNER JOIN FindTable AS ft ON ft.hexval = RIGHT(som.fsono, 4)

答案 1 :(得分:1)

您需要在声明中为表格命名

CREATE FUNCTION F_I_SO_M2M_SOHEXFILT ()
RETURNS @t TABLE (
....

http://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx