我在网上找到了以下脚本的改编:
CREATE TABLE alphaCodes
(personID INTEGER NOT NULL,
codes CHAR(10) NOT NULL,
);
INSERT INTO alphaCodes
VALUES (1, '12300'), (1, '23400'), (1, '45623'),
(2, '99900'), (2, '23411'), (2, '78900'),
(3, '12300'), (3, '23400'), (3, '45699');
go
CREATE PROCEDURE GetPeopleWithCodes
(@d1 CHAR(10) = NULL, @d2 CHAR(10) = NULL,
@d3 CHAR(10) = NULL, @d4 CHAR(10) = NULL,
@d5 CHAR(10) = NULL)
AS BEGIN
--cte for the alphaCodes table
WITH Patient_Diagnosis (personID, codes)
AS (SELECT personID, codes FROM alphaCodes),
codeList (codePattern)
AS
--row constructor makes a table of the variables that will be passed into the stored procedure
(SELECT X.codePattern
FROM (VALUES (@d1), (@d2), (@d3), (@d4), (@d5))
AS X(codePattern)
WHERE X.codePattern IS NOT NULL)
SELECT DISTINCT personID
FROM Patient_Diagnosis AS PD1
WHERE NOT EXISTS
(SELECT *
FROM codeList
WHERE NOT EXISTS
(SELECT *
FROM Patient_Diagnosis AS PD2
WHERE PD1.personID = PD2.personID
AND PD2.codes LIKE codeList.codePattern));
END;
--for whatever reason, the % wildcard does not and and the _ has to be used
exec dbo.GetPeopleWithCodes '123___'
这个存储过程最多需要五个输入参数,并且基本上会返回一个包含许多参数的内连接。所以如果你跑
exec dbo.getpeoplewithcodes '234___'
1,2,3
将被退回。如果你跑
exec dbo.getpeoplewithCodes '234____','123___'
将返回1,3
。当我尝试这样做时,我使用动态 SQL 来创建一个表,该表具有各种连接alphaCodes
。上述方法要快很多,但有一个问题:我一生都无法完全理解它。我喜欢like
最后一个连接中的运算符,但我没有得到第一where not exists
个子查询。任何人都可以帮助解释发生了什么吗?你怎么能只有两个引用 alphaCodes 表并且让这个东西仍然有效?