0

在 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

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

有谁知道这里有什么问题?

4

1 回答 1

3

这是对查询的改进重写(这只修复了别名、连接和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 
于 2013-05-29T13:45:29.283 回答