I have a stored procedure as follows that I'd like to export its results in a table.
DECLARE
@ReportYearCmd VARCHAR(1000),
@CosCountCmd VARCHAR(1000),
@FranchiseCountCmd AS VARCHAR(1000),
@ProductCountCmd AS VARCHAR(1000);
WITH Validations AS (
SELECT TOP 1 * FROM [Handshake] WHERE [Status] = 'Loaded' AND [Update Time] = ( SELECT MAX( [Update Time] ) FROM Handshake )
)
UPDATE Validations
SET
@ReportYearCmd = CASE WHEN Report_Year_Count = 0 THEN NULL
ELSE 'SELECT DISTINCT [Report Year] AS [Report Year] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Report Year] WHERE [Report Year].[Report Year] = [Fact].[Report Year] );'
END,
@CosCountCmd = CASE WHEN COS_Count = 0 THEN NULL
ELSE 'SELECT DISTINCT [Country Code] AS [COS - Country Code] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [COS] WHERE [COS].[Country Code] = [Fact].[Country Code] );'
END,
@FranchiseCountCmd = CASE WHEN Franchise_Count = 0 THEN NULL
ELSE 'SELECT DISTINCT [Style Code] AS [Franchise - Style Code] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Franchise] WHERE [Franchise].[Style Code] = [Fact].[Style Code] );'
END,
@ProductCountCmd = CASE WHEN Product_Count = 0 THEN NULL
ELSE 'SELECT DISTINCT [Style Code] AS [Product - Style Code] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Product] WHERE [Product].[Style Code] = [Fact].[Style Code] );'
END,
[Status] = CASE
WHEN ( Report_Year_Count = 0 AND COS_Count = 0 AND Franchise_Count = 0 AND Product_Count = 0 ) THEN 'Good'
ELSE 'Rejects'
END
FROM [Validations]
OUTER APPLY (
SELECT
ISNULL( ( SELECT COUNT(*) FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Report Year] WHERE [Report Year].[Report Year] = [Fact].[Report Year] ) ), 0 ) AS [Report_Year_Count],
ISNULL( ( SELECT COUNT(*) FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [COS] WHERE [COS].[Country Code] = [Fact].[Country Code] ) ), 0 ) AS [COS_Count],
ISNULL( ( SELECT COUNT(*) FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Franchise] WHERE [Franchise].[Style Code] = [Fact].[Style Code] ) ), 0 ) AS [Franchise_Count],
ISNULL( ( SELECT COUNT(*) FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Product] WHERE [Product].[Style Code] = [Fact].[Style Code] ) ), 0 ) AS [Product_Count]
) AS [ValidationCounts];
-- Return validation sql statements --
SELECT
@ReportYearCmd AS ReportYearCmd,
@CosCountCmd AS CosCountCmd,
@FranchiseCountCmd AS FranchiseCountCmd,
@ProductCountCmd AS ProductCountCmd;
-- Insert actual SQL statament results into Rejects Table --
IF(OBJECT_ID('tempdb..#TEMP') IS NOT NULL)
BEGIN DROP TABLE #TEMP END
SELECT
ReportYearCmd, CosCountCmd, FranchiseCountCmd, ProductCountCmd
INTO #TEMP
FROM (
SELECT
@ReportYearCmd AS ReportYearCmd,
@CosCountCmd AS CosCountCmd,
@FranchiseCountCmd AS FranchiseCountCmd,
@ProductCountCmd AS ProductCountCmd
) AS [RejectsTable]
This only returns the validation statements from the first select statement, but I don't see a RejectTable showing the actual rejects from those SQL statements...
Currently, when I execute this SP I get something like:
ReportYearCmd CosCountCmd FranchiseCountCmd ProductCountCmd
NULL SELECT DISTINCT [Country Code] AS [COS - Country Code] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [COS] WHERE [COS].[Country Code] = [Fact].[Country Code] ) NULL SELECT DISTINCT [Style Code] AS [Product - Style Code] FROM [Fact] WHERE NOT EXISTS ( SELECT * FROM [Product] WHERE [Product].[Style Code] = [Fact].[Style Code] )
I'd like to return the actual result, i.e. if the Cmd is NOT NULL, as in the example above, return CosCountCmd and ProductCountCmd data:
COS - Country Code Product - Style Code
reject1 Rejectxy
reject2 Reject1234
NULL Reject567
I followed some of the examples here but none of them seem to work for me...