开发者

SQL Server: procedure don't output anything?

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
开发者_StackOverflow中文版    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


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

Remove the GOs.


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!


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜