4

我正在执行一个 UPDATE .WRITE() 语句,并发现它显然只有在你这样定义时才有效:

string sql = "UPDATE [dbo].[Table] SET [Column].WRITE(@data, @offset, @count) WHERE ...";
...
sqlCommand.ExecuteNonQuery();

但是,如果我使用[dbo].[Table].[Column].WRITE(...)or [Table].[Column].WRITE(...),则会引发异常:

Incorrect syntax near 'WRITE'.

Stack trace:
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   ...

这是为什么?它似乎与构建 SQL 语句的通常方式不太一致。这种看似特殊的约定是否有任何意义,显然不允许您明确指定表名和模式?

4

1 回答 1

7

好像不太一致...

它实际上与该SET条款的其余部分一致:

SET
    { column_name = { expression | DEFAULT | NULL }
      | { udt_column_name.{ { property_name = expression
                            | field_name = expression }
                            | method_name ( argument [ ,...n ] )
                          }
      }
      | column_name { .WRITE ( expression , @Offset , @Length ) }
      | @variable = expression
      | @variable = column = expression
      | column_name { += | -= | *= | /= | %= | &= | ^= | |= } expression
      | @variable { += | -= | *= | /= | %= | &= | ^= | |= } expression
      | @variable = column { += | -= | *= | /= | %= | &= | ^= | |= } expression
    } [ ,...n ] 

也就是说,在子句SET中,在赋值左侧指定表或模式是无效的(当然,您可以expression在右侧的 s 中按全名或别名引用其他表)。已在UPDATE和之间确定要更新的表SET

于 2013-07-08T07:55:44.860 回答