• As mentioned, I wouldn't recommend this technique. It looks like you're trying to write one routine to get counts from any database. Or are you looking for any table? You could do something more like this:

    CREATE PROCEDURE GetCount @name VARCHAR(200)

    AS

    BEGIN

    WITH cteCount ( name, rcount )

    AS ( SELECT

    o.name

    , ddps.row_count

    FROM

    sys.indexes AS i

    INNER JOIN sys.objects AS o

    ON

    i.object_id = o.object_id

    INNER JOIN sys.dm_db_partition_stats AS ddps

    ON

    i.object_id = ddps.object_id

    AND i.index_id = ddps.index_id

    WHERE

    i.index_id < 2

    AND o.is_ms_shipped = 0

    )

    SELECT

    c.name

    , c.rcount

    FROM

    cteCount c

    WHERE

    c.name = @name;

    END;

    GetCount 'Articles'