这就是我创建的存储过程
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”,那么它可以工作。如果我在没有存储过程的情况下运行编码,只需在正常查询中运行它,也可以工作。为什么会这样?请帮我