pymssql Error: The table either does not exist or the current user does not have permissions on that table

43 Views Asked by At

I have the following Python script which is doing pretty well at extracting data from SQL Server via a Stored Procedure that is inputted to it:

import pymssql
import openpyxl
import os
import pandas as pd
from datetime import datetime

filename = "Finance_Sales"
extension = ".xlsx"
current_datetime = datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
full_filename = filename + "_" + current_datetime + "" + extension

os.chdir("S:\\Python\\Finance\\Projects")

# Establish a connection to the database
conn = pymssql.connect(server = 'Accounting_XPL', database = 'R')
cursor = conn.cursor(as_dict=True)

# Create a cursor
cursor = conn.cursor()

# Execute your stored procedure
cursor.callproc('[Outputs].[Fiance_Sales_Extended_sp]')

# Get column names from the description attribute of the cursor object

column_names = []
column_names = [column[0] for column in cursor.description]

print(type(column_names))
print(column_names)

# Convert the list in a DataFrame row
column_names_as_rows = pd.DataFrame(column_names).T

rows =[]
for row in cursor:
    rows.append(row)

#Iterate through result sets
while cursor.nextset():
    pass

print("Done")

df_r = pd.DataFrame(rows)
df_h = pd.DataFrame(column_names_as_rows)

# Concatenate the header and the result set
frames = [df_h, df_r]
df_s = pd.concat(frames)

#print(df_r)

df_s.to_excel(full_filename, index=False, header=False)

# Close the cursor and connection
cursor.close()
conn.close()

However, the issues seems to be that for certain Stored Procedures I get the following error:

pymssql._mssql.MSSQLDatabaseException: (7314, b'The OLE DB provider "MSOLEDBSQL" for linked server "Sales" does not contain the table ""Melvis"."Finance"."Pre_2023_sales_v"". The table either does not exist or the current user does not have permissions on that table.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n')

I think inside this particular SP (I don't have access to it) has a reference to the view "Pre_2023_sales_v" on the linked server "Sales" which links to "Melvis.Finance.Pre_2023_sales_v". I checked that the view does indeed exists so it must then be a permission issue.

I'm not sure whether it's the case that using Windows Authentication in the Python script is causing the issue.

I am able to query the "Pre_2023_sales_v" view on the linked server using SSMS via Windows Authentication.

Thanks

0

There are 0 best solutions below