请更正我的 SQL 语法

发布于 2024-10-31 09:30:02 字数 106 浏览 0 评论 0原文

我知道我的语法有问题... "select * from tblpayroll where empid = userid"

UserID 是一个变量...

I know that there's something wrong with my syntax... "select * from tblpayroll where empid = userid"

UserID is a variable...

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

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

发布评论

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

评论(2

痴者 2024-11-07 09:30:02

显然,这是你的 vb 变量。

select * from tblpayroll where empid = '" & userid & "' "

这样使用它,它会起作用

apparently, it's your vb variable.

select * from tblpayroll where empid = '" & userid & "' "

use it this way, and it'll work

花辞树 2024-11-07 09:30:02

文本“select * from tblpayroll where empid = userid”将通过原样发送到 SQL 后端,userid 部分将不被替代。因此,除非您有 userid 列,否则您可能会收到错误。即使您确实userid列,结果也不会是您所期望的。

您需要执行的操作取决于 userid 是数字值还是字符串值。对于数字,您可以使用:

"select * from tblpayroll where empid = " & CStr(userid)

这将首先将数字值转换为字符串并按原样检查。

对于字符串值,请使用:

"select * from tblpayroll where empid = '" & userid & "'"

这将简单地用引号将字符串括起来,以确保字符串比较有效。您需要意识到,如果 userid 没有经过某种方式的清理,这是一个坏主意 - 它可能会导致 SQL 注入攻击。解决这个问题的艺术超出了这个特定问题的范围,但值得牢记。

如果您的变量是数字但数据库字段是字符串该怎么办是另一回事。您可以使用 CStr 和零填充来完成此操作,但由于这种情况不太可能发生,因此我没有在此处记录它。

The text "select * from tblpayroll where empid = userid" will be sent through exactly as is to the SQL back end, the userid part will not be substituted. So, unless you have a userid column, you'll probably get an error. Even if you do have a userid column, the results won't be what you expect.

What you need to do depends on whether userid is a numeric or string value. For numerics, you can use:

"select * from tblpayroll where empid = " & CStr(userid)

This will first turn the numeric value into a string and check it as-is.

For string values, use:

"select * from tblpayroll where empid = '" & userid & "'"

This will simply surround the string with quotes to ensure a string comparison works. You need to be aware that this is a bad idea if userid has not been sanitised somehow - it may lead to SQL injection attacks. The art of fixing that is outside the scope of this particular question but it's worth keeping in mind.

What to do if your variable is numeric but the database field is a string is another matter. You can do it with CStr and zero-padding but, since it's an unlikely scenario, I haven't documented it here.

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