SQL语法错误(仅在作为计划作业运行时)

时间:2017-10-31 14:50:16

标签: sql-server scheduled-tasks ssms

我刚刚创建了一个每天运行的预定作业,但是当作业通过调度程序运行时,我得到错误"'项目'附近的语法错误。 [SQLSTATE 42000](错误102)"。

仅当通过调度程序运行作业时才会发生这种情况,并且直接运行时可以正常工作。

DELETE FROM [NAVQueriesDB].[dbo].[New Items on Order] GO
  SET ANSI_WARNINGS OFF
  GO
 INSERT [NAVQueriesDB].[dbo].[New Items on Order] (
   [Updated Date], 
   [Location Code],
   [Order No_],
   [Item No_],
   [Item Description],
   [Variant Code],
   [Description 2],
   [Outstanding Qty_], 
   [Order Date],
   [Vendor_No],
   [Vendor_Name])

  SELECT  
    GETDATE() AS "Updated Date", 
    [Purch Line].[Location Code],
    [Purch Line].[Document No_],
    [Item].[No_],
    [Item].[Description],
    ISNULL([Purch Line].[Variant Code],'-') AS "Variant Code",
    ISNULL([Variant].[Description 2],'-') AS "Description 2", 
    CAST(SUM([Purch Line].[Outstanding Quantity]) as decimal(18,0)) AS "Outstanding Qty.",
    [Purch Line].[Order Date],
    [Purch Line].[Buy-from Vendor No_],
    [Vendor].[Name]
 FROM [Wings$Item] AS "Item"
  LEFT JOIN [Wings$Purchase Line] AS "Purch Line"
  ON [Item].[No_] = [Purch Line].[No_]

  LEFT JOIN [Wings$Item Variant] AS "Variant"
  ON [Item].[No_] = [Variant].[Item No_] AND [Purch Line].[Variant Code] = [Variant].[Code]

  LEFT JOIN [Wings$Vendor] AS "Vendor"
  ON [Purch Line].[Buy-from Vendor No_] = [Vendor].[No_]

  LEFT JOIN [Wings$Item Ledger Entry] AS "Item Ledger"
  ON [Item].[No_] = [Item Ledger].[Item No_]

  WHERE
    [Item].[No_] > '5618%'
    AND [Item].[No_] <> 'Z999999'
    AND [Item Ledger].[Item No_] IS NULL
 GROUP BY
  [Purch Line].[Location Code],
  [Purch Line].[Document No_], 
  [Item].[No_], 
  [Item].[Description],
  [Purch Line].[Variant Code], 
  [Variant].[Description 2],
  [Purch Line].[Outstanding Quantity], 
  [Purch Line].[Order Date],
  [Purch Line].[Buy-from Vendor No_], 
  [Vendor].[Name]
ORDER BY
  [Location Code],
  [Item].[No_],
  [Item].[Description],
  [Purch Line].[Variant Code]

1 个答案:

答案 0 :(得分:1)

使用双引号作为标识符是一个坏主意(IMO)。如果必须引用该标准,则标准为[](这也是为什么我在名单中避免使用空格以便我不必引用它们的原因。)

SQL代理默认设置为QUOTED_IDENTIFIER OFF。如果您将双引号更改为括号,那么这应该有效。或者(但不推荐),您可以将脚本的第一行包含为SET QUOTED_IDENTIFIER ON

例如,GETDATE() AS "Updated Date" ... GETDATE() AS [Updated Date]更好。 GETDATE() AS updated_date是最好的。