msdn 主题中的此 UPDATE 表语句是否正确

发布于 2024-10-20 04:25:40 字数 511 浏览 1 评论 0原文

我在以下 msdn 主题中看到了这种类型的 UPDATE 语句(就像插入语句一样):

http: //msdn.microsoft.com/en-us/library/aa0416cz.aspx#Y2461

更新声明:-

adapter.UpdateCommand = New SqlCommand("UPDATE Customers " &
  "(CustomerID, CompanyName) VALUES(@CustomerID, @CompanyName) " & _
  "WHERE CustomerID = @oldCustomerID AND CompanyName = " &
  "@oldCompanyName", connection)

此声明正确与否?

我尝试执行它,但它给出了语法错误。

I have seen this type of UPDATE statement (just like insert statement) in the following msdn topic:

http://msdn.microsoft.com/en-us/library/aa0416cz.aspx#Y2461

UPDATE statement:-

adapter.UpdateCommand = New SqlCommand("UPDATE Customers " &
  "(CustomerID, CompanyName) VALUES(@CustomerID, @CompanyName) " & _
  "WHERE CustomerID = @oldCustomerID AND CompanyName = " &
  "@oldCompanyName", connection)

Is this statement correct or not?

I have tried executing it and it is giving syntax errors.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眸中客 2024-10-27 04:25:40

不,它应该是:

UPDATE Customers
SET 
CustomerID = @CustomerID,
CompanyName = @CompanyName
WHERE
CustomerID = @oldCustomerID AND
CompanyName = @oldCompanyName

或者为了完成您的示例代码,它应该是:

adapter.UpdateCommand = New SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName", connection)

这是为您和这种情况提供的另一个参考: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx

No, it should be:

UPDATE Customers
SET 
CustomerID = @CustomerID,
CompanyName = @CompanyName
WHERE
CustomerID = @oldCustomerID AND
CompanyName = @oldCompanyName

Or to be complete with your sample code, it should be:

adapter.UpdateCommand = New SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName", connection)

Here is another reference for you and this situation: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldataadapter.updatecommand.aspx

晨与橙与城 2024-10-27 04:25:40

该 SQL 对于 INSERT INTO 似乎是正确的,但对于 UPDATE 则不是正确的 它应该是这样的:

adapter.UpdateCommand = New SqlCommand("UPDATE Customers" & _
    " SET CustomerID = @CustomerID, CompanyName = @CompanyName)" & _
    " WHERE CustomerID = @oldCustomerID AND CompanyName =" & _
    " @oldCompanyName", connection)

That SQL 就是所谓的参数化,因此这使得这段代码(在片段)非常重要:

adapter.UpdateCommand.Parameters.Add( _
  "@CustomerID", SqlDbType.NChar, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
  "@CompanyName", SqlDbType.NVarChar, 30, "CompanyName")

' Pass the original values to the WHERE clause parameters.
Dim parameter As SqlParameter = dataSet.UpdateCommand.Parameters.Add( _
  "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
parameter = adapter.UpdateCommand.Parameters.Add( _
  "@oldCompanyName", SqlDbType.NVarChar, 30, "CompanyName")
parameter.SourceVersion = DataRowVersion.Original

That SQL appears to be correct for an INSERT INTO but not for an UPDATE It should read:

adapter.UpdateCommand = New SqlCommand("UPDATE Customers" & _
    " SET CustomerID = @CustomerID, CompanyName = @CompanyName)" & _
    " WHERE CustomerID = @oldCustomerID AND CompanyName =" & _
    " @oldCompanyName", connection)

That SQL is what one would call paramaterized, so that makes this code (lower in the snippet) very important:

adapter.UpdateCommand.Parameters.Add( _
  "@CustomerID", SqlDbType.NChar, 5, "CustomerID")
adapter.UpdateCommand.Parameters.Add( _
  "@CompanyName", SqlDbType.NVarChar, 30, "CompanyName")

' Pass the original values to the WHERE clause parameters.
Dim parameter As SqlParameter = dataSet.UpdateCommand.Parameters.Add( _
  "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID")
parameter.SourceVersion = DataRowVersion.Original
parameter = adapter.UpdateCommand.Parameters.Add( _
  "@oldCompanyName", SqlDbType.NVarChar, 30, "CompanyName")
parameter.SourceVersion = DataRowVersion.Original
囚我心虐我身 2024-10-27 04:25:40

据我所知,语法无效。以下给出'('附近的语法不正确。

我建议按照丹的回答

CREATE TABLE Customers
(
CustomerID INT, 
CompanyName VARCHAR(10)
)

DECLARE 
@CustomerID INT, 
@CompanyName VARCHAR(10),
@oldCustomerID INT,
@oldCompanyName VARCHAR(10)

UPDATE Customers  (CustomerID, CompanyName)
VALUES(@CustomerID, @CompanyName)
WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName

As far as I can see the syntax is not valid. The following gives Incorrect syntax near '('.

I suggest changing it as per Dan's answer.

CREATE TABLE Customers
(
CustomerID INT, 
CompanyName VARCHAR(10)
)

DECLARE 
@CustomerID INT, 
@CompanyName VARCHAR(10),
@oldCustomerID INT,
@oldCompanyName VARCHAR(10)

UPDATE Customers  (CustomerID, CompanyName)
VALUES(@CustomerID, @CompanyName)
WHERE CustomerID = @oldCustomerID AND CompanyName = @oldCompanyName
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文