rss
twitter
facebook

Home

SQL multi facts

declare
@A int = 1 ,@B int = 3 , @C int =20 , @D int =16

INSERTINTO @temp(A,B,C,D)
SELECT *
FROM
(
SELECT 1 As A, 2 AS B, 3 AS C, 4 AS D
UNION SELECT 1 As A, 2 AS B, null AS C, null AS D
UNION SELECT 1 As A, 2 AS B, 4 AS C, 5 AS D
UNION SELECT 1 As A, 3 AS B, 2 AS C, null AS D
UNION SELECT 1 As A, 3 AS B, 2 AS C, 7 AS D
UNION select 1 As A, 3 AS B, null AS C, null AS D
) A

SELECT
TOP 1 * FROM

@temp
WHERE
(A = @A AND B=@B AND C=@C AND D=@D) OR
(A = @A AND B=@B AND C=@C AND D IS NULL) OR
(A = @A AND B=@B AND C IS NULL AND D IS NULL) OR
(A = @A AND B IS NULL AND C IS NULL AND D IS NULL)
orderby D DESC,C DESC,B DESC,A DESC

0 comentários:

 
Powered by Blogger