Feb-24-2023, 12:42 AM
Hi,
I'm trying to use Python to insert data into a SQL Server. pyodbc
I'm basically using python to export data from Server1 & Import into Server 2. with the same database / table names.
I hold the data in a dataframe called DATA
The export works & I can see the rows in my DATA dataframe.
When I try to import the data using the following code to Server 2 I receive an error
in Microsoft SQL Server Manager Studio I can execute & it works fine
Checking with shows that the data has been inserted, so I know I have write permissions.
I've also used this code on a 2nd project (Moving data from Server1 to Server2 (but a different table)) & it has worked successfully. My other project did not have a PK as a sequential number, so I'm assuming it has something to do with the PK.
Thanks in advance.
Steven
I'm trying to use Python to insert data into a SQL Server. pyodbc
I'm basically using python to export data from Server1 & Import into Server 2. with the same database / table names.
I hold the data in a dataframe called DATA
The export works & I can see the rows in my DATA dataframe.
When I try to import the data using the following code to Server 2 I receive an error
Error:ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]The INSERT permission was denied on the object 'dimEmployee', database 'MIS_DM_Common', schema 'conform'. (229) (SQLExecDirectW)")
My test python INSERT codefor index, row in DATA.iterrows(): cursor.execute("INSERT INTO MIS_DM_Common.conform.DimEmployee (EmployeeConfKey,SalaryNumberWWWGDWEmployeeKey,WWWEmployeeID,WWWEmployeeNumber,Surname,FirstName,Middlename,PreferredName,EmployeeTitle,CurrentActiveIndicator,InsertEventLogKey,UpdateEventLogKey) values('1','B','C','D','E','F','G','H','I','J','1','1','1')") connectionB.commit() cursor.close()I've checked that I have permission on SQL server.
in Microsoft SQL Server Manager Studio I can execute & it works fine
INSERT INTO MIS_DM_Common.conform.DimEmployee (EmployeeConfKey,SalaryNumber,WWWGDWEmployeeKey,WWWEmployeeID,WWWEmployeeNumber,Surname,FirstName,Middlename,PreferredName,EmployeeTitle,CurrentActiveIndicator,InsertEventLogKey,UpdateEventLogKey) values('1','B','C','D','E','F','G','H','I','J','1','1','1');(1 row affected)
Checking with shows that the data has been inserted, so I know I have write permissions.
SELECT * FROM MIS_DM_Common.conform.DimEmployeeI've also ensured that I've ran the following on SQL server, as EmployeeConfKey is a PK that I want to specify.
SET IDENTITY_INSERT MIS_DM_Common.conform.DimEmployee ONMy python connection works as I execute a COUNT query on Server2 before executing the INSERT & everything works.
I've also used this code on a 2nd project (Moving data from Server1 to Server2 (but a different table)) & it has worked successfully. My other project did not have a PK as a sequential number, so I'm assuming it has something to do with the PK.
Thanks in advance.
Steven