2013-04-25 30 views
1

這是我創建的存儲過程SQL服務器 - 2留在存儲過程中的錯誤外連接

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 

,但得到的錯誤,如果我刪除第二個左外連接「關鍵字‘其中’附近有語法錯誤」 「左外連接t_WorkOrder c上d.WorkOrderObject = c.Sysid」,那麼它可以工作。 如果我運行沒有存儲過程的編碼,只需在普通查詢中運行它,然後也可以工作。 爲什麼這樣?請幫我

+0

嗯,我看不出有什麼表別名'd '屬於。但是你應該添加'PRINT @ sSql',看看它是否更清晰 – 2013-04-25 05:58:28

回答

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 
+0

Devart thx很多 – 2013-04-25 06:04:33

+0

請嘗試更新的查詢。 – Devart 2013-04-25 06:14:31

+0

不客氣@JackyTeh。 – Devart 2013-04-25 06:16:33