Execute multiple SQL queries in one statement using Python - python-3.x

I am new to Python and I want to execute multiple SQL queries in one statement using python, but I am not able to find the appropriate way to do so.
I wrote following code but its throwing an error as " DatabaseError: ORA-00933: SQL command not properly ended."
import cx_Oracle;
SQLQuery = "select x from xyz where p= 'sn'; select * from abs where a ='qw';"
connection = cx_Oracle.connect('username', 'password', 'server')
cursor = connection.cursor()
cursor.execute(SQLQuery) #its throwing error here
It would be great if one can suggest me the appropriate function for executing the multiple queries in one call.
Appreciate your response. Thanks in advance.

What do you want to achieve with this?
Technically you could try to get rows from two tables or try to combine rows from different tables, but all this is directly done in SQL.

Remove ; (semicolon) at the end of query and it should run fine.

Related

Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead

I am trying to merge to a SQL Database using the following code in Databricks with pyspark
query = """
MERGE INTO deltadf t
USING df s
ON s.SLAId_Id = t.SLAId_Id
WHEN MATCHED THEN UPDATE SET *
WHEN NOT MATCHED THEN INSERT *
"""
driver_manager = spark._sc._gateway.jvm.java.sql.DriverManager
con = driver_manager.getConnection(url) #
stmt = con.createStatement()
stmt.executeUpdate(query)
stmt.close()
But I'm getting the following error:
SQLException: Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead at position 25.
Any thoughts on where might be going wrong?
I don't know why you're getting this exact error. However I believe there are a number of issues with what you are trying to do.
Running the query via JDBC makes it run in SQL Server only. Construct like WHEN MATCHED THEN UPDATE SET * / WHEN NOT MATCHED INSERT * will not work. Databricks accepts it, but for SQL Server you need to explicitly provide columns to update and values to insert (reference).
Also, do you actually have tables named deltadf and df in SQL Server? I suppose you have a Dataframe or temporary view named df... this will not work. As said, this query executes in SQL Server only. If you want to upload data from Dataframe use df.write.format("jdbc").save (reference).
See this Fiddle - if deltadf and df are tables, running this query in SQL Server (any version) will only complain about Incorrect syntax near '*'.
SQLException: Malformed SQL Statement: Expected token 'USING' but found Identifier with value 't' instead at position 25.
if you missed updating any specific field or specific syntax, you will get this error.
I performed merge operation its working fine for me without error, Please follow below reference .
Reference:
https://www.youtube.com/watch?v=i5oM2bUyH0o
https://docs.databricks.com/delta/delta-update.html#upsert-into-a-table-using-merge
https://www.sqlshack.com/sql-server-merge-statement-overview-and-examples/

Why does sqlite throws a syntax error in the python program?

My table is sqlite3 is created with the following:-
'CREATE TABLE IF NOT EXISTS gig_program ( gig_program_id VARCHAR(20) PRIMARY KEY );'
When I try to insert data into the table using python 3.8 with the following:-
sql = 'INSERT INTO gig_program ( gig_program_id ) VALUES ( "20200524120727" );'
cur.execute(sql)
the following exception was thrown:-
near "gig_program": syntax error
When I cut and past the insert command to the sqlite3 console, it works.
I have also tried using another editor for the program (thinking that there may be hidden characters) but the result is the same.
I would appreciate help. I have used similar methods in other parts of the program to insert data and they work without issue.
Thank you for looking into my questions.
I found that it was actually my mistake. The exception was actually for a second sql statement which I missed out the "FROM" word.
Thank you everyone for your time.
Hope everyone is doing well.

Can we run complex multi line SQL queries using Blueprism?

I am new to SQL stuff in blueprism, I am able to configure SQL object and execute simple queries, but I am facing trouble while trying to run multiline complex SQL queries.
when I was trying to execute the below query in blueprism, getting some error message, saying "Incorrect Syntax near Database2"
"select top 10 * from [Database1].[dbo].[Table1]
join [Database1].[dbo].[Table2] on [Database1].[dbo].[Table2].Fieldname1=[Database1].[dbo].[Table1].Fieldname2
join [Database2].[dbo].[Table1] on [Database2].[dbo].[Table1].Fieldname1=[Database1].[dbo].[Table2].Fieldname2"
Can somebody please help me, what was the wrong in the above query...
I found the answer myself, there should not be any additional white space characters in the query, entire query should be in continuous line. The beauty of blueprism is, it can execute any level of complex queries without any constraints, but need to modify the syntax accordingly. always we should mention the filename and table names in the following format - [databasename].[dbo].[tablename].[fieldname]

Using Excel to run a statement many times

I am trying to use excel to update a list of part numbers in my database:
UPDATE
stock s
SET
s.STC_AUTO_KEY = 2
WHERE s.WHS_AUTO_KEY = 2 AND
EXISTS(
SELECT
p.PNM_AUTO_KEY
FROM
PARTS_MASTER p
WHERE
s.PNM_AUTO_KEY=p.PNM_AUTO_KEY AND p.PN='102550');
UPDATE
stock s
SET
s.STC_AUTO_KEY = 2
WHERE s.WHS_AUTO_KEY = 2 AND EXISTS(
SELECT
p.PNM_AUTO_KEY
FROM
PARTS_MASTER p
WHERE
s.PNM_AUTO_KEY=p.PNM_AUTO_KEY AND p.PN='204-060-444-003');
The statements run without semicolons, but when I try to run more then one at once and use semicolons I get the error:
SQL Error [911] [22019]: ORA-00911: invalid character
java.sql.SQLSyntaxErrorException: ORA-00911: invalid character
so... it looks like I don't know how run run more then one basic statement at once.
I am using DBeaver to interact with a Oracle database.
Thanks guys, sorry if this a no-brainer.
Try adding a blank line between each update statement if possible. You can do this easily with a text editor that supports regular expressions, just replace ';\n' with ';\n\n'

What am I missing in trying to pass Variables in an SSIS Execute SQL Task?

I am creating an SSIS Execute SQL Task that will use variables but it is giving me an error when I try to use it. When I try to run the below, it gives me an error and when I try to build the query, it gives me an error SQL Sytnax Errors encountered and unable to parse query. I am using an OLEDB connection. Am I not able to use variables to specify the tables?
You can't parameterize a table name.
Use the Expressions editor in your Execute SQL Task to Select a SqlStatementSource property.
Try "SELECT * FROM " + #[User::TableName]
After clicking OK twice (to exit the Task editor), you should be able to reopen the editor and find your table name in the SQL statement.
Add a string cast in a case where it might be a simple Object - (DT_WSTR,100)
You are using only single parameter(?) in the query and assigning 3 inputs to that parameters which is not fair put only single input and assign some variable as input as shown in image and change the value of variable respectively.
the parameter name should be incremented by 1 start with 0 because they are the indexes representing the "?" in the query which was written the query window.

Resources