I cannot figure out how to add the last condition. This is part of dynamic code cursor. Cursor consists of a list of table columns. I am comparing values in two tables that are identical in their structure, joining them on PK, and checking one column at a time where the values are not matching. What I need in my last condition is:
- if @columnName is 'untCode'
- then I want to exclude those rows where TMC.untCode is null
- otherwise this condition does not apply
DECLARE @columnName CHAR(10) = 'untcode';
SELECT MC.@columnName, TMC.@columnName
FROM dbo.firstable AS TMC
INNER JOIN dbo.secondtable AS MC
ON MC.ID = TMC.ID
AND MC.Code = TMC.Code
WHERE MC.ID = 123
AND TMC.@columnName<> MC.@columnName
--How do I add this??
AND TMC.@columnName CASE
WHEN @columnName='untCode' THEN not null
ELSE --What???
END ;
Appreciate the help
CodePudding user response:
As you said it:
--How do I add this??
AND NOT (@columnName='untCode' AND TMC.untCode is NULL)
or with a slight change:
--How do I add this??
AND (@columnName<>'untCode' OR TMC.untCode is NOT NULL)
But you should correct AND TMC.@columnName<> MC.@columnName
as @Larnu said; did you mean AND TMC.columnName=MC.columnName`?