datename转换仅在SQL代理中失败

时间:2016-09-29 13:26:07

标签: sql sql-server agent

所以我通过SQL Server管理工作室中的代理执行存储过程。当我正常运行我的存储过程时,它运行正常返回没有错误,但是当它从代理程序运行时它无法运行。

我的存储过程:

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格式。

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

1 个答案:

答案 0 :(得分:1)

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

SELECT @@LANGUAGE 

功能DATENAME取决于语言

SET LANGUAGE us_english

尝试在开始和结束时再次设置它,原样是

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

SET LANGUAGE us_english 

--... your code...

SET LANGUAGE @language