Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

Complex SQL Server query from Python

I have a complex SQL Server query that I need to execute from python.

The SQL query looks like:

SELECT * 
FROM(
    SELECT DATEADD(HOUR, CONVERT(INT, SUBSTRING(hour_id, 2, 2)), CAST(FECHA as DATETIME)) as 'Time_', value
    FROM
        (
        SELECT * FROM [MyTable]
        ) as sel
    UNPIVOT(value for hour_id in (H01, H02, H03, H04, H05, H06, H07, H08, H09, H10, H11, H12, H13, H14, H15, H16, H17, H18, H19, H20, H21, H22, H23, H24) ) as unpvte
) as Q1;

And I created a function to run queries and turn the outcome to Pandas:

import pandas as pd
import numpy as np
import datetime
import pypyodbc

def execute_sql_command(database_name, server_name, user, password, SQLCommand, index_col=0):
        """
        Executed a query in an SQL Server database and returns a Pandas DataFrame
        :param database_name: Name of the Database
        :param SQLCommand: SQL command with no comments
        :param index_col: Index of the column
        :return: Pandas DataFrame
        """

        connection_string = 'DRIVER={SQL Server};Database=' + database_name \
                            + ';SERVER=' + server_name \
                            + ';UID=' + user \
                            + ';PWD=' + password

        connection = pypyodbc.connect(connection_string)
        cursor = connection.cursor()

        data = list()
        idx = list()
        cursor.execute(SQLCommand)
        hdr = [tuple[0] for tuple in cursor.description]

        hdr.pop(index_col)

        results = cursor.fetchone()
        if results is not None:
            results = list(results)

        while results:
            idx.append(results[index_col])  # add the index
            results.pop(index_col)  # remove the index from the row (we already accounted for it)
            data.append(results)  #
            results = cursor.fetchone()
            if results is not None:
                results = list(results)

        connection.close()

        data = np.array(data)
        df = pd.DataFrame(data=data, columns=hdr, index=idx)

        return df

I am getting this error:

pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax close to 'L'.")

My code works for simple queries like SELECT * FROM Table, but fails with this complex query. The query works in Microsoft SQL Server Management Studio.

Upvotes: 0

Views: 1694

Answers (1)

Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

Well, I figured out what the problem was.

I was reading the query from a .sql file. So the file must be in UTF-8 format.

Then compose the SQL command in a single line, this is remove the \n characters.

Upvotes: 1

Related Questions