2016-09-29 43 views
1

因此,我通過SQL Server管理工作室中的代理執行存儲過程。當我正常運行我的存儲過程時,它運行良好,返回沒有錯誤,但是當它從代理運行時,它無法運行。日期名稱轉換僅在SQL代理中失敗

我的存儲過程:

USE [Sales] 
GO 
/****** Object: StoredProcedure [dbo].[forecastprojSPChartDetailsForPlaInsert] Script Date: 29/09/2016 12:50:32 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
-- ============================================= 
-- Author:  <Author,,Name> 
-- Create date: <Create Date,,> 
-- Description: <Description,,> 
-- ============================================= 
ALTER PROCEDURE [dbo].[forecastprojSPChartDetailsForPlaInsert] 
    -- Add the parameters for the stored procedure here 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 
    SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; 

       Select DISTINCT 

       MIN(S.Id) As Id 
       , S.DATE_DEL 
       ,'0.00' AS Installation 
       , SD.ProjectNo 
       , SHA.[Customer Name] 
       , P2.[Project Name] 
       , AD.ApplicationDate 
       , AD.IssueOfPaymentNotice 
       , AD.FinalDateForSubPayment 
       , AD.ValuationDate 
       , CF.Comment 
       , CF.QsNotified 
       , CF.AccountsNotified 
       , CF.CallBackDate 
       , CF.PaidAmount 
       , CF.PrevPaidOtherTextBox 
       , CF.ConfirmedFinalDate 
       , CF.ConfirmedPaidDate 
       , CF.RemovedCheck 
       , CF.RemovedCheckComment 
       , 'No' as SO 
       , CF.ApplicationDateChanged 

       ,datename(month,SC.DateOfSend) 
       ,datename(month,S.Date_Del) 
       ,datename(month,AD.ApplicationDate) 

       from SODDFD S 
       INNER JOIN SpecDetails SD ON SD.SrecId = S.Srecid 
       INNER JOIN SpecificationHeader SHA ON SD.QuoteNo = SHA.Qno 
       INNER JOIN Projects2 P2 ON P2.PID = SHA.PID 
       INNER JOIN SentContracts SC on SC.PID = P2.PID and SC.DD = '1' 
       INNER JOIN ApplicationDates AD on SHA.PID = AD.PID 

       AND 
       CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN datename(month,SC.DateOfSend) 
       ELSE datename(month,S.Date_Del) 
       END = datename(month,AD.ApplicationDate) 

       LEFT OUTER JOIN SentappDetails SA ON SA.SrecId = SD.SrecId 
       LEFT JOIN CashFlowProjectionTbl CF on AD.PID = CF.PID and AD.ApplicationDate = CF.ApplicationDate 

       where 
       S.PID in (Select distinct PID from ApplicationDates) AND 
       S.Id <> 0 AND 
       S.Id <> '' AND 
       Nominal = CASE WHEN SD.NewSellingPrice IS NULL THEN Nominal ELSE 'S001' END AND 
       s.DATE_DEL <> ''AND 
       (AD.ApplicationDate like '%/2016' or ConfirmedFinalDate <> '' or ConfirmedFinalDate IS NOT NULL) 
       and (convert(date,IssueOfPaymentNotice,103) >= '01/05/2016' or ConfirmedFinalDate <> '' or ConfirmedFinalDate IS NOT NULL) 
       --and (Cf.PaidAmount = '0' or Cf.PaidAmount = '0.00' or Cf.PaidAmount = '' or Cf.PaidAmount is NULL) 
       and (RemovedCheck = '' or RemovedCheck is NULL) 
       and convert(date,Ad.ApplicationDate,103) >= '01/06/2016' 


       GROUP BY S.PID, SD.NewSellingPrice, s.DATE_DEL, SA.CURR_APP, s.DEL_QTY, Comment, QsNotified, AccountsNotified, 
       CASE WHEN SD.NewSellingPrice IS NULL THEN S.Id END ,SD.ProjectNo, SHA.[Customer Name], P2.[Project Name], 
       AD.ApplicationDate, ad.IssueOfPaymentNotice, AD.FinalDateForSubPayment, CF.CallBackDate, 
       CF.PaidAmount , CF.PrevPaidOtherTextBox, CF.ConfirmedFinalDate, AD.ValuationDate, CF.ConfirmedPaidDate, CF.RemovedCheck, CF.RemovedCheckComment, CF.ApplicationDateChanged 
        ,datename(month,SC.DateOfSend) 
       ,datename(month,S.Date_Del) 
       ,datename(month,AD.ApplicationDate) 


    END 

當這個從我的代理程序運行失敗返回該錯誤:

enter image description here

但是如果我更換:

 AND 
     CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN datename(month,SC.DateOfSend) 
     ELSE datename(month,S.Date_Del) 
     END = datename(month,AD.ApplicationDate) 

有了:

 AND 
     CASE WHEN CONVERT(DATE,GETDATE(),103) > CONVERT(DATE,AD.ApplicationDate,103) THEN 'July' 
     ELSE 'July' 
     END = 'July' 

...它運行良好。

我唯一能想到的是當我的代理運行時,在我的「DATENAME」轉換中出現錯誤。銘記我傳遞的日期:

  • SC.DateOfSend
  • S.Date_Del
  • AD.ApplicationDate

都是DD/MM/YYYY格式。

我想知道是否有人可以幫助我或建議我該怎麼做,因爲我一直堅持這個小時。先謝謝你。

+0

您可以添加錯誤嗎? –

+0

代理運行後,服務器日誌中是否存在t-sql錯誤? –

+0

見編輯的問題。 @AlexK。 –

回答

1

您需要更改服務器的默認或登錄默認語言

SELECT @@LANGUAGE 

功能DATENAME取決於語言

SET LANGUAGE us_english 

嘗試開始和結束時捕捉到再次將其設置爲IS是

declare @language as varchar(100) 
SELECT @language= @@LANGUAGE 

SET LANGUAGE us_english 

--... your code... 

SET LANGUAGE @language 
+0

因此,我的SQL代理以不同於我的用戶的語言運行? –

+0

我用SET LANGUAGE英國人,非常感謝。 –

相關問題