【问题标题】:SQL Stored Procedure Incorrect syntax near '-'SQL 存储过程 '-' 附近的语法不正确
【发布时间】:2020-06-23 16:29:04
【问题描述】:

在 Microsoft SQL Server Management Studio 中执行我的存储过程时,我在更新 DATETIME2 字段时遇到错误:'-' 附近的语法不正确。

将存储过程插入同一张表时,一切正常。 我做错了什么?

非常感谢!

执行存储过程:

USE [DataAccountancyDB]
GO

DECLARE @return_value int,
        @Id int

SELECT  @Id = 3

EXEC    @return_value = [dbo].[spJournalPurchaseEntry_Update]
        @Id = @Id OUTPUT,
        @JournalPurchaseId = 1,
        @Date = 2019-12-30,
        @DocumentNumber = 2020007,
        @AccountingYearId = 3,
        @AccountingPeriodId = 2,
        @VATPeriodId = 1,
        @Comment = N'Test',
        @SupplierId = 1,
        @InvoiceAmount = 1000

SELECT  @Id as N'@Id'

SELECT  'Return Value' = @return_value

GO

我收到以下错误:

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '-'.

第 12 行:

@Date = 2019-12-30

.

仅供参考我的桌子

CREATE TABLE [dbo].[JournalPurchaseEntry]
(
    [Id] INT NOT NULL PRIMARY KEY IDENTITY, 
    [JournalPurchaseId] INT NOT NULL, 
    [Date] DATETIME2 NOT NULL, 
    [DocumentNumber] INT NOT NULL, 
    [AccountingYearId] INT NOT NULL, 
    [AccountingPeriodId] INT NOT NULL, 
    [VATPeriodId] INT NOT NULL, 
    [Comment] NVARCHAR(250) NULL, 
    [SupplierId] INT NULL, 
    [InvoiceAmount] MONEY NULL, 
    CONSTRAINT [FK_JournalPurchaseEntry_ToJournalPurchase] FOREIGN KEY ([JournalPurchaseId]) REFERENCES [JournalPurchase]([Id]),
    CONSTRAINT [FK_JournalPurchaseEntry_ToAccountingYear] FOREIGN KEY ([AccountingYearId]) REFERENCES [AccountingYear]([Id]),
    CONSTRAINT [FK_JournalPurchaseEntry_ToAccountingPeriod] FOREIGN KEY ([AccountingPeriodId]) REFERENCES [AccountingPeriod]([Id]),
    CONSTRAINT [FK_JournalPurchaseEntry_ToVATPeriod] FOREIGN KEY ([VATPeriodId]) REFERENCES [VATPeriod]([Id]),
    CONSTRAINT [FK_JournalPurchaseEntry_ToClient] FOREIGN KEY ([SupplierId]) REFERENCES [Supplier]([Id])
)

我的存储过程

CREATE PROCEDURE [dbo].[spJournalPurchaseEntry_Update]
    @Id INT OUTPUT,
    @JournalPurchaseId INT, 
    @Date DATETIME2, 
    @DocumentNumber INT, 
    @AccountingYearId INT, 
    @AccountingPeriodId INT, 
    @VATPeriodId INT, 
    @Comment NVARCHAR(250), 
    @SupplierId INT, 
    @InvoiceAmount MONEY
AS
BEGIN
    SET NOCOUNT ON;

    UPDATE dbo.JournalPurchaseEntry 
    SET
        JournalPurchaseId = @JournalPurchaseId, 
        [Date] = @Date, 
        DocumentNumber = @DocumentNumber, 
        AccountingYearId = @AccountingYearId, 
        AccountingPeriodId = @AccountingPeriodId, 
        VATPeriodId = @VATPeriodId, 
        Comment = @Comment, 
        SupplierId = @SupplierId, 
        InvoiceAmount = @InvoiceAmount

    WHERE [Id] = @Id;
END

【问题讨论】:

  • 错误告诉你去哪里看。第 9 行是 @Date = 2019-12-30, 这是一个包含 3 个整数的表达式。它应该是文字 varchar
  • @Date = '2019-12-30' 它会工作,即使它不是 varchar

标签: sql sql-server stored-procedures


【解决方案1】:

您需要在引号中传递参数

@Date = '2019-12-30'

【讨论】:

  • 或者更好地使用符合ANSI的YYYYMMDD并通过'20191230'
猜你喜欢
  • 2014-05-27
  • 2017-08-28
  • 2010-09-21
  • 1970-01-01
  • 2020-07-05
  • 2021-10-25
  • 1970-01-01
  • 2011-01-24
  • 2015-06-12
相关资源
最近更新 更多