Run a proc on several different values of one parameter

Posted by WEFX on Stack Overflow See other posts from Stack Overflow or by WEFX
Published on 2014-06-11T21:08:25Z Indexed on 2014/06/11 21:24 UTC
Read the original article Hit count: 157

Filed under:
|

I have the following query that gets run within a proc. The function MyFunction returns a table, and this query joins on that table. This proc works great when a @MyArg value is supplied. However, I’m wondering if there’s a way to run this on all @MyArg values in the database. I’m sure there’s a way to do it within a loop, but I know that loops are generally to be avoided at the db layer.

I really just need to perform this for the sake of checking (and possibly cleansing) some bad data.

SELECT ColumnA, ColumnB, ColumnC
FROM
(       
    SELECT 
        a.ColumnA, 
        a.ColumnB, 
        a.ColumnC,
        ROW_NUMBER() 
            over(partition by a.ColumnD order by f.ColumnX) as RowNum
    FROM dbo.MyTableA AS a
        INNER JOIN dbo.MyFunction(@MyArg) f ON f.myID = a.myID
    WHERE (a.myBit = 1 OR a.myID = @MyArg)
) AS x
WHERE x.rownum = 1;

© Stack Overflow or respective owner

Related posts about sql

Related posts about sql-server-2008-r2