日期名称转换仅在 SQL 代理中失败

datename conversion fails in SQL agent only

所以我在 SQL Server management studio 中通过代理执行存储过程。当我 运行 我的存储过程正常时,它 运行 没有错误返回,但是当它 运行 来自代理时它无法 运行.

我的存储过程:

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

当此 运行 来自我的代理时,它无法返回此错误:

但是如果我替换:

        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" 转换出现错误。请记住我传递的日期:

都是dd/MM/yyyy格式。

我想知道是否有人可以帮助我或建议我该怎么做,因为我已经坚持了几个小时。提前谢谢你。

您需要更改服务器默认语言或您的登录默认语言

SELECT @@LANGUAGE 

函数DATENAME取决于语言

SET LANGUAGE us_english

尝试在开始时捕获并在结束时重新设置它

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

SET LANGUAGE us_english 

--... your code...

SET LANGUAGE @language