Python Forum
Get database used data space from pyodbc - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get database used data space from pyodbc (/thread-28991.html)



Get database used data space from pyodbc - susja - Aug-13-2020

I need to get 'used data space' of database. I decided to use sp_spaceused. I am using python 3 and pyodbc My code:
        query = '''
CREATE TABLE SpaceUsed (
     TableName sysname
    ,NumRows BIGINT
    ,ReservedSpace VARCHAR(50)
    ,DataSpace VARCHAR(50)
    ,IndexSize VARCHAR(50)
    ,UnusedSpace VARCHAR(50)
    ) 
INSERT INTO SpaceUsed 
EXEC sp_msforeachtable @command1=\'exec sp_spaceused ''?''\'

SELECT 
sum(CONVERT(int,REPLACE(DataSpace,' KB','')) ) as SUM
FROM SpaceUsed
    '''

    db = pyodbc.connect(dsn2)
    curs = db.cursor()
    curs.execute(query)
    actual_size = curs.fetchall()[0][0]
It throws an error:
Error:
actual_size = curs.fetchall()[0][0] pyodbc.ProgrammingError: No results. Previous SQL was not a query.
Questions:

1. How to fix it?
2. What select statement could I use instead of stored procedure to get 'used data space'?
Thanks


RE: Get database used data space from pyodbc - susja - Aug-14-2020

Well ... please disregard my question.
I realized that I have check just simply the database size and don't need to run stored procedure.
Hence it's not relevant for me anymore.
Thanks for advice anyway ..