1

I am trying to get sql server data and log path using SERVERPROPERTY.

When I run below stmt in SSMS, I get paths.

SELECT  SERVERPROPERTY('InstanceDefaultLogPath') ,SERVERPROPERTY('InstanceDefaultDataPath')

But when I try to run the same query from python using pyodbc. it gives me:

result = connsql.cursor().execute(query).fetchone()
pyodbc.ProgrammingError: ('ODBC SQL type -150 is not yet supported.  column-index=0  type=-150', 'HY106')

Any idea how to get the paths in python?

Code:

 def getSQLServerPath(self):
        try:
            print("Into function..")
            connsql = self.sql_connection()
            query = "SELECT SERVERPROPERTY('InstanceDefaultLogPath') ,SERVERPROPERTY('InstanceDefaultDataPath') "
            result = connsql.cursor().execute(query).fetchone()
            print(result)
            connsql.cursor().commit()
            connsql.close()
            # return path
        except Exception:
            logging.exception("getSQLServerPath function: Something went wrong.")

1 Answer 1

2

The error is actually telling you the problem here, it's the data type being returned that's the problem. The expression SERVERPROPERTY('InstanceDefaultLogPath') returns the data type sql_variant, which almost nothing apart from SQL Server supports. You can check this with the below SQL:

SELECT system_type_name
FROM sys.dm_exec_describe_first_result_set('SELECT SERVERPROPERTY(''InstanceDefaultLogPath'');',NULL, NULL);

As a result, you need to explicitly CONVERT the values to datatypes that ODBC does support. As these are both file paths, an nvarchar would seem the correct choice:

SELECT CONVERT(nvarchar(260),SERVERPROPERTY('InstanceDefaultLogPath')) AS InstanceDefaultLogPath,
       CONVERT(nvarchar(260),SERVERPROPERTY('InstanceDefaultDataPath')) AS InstanceDefaultDataPath;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.