且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在UPDATE命令的WHERE子句中使用多个条件。

更新时间:2021-06-29 04:05:28

了解更多SQL(这里是 WHERE 子句)。一般语法是

Read about SQL (here the WHERE clause). The general syntax is
WHERE <search_condition>

其中搜索条件可以是使用 AND OR NOT 的组合。



所以你的SQL语句应包含类似

where the search condition can be a combination using AND or OR and NOT.

So your SQL statement should contain something like

WHERE ap_No = 142 AND MNT = Jan AND YR = 2018



相关读取(此处为T-SQL):

WHERE(Transact -SQL)| Microsoft Docs [ ^ ]

搜索条件(Transact-SQL)| Microsoft Docs [ ^ ]


尝试:

Try:
UPDATE LeaveMaster SET Sap_No= @Sap_No, From_Date= @From_Date, To_Date=@To_Date, LeaveType=@LeaveType, Days=@Days, LeaveStatus=@LesveType WHERE Sap_No= " & CInt(TxtSapID.Text) & " AND MNT= " & MN & " AND YR= " & YRR



但是......不要这样做。

你清楚地知道如何使用参数提到查询,那么为什么要在那里抛出字符串连接并让自己对SQL注入开放?



永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。总是使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:


But ... don't do that.
You clearly know how to use parameterised queries, so why throw a string concatenation in there and leave yourself wide open to SQL Injection?

Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

哪个SQL看作三个单独的命令:

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?