Problem with query result

  • I need to get the number of databases on one instance with Total Sapce Used, Total Space Alocated and Total Free Space

    The point is that in the second query result Q2: the result of the number of databases appear completely out of the reality 😀

    Can anyone explain me what am i doing wrong.

    Sorry :sick:

    Thanks and regards,

    JMSM 😉

    Q1: select count(name) from sys.sysdatabases

    result:

    89

    Q2: select @@servername as 'instance name', count(*) as 'Nº of DBs by Instance', sum(a.size) as 'espaço alocado (mb)', sum(a.used) as 'espaço utilizado (mb)', sum(a.size)-sum(a.used) as 'espaço livre (mb)'

    from db_name..tbl_name a, sys.sysdatabases b

    result:

    INamXYZ[font="Arial Black"]7921[/font]404052882307547517329813

    Q3: select @@servername as 'instance name', count(*) as 'Nº of DBs by Instance'

    from sys.sysdatabases

    result:

    INamXYZ 89

  • use sp_helpdb

  • select @@servername as 'instance name', count(*) as 'Nº of DBs by Instance', sum(a.size) as 'espaço alocado (mb)', sum(a.used) as 'espaço utilizado (mb)', sum(a.size)-sum(a.used) as 'espaço livre (mb)'

    from db_name..tbl_name a, sys.sysdatabases b

    Your count is high because your are joining with whatever db_name..tbl_name is producing 7921 records.

    quick fix, run two queries:

    select @@servername as 'instance name', (select count(name) from sys.sysdatabases) as 'Nº of DBs by Instance', sum(a.size) as 'espaço alocado (mb)', sum(a.used) as 'espaço utilizado (mb)', sum(a.size)-sum(a.used) as 'espaço livre (mb)'

    from db_name..tbl_name a, sys.sysdatabases b

  • Thanks a lot Jamie (everybody). Your answer was very useful to my work.

    Regards,

    JMSM 😉

  • JMSM (3/12/2009)


    Q2: select @@servername as 'instance name', count(*) as 'Nº of DBs by Instance', sum(a.size) as 'espaço alocado (mb)', sum(a.used) as 'espaço utilizado (mb)', sum(a.size)-sum(a.used) as 'espaço livre (mb)'

    from db_name..tbl_name a, sys.sysdatabases b

    You've got a cross join (Cartesian product) there. Each row of db_name..tbl_name is going to match to each and every row on sysdatabases.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply