Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Stored procedure nested output

Posted on 2009-05-05
3
Medium Priority
?
542 Views
Last Modified: 2012-05-06
I have a stored procedure that I want to return all those fields in a table that are null throughout the records. Basically will show unused fields in a table. I am passing @TableName as an input parameter and intend to use the output as a simple column on a report in SSRS.

PROBLEM: I can't get the output of a stored procedure. It has a nested execute statement and is only returning one field.
SProc is below.

Grateful thanks in advanced.
USE [Metastorm]
GO
/****** Object:  StoredProcedure [dbo].[usp_ReportPackNullFields]    Script Date: 05/05/2009 11:02:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_ReportPackNullFields]
	@TableName varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
	DECLARE @query nvarchar(max);
	DECLARE @Colname nvarchar(100);
    DECLARE @column sysname;
    DECLARE columns_cursor CURSOR FOR
    	SELECT c.name
    	FROM sys.tables t JOIN sys.columns c ON t.object_id = c.object_id
    	WHERE t.name = @tablename AND c.is_nullable = 1;
    OPEN columns_cursor;
    FETCH NEXT FROM columns_cursor INTO @column;
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
    	SET @query = N'
    	DECLARE @c int;
		DECLARE @column nvarchar(100);
		SELECT @c = COUNT(*) FROM ' + @tablename + ' WHERE ' + @column + N' IS NOT NULL
    	IF (@c = 0)
			SELECT (''' + @column + N''') AS NullColumn;'
		EXECUTE (@query);
		FETCH NEXT FROM columns_cursor INTO @column;
    END
    CLOSE columns_cursor;
    DEALLOCATE columns_cursor;
    SET NOCOUNT OFF;
END;

Open in new window

0
Comment
Question by:BrightRaven
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Expert Comment

by:pivar
ID: 24303605
Hi,

You can fill a temptable and return it at the end of the sp.

/peter


USE [Metastorm]
GO
/****** Object:  StoredProcedure [dbo].[usp_ReportPackNullFields]    Script Date: 05/05/2009 11:02:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_ReportPackNullFields]
        @TableName varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
 
CREATE table #result (Nullcolumn varchar(100))
 
        DECLARE @query nvarchar(max);
        DECLARE @Colname nvarchar(100);
    DECLARE @column sysname;
    DECLARE columns_cursor CURSOR FOR
        SELECT c.name
        FROM sys.tables t JOIN sys.columns c ON t.object_id = c.object_id
        WHERE t.name = @tablename AND c.is_nullable = 1;
    OPEN columns_cursor;
    FETCH NEXT FROM columns_cursor INTO @column;
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @query = N'
        DECLARE @c int;
                DECLARE @column nvarchar(100);
                SELECT @c = COUNT(*) FROM ' + @tablename + ' WHERE ' + @column + N' IS NOT NULL
        IF (@c = 0)
                SELECT (''' + @column + N''') AS NullColumn;'
        INSERT INTO #result
                EXECUTE (@query);
        END
                FETCH NEXT FROM columns_cursor INTO @column;
    END
    CLOSE columns_cursor;
    DEALLOCATE columns_cursor;
 
    SELECT * FROM #result
 
    SET NOCOUNT OFF;
END;

Open in new window

0
 
LVL 22

Accepted Solution

by:
pivar earned 2000 total points
ID: 24303617
Sorry, a typo. Remove END after EXECUTE(@query), like this
USE [Metastorm]
GO
/****** Object:  StoredProcedure [dbo].[usp_ReportPackNullFields]    Script Date: 05/05/2009 11:02:35 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[usp_ReportPackNullFields]
        @TableName varchar(100)
AS
BEGIN
    SET NOCOUNT ON;
 
CREATE table #result (Nullcolumn varchar(100))
 
        DECLARE @query nvarchar(max);
        DECLARE @Colname nvarchar(100);
    DECLARE @column sysname;
    DECLARE columns_cursor CURSOR FOR
        SELECT c.name
        FROM sys.tables t JOIN sys.columns c ON t.object_id = c.object_id
        WHERE t.name = @tablename AND c.is_nullable = 1;
    OPEN columns_cursor;
    FETCH NEXT FROM columns_cursor INTO @column;
    WHILE (@@FETCH_STATUS = 0)
    BEGIN
        SET @query = N'
        DECLARE @c int;
                DECLARE @column nvarchar(100);
                SELECT @c = COUNT(*) FROM ' + @tablename + ' WHERE ' + @column + N' IS NOT NULL
        IF (@c = 0)
                SELECT (''' + @column + N''') AS NullColumn;'
        INSERT INTO #result
                EXECUTE (@query);
                FETCH NEXT FROM columns_cursor INTO @column;
    END
    CLOSE columns_cursor;
    DEALLOCATE columns_cursor;
 
    SELECT * FROM #result
 
    SET NOCOUNT OFF;
END;

Open in new window

0
 

Author Closing Comment

by:BrightRaven
ID: 31577965
Many thanks. Works a treat.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question