Hi All,
I have a query, all code below, which works but I am convinced there is a better way.
If you cun the code below and select *, you will see:
DBCON DB1 Crispin
DBCON DB2 Tarah
DBCON DB3 NULL
XXCON DB10 Crispin
XXCON DB20 Tarah
XXCON DB30 NULL
If @Hostname = 'Crispin', I want all the Crispin's, if Tarah then.... if @Hostname = 'bob' or fred etc (Any unknown) I want the NULL hostnames.
The attached Query works but makes two scans of the table and a NOT IN.
If there a better option.
Cheers,
Crispin
--drop TABLE #cp
--
--CREATE TABLE #CP (
-- PropertyName VARCHAR(100),
-- PropertyValue VARCHAR(100),
-- HostName VARCHAR(100)
--)
--
--INSERT INTO #CP VALUES ('DBCON', 'DB1', 'Crispin')
--INSERT INTO #CP VALUES ('DBCON', 'DB2', 'Tarah')
--INSERT INTO #CP VALUES ('DBCON', 'DB3', NULL)
--
--INSERT INTO #CP VALUES ('XXCON', 'DB10', 'Crispin')
--INSERT INTO #CP VALUES ('XXCON', 'DB20', 'Tarah')
--INSERT INTO #CP VALUES ('XXCON', 'DB30', NULL)
DECLARE
@HostName VARCHAR(100)
--SET @HostName = 'Crispin'
SET @HostName = 'Bob'
SELECT
*
FROM
#CP cp
WHERE
1 = CASE
WHEN (@Hostname NOT IN (SELECT Hostname FROM #CP WHERE HostName IS NOT NULL) AND cp.HostName IS NULL) THEN 1
WHEN cp.HostName = @HostName THEN 1
ELSE 0
END
Cheers,CrispinI can't die, there are too many people who still have to meet me!It's not a bug, SQL just misunderstood me!