I am sure Python gives you more information about the error than "pandas.io.sql.DatabaseEror: Execution failed on sql". It will tell you the exact line number in each module. I guess it will go wrong in "pd.read_sql_query()".
Best way is to add debug lines in your program to test whether variables contain what you expect.
First: somewhere in your program you must have logged in to the SQL Server database. Did that succeed? Can you test that with a simple query?
Second: just before executing the query: add statements to print the contents of the variables that matter.
Third: try to simplify the SQL. Do it without the variable substitution by SQL Server.
Best way is to add debug lines in your program to test whether variables contain what you expect.
First: somewhere in your program you must have logged in to the SQL Server database. Did that succeed? Can you test that with a simple query?
Second: just before executing the query: add statements to print the contents of the variables that matter.
userid, rolename = row[['User Id', 'Role Name']] print("userid = " + userid) print("rolename = " + rolename) Print("SQL = " + SQL) sql_query = pd.read_sql_query((SQL, (userid, rolename)), conn)What does it show?
Third: try to simplify the SQL. Do it without the variable substitution by SQL Server.
SQL = "select * from table_name where userid = 'John' and roleName = 'administrator'" ... sql_query = pd.read_sql_query(SQL, conn)(First fill in the correct table_name, userid and rolename.) What is the result of that?