标量值函数

时间:2018-05-04 14:39:02

标签: sql sql-server

我对T-SQL有点新,我尝试使用此查询从字段中调用以下2个函数

    "select      tsk_nonconformitychecklist nonconformitychecklist,tsk_casemanagementchecklist casemanagementchecklist,dbo.r5o7_o7get_desc('EN','UCOD', tsk_objtype,'OBTP', null) equipmenttype_display,TSK_REVISION taskrev,t.tsk_org taskorganization,TSK_CODE task,TSK_CLASS taskclass,dbo.r5o7_o7get_desc
    ('EN','TASK', TSK_CODE,null, null) taskdesc,tsk_objclass equipmentclass,dbo.r5o7_o7get_desc('EN','UCOD',  tsk_type,'TSTP', null) taskplantype_display FROM r5tasks t 
    WHERE t.tsk_revrstatus = 'A'
                       AND COALESCE(t.tsk_notused, '-') <> '+'
                      AND COALESCE(t.tsk_isolationmethod, '-') = COALESCE(  NULL , '-')
                      AND COALESCE(t.tsk_jobplan, '+') = '-' 
    AND (COALESCE( NULL , 'false') = 'false' OR COALESCE(tsk_multipletrades, '-') = '-')
    AND (COALESCE( NULL , 'false') = 'false' OR tsk_planninglevel = 'TP')
    AND (COALESCE( NULL , 'false') = 'false' OR tsk_enableenhancedplanning = '+') 
    AND (COALESCE( NULL , 'false') = 'false' OR COALESCE(tsk_enableenhancedplanning,'-') = '-') and 
    (((select dbo.P_VALIDATEGL ( :parameter.afe , dbo.P_RETRIEVEGL(:parameter.afe  ,null,null,tsk_code))) not like UPPER( N'%#%') 
 and substring( :parameter.afe ,5,3) <>'INV'  ) or substring(:parameter.afe  ,5,3) ='INV') order by TS_CODE ASC"

我每次尝试时都会收到错误

  

Msg 102,Level 15,State 1,Line 11
  &#39;附近的语法不正确:&#39;。

功能是:

1)

[dbo].[P_VALIDATEGL] (@AFE nvarchar(30), @GL nvarchar(30))
returns nvarchar(400)
as
begin
    declare @return nvarchar(400)
    declare @count int
    declare @Err nvarchar(400)
    set @return=''

    --Function parameter check
    if isnull(@AFE,'###')='###'
     set @return='#AFE Missing'

    if isnull(@GL,'###')='###'
     set @return='#GL Code Missing (Part/Service not compatible)-Please provide a manual GL or choose another item'


    if @return=''
    --MAIN CODE
    begin
        declare @AFEClass nvarchar(30)
        declare @GLClass nvarchar(80)
        declare @GLClassDesc nvarchar(80)

        --Valid AFE??
        select @count=count(*) from R5PROJECTS where PRJ_CODE=@AFE and PRJ_STATUS='O' and isnull(PRJ_CLASS,'###')<>'###'
        if isnull(@count,0)=0
        begin
            set @return='#AFE is invalid'
            return @return
        end
        --Valid GL??
        select @count=count(*) from R5COSTCODES where CST_CODE=@GL and CST_NOTUSED='-' and isnull(CST_CLASS,'###')<>'###'
        if isnull(@count,0)=0
        begin
            set @return='#GL Code is invalid'
            return @return
        end

        select @AFEClass=PRJ_CLASS from r5projects where PRJ_CODE=@AFE 
        select @GLClass=CST_CLASS from R5COSTCODES where CST_CODE=@GL

        if @GLClass='C'
            set @GLClassDesc='Capex'
        if @GLClass='O'
            set @GLClassDesc='Opex'
        if @GLClass='F'
            set @GLClassDesc='Fixed Asset'
        if @GLClass='I'
            set @GLClassDesc='Inventory'
        if @GLClass='G'
            set @GLClassDesc='G&amp;A'



        if  isnull(@AFEClass,'###') <> 'INV' --#4565 
        begin 
            if  @GLClass='C' and isnull(@AFEClass,'###') <> 'CAPEX'
                set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass

            if  @GLClass='O' and isnull(@AFEClass,'###') <> 'OPEX'
                set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass

            if  @GLClass='F' and isnull(@AFEClass,'###') NOT IN ('OPEX','GENERAL')
                set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass

            if  @GLClass='I' and isnull(@AFEClass,'###') NOT IN ('OPEX','CAPEX') 
                set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass

            if  @GLClass='G' and isnull(@AFEClass,'###') <> ('GENERAL')
                set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class! '+@AFEClass
        end -- ##4565



        /*
        if isnull(@AFEClass,'###') = 'CAPEX' and @GLClass<>'C'
        begin   
            set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass
        end
        if isnull(@AFEClass,'###') <> 'CAPEX' and @GLClass in ('C','I')
            set @return='#The GL Code Class '+@GLClassDesc+' is not compatible with AFE class '+@AFEClass
            */

    end

    return @return
end"

2)

[dbo].[P_RETRIEVEGL] (@AFE nvarchar(30), @Part nvarchar(30), @Part-org nvarchar(15), @Task nvarchar(30))
returns nvarchar(400)
as
begin
    declare @return nvarchar(400)
    declare @count int
    set @return=''

    --Function parameter check
    if isnull(@AFE,'###')='###'
    set @return='#AFE Missing'
    if (
        (isnull(@Task,'###')='###' and isnull(@Part,'###')='###') 
        or 
        (isnull(@Task,'###')<>'###' and isnull(@Part,'###')<>'###')
        )
    set @return='#Must provide a part or a task'
    if  isnull(@Part,'###')<>'###' and isnull(@PartOrg,'###')='###'
    set @return='#Must provide a Part Org'

    if isnull(@part,'###')<>'###'
    begin
        select @count=count(*) from r5parts where par_code=@Part and par_org=@PartOrg
        if isnull(@count,0)=0
                set @return='#Part is invalid'
    end


    if @return=''
    --MAIN CODE
    begin
        declare @AFEClass nvarchar(30)
        declare @GL nvarchar(80)

        select @count=count(*) from R5PROJECTS where PRJ_CODE=@AFE and PRJ_STATUS='O'

        if isnull(@count,0)=0
        begin
            set @return='#AFE is invalid'
            return @return
        end

        select @AFEClass=PRJ_CLASS from r5projects where PRJ_CODE=@AFE 


        if isnull(@AFEClass,'###') not in ('CAPEX','INV','GENERAL','OPEX')
        begin
            set @return='#AFE must have a valid class'
            return @return
        end

        if isnull(@AFEClass,'###') = 'CAPEX'
        --Balance Sheet
        begin
            --PART SCENARIO
            if isnull(@Part,'###')<>'###'
            begin
                select @GL=PAR_UDFCHAR01 from R5PARTS where PAR_CODE=@Part and PAR_ORG=@PartOrg
                return @GL
            end

            --Task SCENARIO
            if isnull(@Task,'###')<>'###'
            begin
                select top 1 @GL=TSK_UDFCHAR01 from R5TASKS where TSK_CODE=@Task and TSK_REVRSTATUS='A' order by TSK_REVISION desc 
                return @GL
            end
        end

        if isnull(@AFEClass,'###') <> 'CAPEX'
        --Others
        begin
            --PART SCENARIO
            if isnull(@Part,'###')<>'###'
            begin
                select @GL=PAR_UDFCHAR02 from R5PARTS where PAR_CODE=@Part and PAR_ORG=@PartOrg
                return @GL
            end

            --Task SCENARIO
            if isnull(@Task,'###')<>'###'
            begin
                select top 1 @GL=TSK_UDFCHAR02 from R5TASKS where TSK_CODE=@Task and TSK_REVRSTATUS='A' order by TSK_REVISION desc 
                return @GL
            end
        end


    end

    return @return
   end

0 个答案:

没有答案