views:

36

answers:

3

I'm trying to create a stored procedure which will fetch data from sys.databases and sys.database_files and combine this information into a single result set. Here's the code

CREATE PROCEDURE dbo.PROC_getDbInfo
AS
    SET NOCOUNT ON
    GO
    TRUNCATE TABLE dbo.dbinfo
    GO
    EXECUTE sp_msforeachdb 'insert into dbo.dbinfo 
            select  ''?'' as name,
                    type_desc, 
                    physical_name, 
                    state_desc, 
                    size * 1.0/128 as size_in_mb, 
                    max_size, 
                    growth * 1.0/128 as growth_in_mb, 
                    is_percent_growth,
                    is_read_only    
    from [?].sys.database_files'

    GO

    SELECT  @@SERVERNAME as instance_name,
        f.name,
        d.create_date,
        d.compatibility_level,
        d.collation_name,
        d.user_access_desc,
        d.state_desc,
        d.recovery_model_desc,
        d.page_verify_option_desc,
        d.log_reuse_wait_desc,
        f.type_desc, 
        f.physical_name, 
        f.state_desc, 
        f.size_in_mb, 
        f.max_size, 
        f.growth_in_mb, 
        f.is_percent_growth,
        f.is_read_only  
    FROM dbo.dbinfo AS f INNER JOIN
            sys.databases AS d
                ON f.name = d.name
    ORDER BY f.name
GO

When executing this procedure, I don't get any output. Why?

EXECUTE dbo.PROC_getDbInfo
+2  A: 

It's because all your sproc will actually be doing is:

SET NOCOUNT ON

As the next line after that is GO - that ends that batch, and so the sproc will have been created just with that SET NOCOUNT statement in!

AdaTheDev
+1  A: 

Because of GOs, your stored procedure only contains SET NOCOUNT ON.

Remove the GOs.

GSerg
+1  A: 

That's because the stored procedure will only run up to the first GO - the rest are separate statements. Your stored procedure will have been created containing just SET NOCOUNT ON - the rest will have been run just once; when you actually created the stored procedure - at which point they were treated as separate commands.

CodeByMoonlight