• This may help.

    DECLARE @T TABLE

    (Col1 INT, Col2 INT, Col3 INT, Col4 INT

    ,Col5 INT, Col6 INT, Col7 INT, Col8 INT

    ,Col9 INT, Col10 INT, Col11 INT, Col12 INT)

    INSERT INTO @T (Col1, Col2, Col3, Col4)

    SELECT 1, 2, 3, 4

    UNION ALL SELECT 5, 6, 7, 8

    SELECT * FROM @T

    SELECT TOP 1 n

    FROM (SELECT TOP 1 * FROM @T) a

    CROSS APPLY (

    VALUES (1, Col1), (2, Col2)

    ,(3, Col3), (4, Col4)

    ,(5, Col5), (6, Col6)

    ,(7, Col7), (8, Col8)

    ,(9, Col9), (10, Col10)

    ,(11, Col11), (12, Col12)) b(n, Col)

    WHERE Col IS NULL

    ORDER BY n

    The number returned is the first column containing a NULL in the first row of the table.

    This is basically an UNPIVOT that is using the technique described here: http://www.sqlservercentral.com/articles/CROSS+APPLY+VALUES+UNPIVOT/91234/

    Somewhere in that article I believe it warns you that you need to CAST each column to the same (compatible) type if there are differences.


    My mantra: No loops! No CURSORs! No RBAR! Hoo-uh![/I]

    My thought question: Have you ever been told that your query runs too fast?

    My advice:
    INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
    The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.

    Need to UNPIVOT? Why not CROSS APPLY VALUES instead?[/url]
    Since random numbers are too important to be left to chance, let's generate some![/url]
    Learn to understand recursive CTEs by example.[/url]
    [url url=http://www.sqlservercentral.com/articles/St