Return the answer of Query and Put the results in to a Table variable

Fred
Fred used Ask the Experts™
on
I am trying to create a table variable that will store  the output of one query, using the 4 tables

            SELECT COUNT(*)
            FROM      [dbo].[ADF_Path]
            WHERE PathActive = 'y'
            UNION
            SELECT COUNT(*)
            FROM  [dbo].[ADF_Course]
            WHERE CourseActive = 'y'
            UNION
            SELECT COUNT(*)
            FROM      [dbo].[ADF_Section]
            WHERE SectionActive = 'y'
            UNION

DECLARE @ACtiveGroup TABLE
                        
                  (
                        SELECT COUNT(*) FROM      [dbo].[ADF_Path]
                        WHERE PathActive = 'y'
                        UNION
                        SELECT COUNT(*)   FROM  [dbo].[ADF_Course]
                        WHERE CourseActive = 'y'
                        UNION
                        SELECT COUNT(*)  FROM      [dbo].[ADF_Section]
                        WHERE SectionActive = 'y'
                        UNION
                        SELECT COUNT (*) FROM      [dbo].[ADF_Event]
                        WHERE EventActive = 'y'
            )
            
            SELECT FROM  @ACtiveGroup

ADF_Path had 1 record ,Course has 7, Section has 110 and Event has 636 when I run the the query using the where clause. I
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
DECLARE @ActiveGroup TABLE (
    table_name varchar(50) NOT NULL,
    row_count int NULL
    )

INSERT INTO @ActiveGroup
SELECT 'ADF_Path', COUNT(*)
FROM  [dbo].[ADF_Path]
WHERE PathActive = 'y'
UNION ALL
SELECT 'ADF_Course', COUNT(*)
FROM  [dbo].[ADF_Course]
WHERE CourseActive = 'y'
UNION ALL
SELECT 'ADF_Section', COUNT(*)
FROM  [dbo].[ADF_Section]
WHERE SectionActive = 'y'
UNION ALL
SELECT 'ADF_Event', COUNT (*)
FROM  [dbo].[ADF_Event]
WHERE EventActive = 'y'

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial