This is a simple one but I can't seem to figure it out.
I have two parameters, 1 and 2. If one parameter is passed a null value, use the other parameter in there WHERE clause.
Ex:
SELECT ...
WHERE CASE
WHEN @Parameter1 IS NULL
THEN Field2 = @Parameter2
WHEN @Parameter2 IS NULL
THEN Field1 = @Parameter1
END
Run Code Online (Sandbox Code Playgroud)
I know this is something simple or maybe I'm using my logic wrong. Any help or direction is appreciated.
正如标题所述,我有一个递归 CTE,当我更改 WHERE 子句中的运算符时,即使只有两行数据,它也会爆炸。
CREATE TABLE #Recursion
(Parent varchar(10), Child varchar(10), TopDate datetime)
INSERT INTO #Recursion (Parent, Child, TopDate)
VALUES
('00003137', '00003137', '2018-08-31'),
('04536347', '00003137', '2017-02-28'),
('05458040', '05458040', '9999-12-31'),
('00269705',' 05458040',' 9999-12-31')
;WITH
Parent AS
( SELECT parent
, child
, 1 as sort
, TopDate
FROM #Recursion
WHERE parent = child
UNION ALL
SELECT s.parent
, d.child
, d.sort + 1
, s.TopDate
FROM Parent as d
JOIN #Recursion s
ON s.child= d.parent
WHERE s.TopDate < d.TopDate
)
SELECT …Run Code Online (Sandbox Code Playgroud)