在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
但是现在我根本没有得到任何记录(代码似乎运行没有错误)。我知道我应该得到记录,因为如果我切换到非存储过程,我会得到记录。
有谁知道这里有什么问题?
答案 0 :(得分: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`在ids之间有逗号和空格。如果是这样,那么这应该有效:
WHERE CHARINDEX(', ' + CAST(firmID as varchar(30)) + ', ', ', ' + @FirmIDString + ', ') > 0