1

这就是我创建的存储过程

create procedure p_fetchInspection 
                    @bIsByFreeText bit, @sFreeText nvarchar(max)
                    ,@bIsByTestedDate bit, @dTestedDateFrom datetime, @dTestedDateTo datetime
                    ,@bIsByLocation bit, @sLocation nvarchar(500)
                    ,@bIsByWorkOrder bit, @sWorkOrderFreeText nvarchar(max)
                    ,@bIsByTesterName bit, @sTesterName nvarchar(100)
                    ,@bIsByCorrosionTestingClass bit, @sCorrosionTestingClass nvarchar(10)
                    ,@bIsByTesterReceived bit,@bIsTesterReceived bit
                    ,@bIsByTesterUploaded bit,@bIsTesterUploaded bit
                    ,@bIsByAddedFromDevice bit,@bIsAddedFromDevice bit
                    ,@bIsByChecked bit,@bIsChecked bit
                    ,@bIsByRecordType bit,@bIsDefault bit,@bIsPoleNotSite bit,@bIsNewPole bit
as
BEGIN
--------------------
-- Declare
DECLARE @sSelect nVARCHAR(4000)
DECLARE @sFrom nVARCHAR(4000)
DECLARE @sWhere nVARCHAR(4000)
DECLARE @sOrder nVARCHAR(500)
DECLARE @sSql nVARCHAR(4000)
--------------------
SET @sSelect = 
'
select  
    [WorkOrder] = isnull(c.WorkOrderNo,'''')
    ,[Tester] = a.TesterName
                +
                ''
                ''
                +
                dbo.F_convertToSortableDateAndHourAndMinuteFormat2(a.TestedDate)
    ,[PoleId/Location] = a.PoleId
                        +
                        ''
                        ''
                        +
                        case when a.IsAddFromDevice = 0 then b.ExactDescription else a.LocationNameFromDevice end
    ,[Class] = a.CorrosionTestingClass
    ,[Remarks] = a.Remarks
    ,[IsTesterReceived] = case when a.IsTesterReceived = 1 then ''Yes'' else ''No'' end
    ,[IsTesterUploaded] = case when a.IsTesterUploaded = 1 then ''Yes'' else ''No'' end
    ,[IsAddedFromDevice] = case when a.IsAddFromDevice = 1 then ''Yes'' else ''No'' end
    ,a.SysId
'
SET @sFrom =
'
from 
    t_Inspection a
    left outer join t_asset b on a.AssetObject = b.SysId
    left outer join t_WorkOrder c on d.WorkOrderObject = c.Sysid
'
SET @sWhere = 
'
where 
    a.SysId = d.Sysid
'
------------------------------------------
if @bIsByFreeText = 1
    begin
        SET @sWhere = @sWhere + @sFreeText
    end
------------------------------------------
if @bIsByTestedDate = 1
    begin
        SET @sWhere = @sWhere +
'
    and (DATEDIFF(day, a.TestedDate, @TestedDateFrom) <= 0 
        AND DATEDIFF(day, a.TestedDate, @TestedDateTo) >= 0)
'
    end
------------------------------------------
if @bIsByLocation = 1
    begin
        SET @sWhere = @sWhere +
'
    and b.ExactDescription like ''%'' + @Location + ''%''
'
    end
------------------------------------------
if @bIsByWorkOrder = 1
    begin
        SET @sWhere = @sWhere + @sWorkOrderFreeText 

    end
------------------------------------------
if @bIsByTesterName = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.TesterName like ''%'' + @TesterName + ''%''
'
    end
------------------------------------------
if @bIsByCorrosionTestingClass = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.CorrosionTestingClass = @CorrosionTestingClass
'
    end
------------------------------------------
if @bIsByTesterReceived = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.IsTesterReceived = @IsTesterReceived
'
    end
------------------------------------------
if @bIsByTesterUploaded = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.IsTesterUploaded = @IsTesterUploaded
'
    end
------------------------------------------
if @bIsByAddedFromDevice = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.IsAddFromDevice = @IsAddedFromDevice
'
    end
------------------------------------------
if @bIsByChecked = 1
    if @bIsChecked = 1
    begin
        SET @sWhere = @sWhere +
'
    and a.CheckedBy <> ''''
'
    end
    if @bIsChecked = 0
    begin
        SET @sWhere = @sWhere +
'
    and a.CheckedBy = ''''
'
    end
------------------------------------------
if @bIsByRecordType = 1
begin
    DECLARE @sRecordTypeWhere NVARCHAR(max)
    set @sRecordTypeWhere = ''

    if @bIsDefault = 1
    begin
        SET @sRecordTypeWhere = @sRecordTypeWhere + case when @sRecordTypeWhere = '' then '' else ' or ' end + ' a.isDefault = 1'
    end
    if @bIsPoleNotSite = 1
    begin
        SET @sRecordTypeWhere = @sRecordTypeWhere + case when @sRecordTypeWhere = '' then '' else ' or ' end + ' a.isPoleNotSite = 1'
    end
    if @bIsNewPole = 1
    begin
        SET @sRecordTypeWhere = @sRecordTypeWhere + case when @sRecordTypeWhere = '' then '' else ' or ' end + ' a.isNewPole = 1'
    end

    SET @sWhere = @sWhere + ' and (' + replace(@sRecordTypeWhere, space(2), space(1)) + ')'
end
-----------------------------------------------------------------------
-- Order By Clause
SET @sOrder =
'
order by 
    c.WorkOrderNo
    , b.ExactDescription
    , a.PoleId
'
--------------------
-- Execute SQL
SET @sSql = @sSelect + @sFrom + @sWhere + @sOrder
EXEC sp_executesql 
        @sSql       
        , N'@TestedDateFrom DateTime, @TestedDateTo DateTime, @Location nvarchar(500), @TesterName nvarchar(100), @CorrosionTestingClass nvarchar(10), @IsTesterReceived bit, @IsTesterUploaded bit, @IsAddedFromDevice bit'
        , @TestedDateFrom = @dTestedDateFrom
        , @TestedDateTo = @dTestedDateTo 
        , @Location = @sLocation
        , @TesterName = @sTesterName
        , @CorrosionTestingClass = @sCorrosionTestingClass
        , @IsTesterReceived = @bIsTesterReceived
        , @IsTesterUploaded = @bIsTesterUploaded 
        , @IsAddedFromDevice = @bIsAddedFromDevice
END

但是得到错误“关键字'where'附近的语法不正确”,如果我删除第二个左外连接“左外连接t_WorkOrder c on d.WorkOrderObject = c.Sysid”,那么它可以工作。如果我在没有存储过程的情况下运行编码,只需在正常查询中运行它,也可以工作。为什么会这样?请帮我

4

1 回答 1

2

你只需要改变

DECLARE @sFrom nVARCHAR(100) 

DECLARE @sFrom nVARCHAR(500) 

或者试试这个——

CREATE PROCEDURE dbo.p_fetchInspection 

      @bIsByFreeText BIT
    , @sFreeText NVARCHAR(MAX)
    , @bIsByTestedDate BIT
    , @dTestedDateFrom DATETIME
    , @dTestedDateTo DATETIME
    , @bIsByLocation BIT
    , @sLocation NVARCHAR(500)
    , @bIsByWorkOrder BIT
    , @sWorkOrderFreeText NVARCHAR(MAX)
    , @bIsByTesterName BIT
    , @sTesterName NVARCHAR(100)
    , @bIsByCorrosionTestingClass BIT
    , @sCorrosionTestingClass NVARCHAR(10)
    , @bIsByTesterReceived BIT
    , @bIsTesterReceived BIT
    , @bIsByTesterUploaded BIT
    , @bIsTesterUploaded BIT
    , @bIsByAddedFromDevice BIT
    , @bIsAddedFromDevice BIT
    , @bIsByChecked BIT
    , @bIsChecked BIT
    , @bIsByRecordType BIT
    , @bIsDefault BIT
    , @bIsPoleNotSite BIT
    , @bIsNewPole BIT

AS BEGIN

DECLARE 
      @sSelect NVARCHAR(4000)
    , @sFrom NVARCHAR(1000)
    , @sWhere NVARCHAR(4000)
    , @sOrder NVARCHAR(500)
    , @sSql NVARCHAR(4000)

SELECT @sSelect = 
'SELECT 
      WorkOrder = ISNULL(c.WorkOrderNo, '''')
    , Tester = a.TesterName + CHAR(13) + dbo.F_convertToSortableDateAndHourAndMinuteFormat2(a.TestedDate)
    , [PoleId/Location] = a.PoleId + CHAR(13) + CASE WHEN a.IsAddFromDevice = 0 THEN b.ExactDescription ELSE a.LocationNameFromDevice END
    , Class = a.CorrosionTestingClass
    , Remarks = a.Remarks
    , IsTesterReceived = CASE WHEN a.IsTesterReceived = 1 THEN ''Yes'' ELSE ''No'' END
    , IsTesterUploaded = CASE WHEN a.IsTesterUploaded = 1 THEN ''Yes'' ELSE ''No'' END
    , IsAddedFromDevice = CASE WHEN a.IsAddFromDevice = 1 THEN ''Yes'' ELSE ''No'' END
    , a.SysId' + CHAR(13)
, @sFrom =
'FROM dbo.t_Inspection a
LEFT JOIN dbo.t_asset b ON a.AssetObject = b.SysId
LEFT JOIN dbo.t_WorkOrder c ON d.WorkOrderObject = c.Sysid' + CHAR(13)
, @sWhere = 
'WHERE a.SysId = d.Sysid' + CHAR(13)

IF @bIsByFreeText = 1
    SELECT @sWhere = @sWhere + @sFreeText

IF @bIsByTestedDate = 1
    SELECT @sWhere = @sWhere +
'
    AND (
          DATEDIFF(DAY, a.TestedDate, @TestedDateFrom) <= 0 
            AND 
          DATEDIFF(DAY, a.TestedDate, @TestedDateTo) >= 0
        )
'

IF @bIsByLocation = 1
    SELECT @sWhere = @sWhere +
'
    AND b.ExactDescription LIKE ''%'' + @Location + ''%''
'

IF @bIsByWorkOrder = 1
    SELECT @sWhere = @sWhere + @sWorkOrderFreeText 

IF @bIsByTesterName = 1
    SELECT @sWhere = @sWhere +
'
    AND a.TesterName LIKE ''%'' + @TesterName + ''%''
'

IF @bIsByCorrosionTestingClass = 1
    SELECT @sWhere = @sWhere +
'
    AND a.CorrosionTestingClass = @CorrosionTestingClass
'

IF @bIsByTesterReceived = 1
    SELECT @sWhere = @sWhere +
'
    AND a.IsTesterReceived = @IsTesterReceived
'

IF @bIsByTesterUploaded = 1
    SELECT @sWhere = @sWhere +
'
    AND a.IsTesterUploaded = @IsTesterUploaded
'

IF @bIsByAddedFromDevice = 1
    SELECT @sWhere = @sWhere +
'
    AND a.IsAddFromDevice = @IsAddedFromDevice
'

IF @bIsByChecked = 1 BEGIN

    SELECT @sWhere = @sWhere + '
    AND a.CheckedBy ' + 
        CASE WHEN @bIsChecked = 1 
            THEN '!=' 
            ELSE '=' 
        END + ' ''''
    '

END

IF @bIsByRecordType = 1 BEGIN

    DECLARE @sRecordTypeWhere NVARCHAR(MAX)
    SELECT @sRecordTypeWhere = ''

    IF @bIsDefault = 1
        SELECT @sRecordTypeWhere = @sRecordTypeWhere + 
            CASE WHEN @sRecordTypeWhere = '' 
                THEN '' 
                ELSE ' OR ' 
            END + ' a.isDefault = 1'

    IF @bIsPoleNotSite = 1
        SELECT @sRecordTypeWhere = @sRecordTypeWhere + 
            CASE WHEN @sRecordTypeWhere = '' 
                THEN '' 
                ELSE ' OR ' 
            END + ' a.isPoleNotSite = 1'

    IF @bIsNewPole = 1
        SELECT @sRecordTypeWhere = @sRecordTypeWhere + 
            CASE WHEN @sRecordTypeWhere = '' 
                THEN '' 
                ELSE ' OR ' 
            END + ' a.isNewPole = 1'

    SELECT @sWhere = @sWhere + ' AND (' + REPLACE(@sRecordTypeWhere, '  ', ' ') + ')'
END

SELECT @sOrder =
'ORDER BY
      c.WorkOrderNo
    , b.ExactDescription
    , a.PoleId
'

SET @sSql = @sSelect + @sFrom + @sWhere + @sOrder

PRINT @sSql

EXEC sys.sp_executesql 
          @sSql     
        , N'@TestedDateFrom DateTime, @TestedDateTo DateTime, @Location nvarchar(500), @TesterName nvarchar(100), @CorrosionTestingClass nvarchar(10), @IsTesterReceived bit, @IsTesterUploaded bit, @IsAddedFromDevice bit'
        , @TestedDateFrom = @dTestedDateFrom
        , @TestedDateTo = @dTestedDateTo 
        , @Location = @sLocation
        , @TesterName = @sTesterName
        , @CorrosionTestingClass = @sCorrosionTestingClass
        , @IsTesterReceived = @bIsTesterReceived
        , @IsTesterUploaded = @bIsTesterUploaded 
        , @IsAddedFromDevice = @bIsAddedFromDevice

END
于 2013-04-25T05:58:08.140 回答