SQL Select 语句不会返回字符字段,仅返回数字字段

发布于 2025-01-02 11:27:00 字数 610 浏览 0 评论 0原文

我今天一整天都在为这个问题绞尽脑汁。 我有以下 ASP 代码,它使用下拉框中的 Request.Querystring 输入 启动 select 语句。 querystrinch 确实显示在 ?= URL 中,但仅适用于 Microsoft SQL DB 中的数字列。我无法查找名称或简单的 3 个字符字段。

CODE:
If Request.QueryString("m") > 0 Then
filterID = Request.QueryString("m")
filterColmn = "imDegree"
  Else filterID = 0
End If

If filterID > 0 Then
     SQlQuery = "SELECT * FROM v_InternImport WHERE iID IN (SELECT iID FROM v_InternImport WHERE " &   filterColmn & " = " & filterID & ")"
  End If
End If

我知道这个选择语句作为其中的子选择语句,但我什至无法从我的数据库中获得直接返回。 select 语句引用了填充之前加载的主 asp 页面的相同视图,并且显示正常?

I have been racking my brain over this all day today.
I have the following ASP code that uses a Request.Querystring input from a dropdown box
to launch a select statement. The querystrinch does show in the ?= URL but will only work on
columns in the Microsoft SQL DB that are numeric. I cant lookup names or simple 3 character fields.

CODE:
If Request.QueryString("m") > 0 Then
filterID = Request.QueryString("m")
filterColmn = "imDegree"
  Else filterID = 0
End If

If filterID > 0 Then
     SQlQuery = "SELECT * FROM v_InternImport WHERE iID IN (SELECT iID FROM v_InternImport WHERE " &   filterColmn & " = " & filterID & ")"
  End If
End If

I understand that this select statement as a sub select stament in it but I cant even get a staight reuturn from my DB. The select statement references the same view that populates the main asp page that loads before and the shows fine?

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

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

发布评论

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

评论(1

云裳 2025-01-09 11:27:00

当您将字符串传递到 SQL Server 时,需要用单引号将其引起来。

当您传递数字时,不使用引号。

因此,当您说(总结)

SELECT * FROM table WHERE filterColumn = filterID

时,您应该发送一个数字。

匹配字符串:
SELECT * FROM table WHERE filterColumn = 'filterID'

这假设您已经解决了评论者提到的有关 filterID 变量中是否有值的任何其他问题。我衷心同意使用参数化查询的建议。

编辑:单引号放在双引号内。

SQlQuery = "SELECT * FROM v_InternImport 
WHERE iID IN (SELECT iID FROM v_InternImport 
WHERE " &   filterColmn & " = '" & filterID & "')"

When you pass a string to SQL Server, you need to surround it with single quotes.

When you pass a number, you don't use the quotes.

So, when you say (summarizing)

SELECT * FROM table WHERE filterColumn = filterID

you should be sending a number.

To match a string:
SELECT * FROM table WHERE filterColumn = 'filterID'

This assumes that you have solved any other problems mentioned by the commenters about whether you even have a value in the filterID variable. I heartly concur with the recommendation to use parameterized queries.

Edit: The single quotes go inside the double quotes.

SQlQuery = "SELECT * FROM v_InternImport 
WHERE iID IN (SELECT iID FROM v_InternImport 
WHERE " &   filterColmn & " = '" & filterID & "')"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文