【问题标题】:Incorrect syntax near '(' and near '=''(' 和 '=' 附近的语法不正确
【发布时间】:2016-07-30 16:12:52
【问题描述】:

我要做的是更新一个条目,如果它不存在 - 创建一个新条目。

This 是我想要关注的。

我收到语法错误异常,我不知道出了什么问题。


这就是我制作桌子的方式:

CREATE TABLE [dbo].[Rides] (
    [phone]       VARCHAR (32)  NOT NULL,
    [destination] VARCHAR (50)  NOT NULL,
    [departure]   VARCHAR (50)  NOT NULL,
    [time]        DATETIME      NOT NULL,
    [comment]     NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([phone] ASC)
);

这是我的疑问:

SqlCommand command = connection.CreateCommand();
command.CommandText =
@"UPDATE Rides SET (destination=@Dest, departure=@Depart, time=@Time, comment=@Comment) WHERE phone=@UName
IF (@@ROWCOUNT=0)
    INSERT INTO Rides VALUES (destination=@Dest, departure=@Depart, time=@Time, comment=@Comment)";

command.Parameters.AddWithValue("@UName", entry.phone);
command.Parameters.AddWithValue("@Dest", entry.destinationID);
command.Parameters.AddWithValue("@Depart", entry.departureID);
command.Parameters.AddWithValue("@Time", entry.time.ToString("yyyy-MM-dd HH:mm:ss:fff"));
command.Parameters.AddWithValue("@Comment", entry.comment);
command.ExecuteNonQuery();

那是entry:

public struct Entry
{
    public string phone;
    public string destinationID;
    public string departureID;
    public DateTime time;
    public string comment;
}

这是我的错误:

System.Data.dll 中出现“System.Data.SqlClient.SqlException”类型的异常,但未在用户代码中处理

附加信息:'(' 附近的语法不正确。

“=”附近的语法不正确。

堆栈跟踪:

[SqlException (0x80131904): Incorrect syntax near '('.
Incorrect syntax near '='.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +2442126
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5736904
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +628
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +3731
   System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) +225
   System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds, Boolean describeParameterEncryptionRequest) +2026
   System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) +375
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +337
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +280
   RidesDatabaseAccessor.updateEntry(Entry entry) in c:\Users\Climax708\Documents\Programming\TrempLehayal\App_Code\RidesDatabaseAccessor.cs:145
   newride.Page_Load(Object sender, EventArgs e) in c:\Users\Climax708\Documents\Programming\TrempLehayal\newride.aspx.cs:75
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2952

【问题讨论】:

    标签: c# sql sql-server


    【解决方案1】:

    我做了以下更改:

    1. 我在您的两个语句之间添加了一个分号(UPDATEINSERT)。
    2. 我从您的 UPDATE 语句中删除了括号(它们不是 需要)
    3. 我更正了您的 INSERT 语句的语法。列名 必须在他们自己的括号声明中单独给出 在VALUES之前。

      command.CommandText = @"UPDATE Rides 
           SET destination=@Dest, departure=@Depart, time=@Time, comment=@Comment 
           WHERE phone=@UName; 
           IF (@@ROWCOUNT=0)
               INSERT INTO Rides (destination, departure, time, comment) 
               VALUES (@Dest, @Depart, @Time, @Comment)";
      

    【讨论】:

    • 虽然您正确地发现了问题,但我认为您应该告诉 OP 您所做的更改,否则他将冒着浪费时间寻找差异的风险。
    • 谢谢!我添加了一个解释。
    • 只是一点点修复。在插入的情况下,他还需要插入电话号码
    【解决方案2】:

    在您的更新中,您不得在列周围使用 () 来表示设置

    UPDATE Rides 
    SET destination = @Dest, 
        departure = @Depart, 
        time = @Time, 
        comment = @Comment 
    WHERE phone = @UName
    

    【讨论】:

      【解决方案3】:

      我觉得你应该试试

      @"UPDATE Rides SET destination=@Dest,department=@Depart,time=@Time,comment=@Comment WHERE phone=@UName

      插入您的更新查询

      【讨论】:

      • 这并不能解决 OP 的问题。如果不存在,他们想插入一个新行。
      猜你喜欢
      • 2015-12-12
      • 2013-12-16
      • 1970-01-01
      • 2018-08-14
      • 2011-03-11
      • 2014-02-16
      • 2017-01-11
      • 2020-02-06
      相关资源
      最近更新 更多