为什么UPDATE .WRITE()仅在我使用[column] .WRITE()时才有效,而不是[table]。[column] .WRITE()?

时间:2013-07-08 07:50:30

标签: sql-server tsql

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

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

但是,如果我使用[dbo].[Table].[Column].WRITE(...)[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语句的通常方式非常一致。这个看似异常的约定是否有任何意义,显然不允许您明确指定表名和模式?

1 个答案:

答案 0 :(得分:6)

  

它看起来并不一致......

它实际上与SET clause的其余部分一致:

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中以全名或别名引用其他表格。已在UPDATESET之间确定要更新的表格。