0

在这个 sql server 存储过程查询

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 = @FirmID
  AND HireID IN @HireID
  AND isStillaConflict = 1
ORDER BY file_number,
         TheirClient,
         OurClient,
         lawyer_lastname,
         lawyer_firstname

该参数@HireID是一串以逗号分隔的整数(它周围没有括号)。我想检查HireID整数是否在@HireID字符串中。但我不知道如何解析这个。

有人可以帮忙吗?

谢谢

4

2 回答 2

0

试试这个——

DECLARE @HireID VARCHAR(100)
SELECT @HireID = '2,18'

;WITH cte AS 
(
    SELECT ID = t.c.value('.', 'INT')
    FROM (
        SELECT txml = CAST('<t>' + REPLACE(@HireID, ',', '</t><t>') + '</t>' AS XML)
    ) a
    CROSS APPLY txml.nodes('/t') AS t(c)
)
SELECT *
FROM Hire_Response
JOIN Conflicts ON Hire_Response.ConflictID = Conflicts.ConflictID
JOIN Lawyers ON Lawyers.lawyerID = Conflicts.lawyerID
WHERE firmID = @FirmID
    AND isStillaConflict = 1
    AND HireID in (SELECT ID FROM cte) 
ORDER BY file_number, TheirClient, OurClient, lawyer_lastname, lawyer_firstname
于 2013-05-29T05:39:00.930 回答
0

如果我理解您的问题,您想在@HireID 列表中查找 HireID 所在的行。如果 HireID 是一致的长度,并且列表是分隔的,那么你可以使用这个:

AND @HireID LIKE '%'+CAST(HireID AS VARCHAR(5))+'%'

你也可以使用 CHARINDEX:

AND CHARINDEX(HireID,@HireID) > 0

编辑:要考虑不一致的长度,您可以使用:

AND (@HireID LIKE '%'+CAST(HireID AS VARCHAR(5))+',%'
    OR @HireID LIKE '%,'+CAST(HireID AS VARCHAR(5))+'%')
于 2013-05-28T20:53:04.167 回答