【问题标题】:Select statement not working in sql server选择语句在sql server中不起作用
【发布时间】:2023-03-30 02:36:01
【问题描述】:

在 microsoft sql server 2005 经典的 asp 代码中,我使用以下代码调用 sql 查询:

selectHireResponseSQL = "
    SELECT HireResponseID, HireResponse, DateResponse, Comments, YearFileOpened
      , file_number, isCaseOpen, last_update, isConfidential, date_created
      , OurClient, TheirClient, ProjectName, DESCRIPTION, lawyer_lastname
      , lawyer_firstname, Conflicts.ConflictID
  FROM Hire_Response
       , Conflicts
       , Lawyers
 WHERE  Hire_Response.ConflictID = Conflicts.ConflictID
   AND Lawyers.lawyerID = Conflicts.lawyerID
   AND firmID IN (" & FirmIDString & ")
   AND HireID = " & HireID & "
   AND isStillaConflict = 1
 ORDER BY
       file_number
       , TheirClient
       , OurClient
       , lawyer_lastname
       , lawyer_firstname
"

以上不是存储过程。 此外,FirmIDString 变量是一个字符串,它是一个以逗号分隔的数字列表,例如 '1,2,3'

字符串被格式化后的一个例子是:

select HireResponseID, HireResponse, DateResponse, Comments, YearFileOpened, file_number, isCaseOpen, last_update, isConfidential, date_created, OurClient, TheirClient, ProjectName, description, lawyer_lastname, lawyer_firstname, Conflicts.ConflictID 
from Hire_Response, Conflicts, Lawyers 
WHERE Hire_Response.ConflictID=Conflicts.ConflictID AND Lawyers.lawyerID=Conflicts.lawyerID AND firmID IN (47,140,138,137,139) AND HireID = 594 AND isStillaConflict = 1 
ORDER BY file_number, TheirClient, OurClient, lawyer_lastname, lawyer_firstname 

现在我想把它变成一个存储过程。于是我把asp经典代码改成了

selectHireResponseSQL = "
               EXEC ps_selectHireResponseSQL '" & FirmIDString & "'," & HireID

而存储过程是:

SELECT HireResponseID, HireResponse, DateResponse, Comments, YearFileOpened
      , file_number, isCaseOpen, last_update, isConfidential, date_created
      , OurClient, TheirClient, ProjectName, DESCRIPTION, lawyer_lastname
      , lawyer_firstname, Conflicts.ConflictID
  FROM Hire_Response
       , Conflicts
       , Lawyers
 WHERE  Hire_Response.ConflictID = Conflicts.ConflictID
   AND Lawyers.lawyerID = Conflicts.lawyerID
   AND CHARINDEX(',' + CAST(firmID AS NVARCHAR) + ',',','+@FirmIDString + ',') >0
   AND HireID = @HireID
   AND isStillaConflict = 1
 ORDER BY
       file_number
       , TheirClient
       , OurClient
       , lawyer_lastname
       , lawyer_firstname

但是现在我根本没有得到任何记录(尽管代码似乎运行没有错误)。我知道我应该得到记录,因为如果我切换到非存储过程,我会得到记录。

有人知道这里出了什么问题吗?

【问题讨论】:

标签: sql sql-server sql-server-2005 select


【解决方案1】:

这是对查询的改进重写(这只修复了别名、连接和 nvarchar 没有大小):

select HireResponseID, HireResponse, DateResponse, Comments, YearFileOpened, file_number,
       isCaseOpen, last_update, isConfidential, date_created, OurClient, TheirClient,
       ProjectName, description, lawyer_lastname, lawyer_firstname, Conflicts.ConflictID 
from Conflics c join
     Hire_Response hr
     on hr.ConflictID=c.ConflictID join
     Lawyers l
     on l.lawyerID=c.lawyerID 
WHERE CHARINDEX(',' + CAST(firmID as varchar(30)) + ',', ',' + @FirmIDString + ',') > 0 
    AND HireID = @HireID
    AND isStillaConflict = 1 
ORDER BY file_number, TheirClient, OurClient, lawyer_lastname, lawyer_firstname;

这不会解决您的问题。如果您在格式化后打印出工作版本会很有帮助。

我最好的猜测是 @FirmIDString` 在 id 之间有逗号和空格。如果是这样,那么这应该工作:

WHERE CHARINDEX(', ' + CAST(firmID as varchar(30)) + ', ', ', ' + @FirmIDString + ', ') > 0 

【讨论】:

  • 我对正在做的查询(非存储过程)做了一个response.write,它是EXEC ps_selectHireResponseSQL '76',659,所以即使有1个数字,它仍然不起作用......当有多个数字,它被格式化为EXEC ps_selectHireResponseSQL '47,140,138,137,139',594
  • @omega 。 . .您确定在这种情况下原始查询返回行吗?如果是这样,您可以通过打印出有效的查询来编辑您的问题吗?
  • @omega 。 . .我有个主意。当您将@FirmId 声明为存储过程的参数时,您使用varchar 还是varchar(<some length>)?如果没有长度,则默认为 1,所有内容都被截断。
  • 我更新了帖子以显示示例。在代码中我声明为(@FirmIDString as nvarchar, @HireID as int)
  • 我相信你是对的,我不知道不声明长度会使长度为 1。当我给出长度为 11 时,它起作用了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-19
  • 2016-06-22
  • 1970-01-01
相关资源
最近更新 更多