Created
July 1, 2013 14:56
-
-
Save emmanueltissera/5901559 to your computer and use it in GitHub Desktop.
Transact SQL for SQL 2008 and above to work with a integer array like structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALTER PROCEDURE getNodes | |
@isActive bit | |
AS | |
BEGIN | |
-- Declare a single column table to be used as a single dimension array | |
DECLARE @activeNodes TABLE (id int) | |
-- Only if the following condition is satisfied, 1070 will be added to the pseudo-array | |
IF @isActive = 1 | |
INSERT INTO @activeNodes(id) VALUES (1070) | |
-- 1067 and 1069 will be anyhow added in one statement (SQL 2008 and higher) to the pseudo-array | |
INSERT INTO @activeNodes(id) VALUES (1067), (1069) | |
-- Now an IN clause could be used, really useful in more complex queries | |
SELECT [nodeId], [xml] | |
FROM [cmsContentXml] | |
WHERE [nodeId] IN (SELECT id FROM @activeNodes) | |
END | |
GO |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment