Question

I am using python33 and cx_oracle (with Oracle 11g) to analyze a database, but I have run into a problem. The problem is with this SQL: merge into ftab01 a using (select username , count (case when action ='friend' then 1 end) friendCount from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set a.friendCount=b.friendCount if I run this command in SQL Developer, everything works perfectly, but if I do this: cursor.execute("merge into ftab01 a using (select username , count (case when action ='friend' then 1 end) friendCount from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set a.friendCount=b.friendCount"), it will fail (without any error! - It just does not change the table). Other commands are working properly (for example: cursor.execute('alter table '+self.tabname + ' add ('+column_name+' number)') - this code is just two lines above the problematic code. I have no idea what might be wrong, I tried googling for longer time but I haven't found anything (maybe because I don't know how to name this problem)

Code where I used it:

def action_counts(self,action_list):
sql = "merge into "+self.tabname + " a using (select username "
sql_when_matched ="";
for action in action_list:
  column_name = (action+'Count').replace('-','_')
  print(column_name)
  sql += ", count (case when action ='"+action+"' then 1 end) "+column_name
  sql_when_matched += " a."+column_name+"=b."+column_name+", "
  cursor.execute('alter table '+self.tabname + ' add ('+column_name+' number)')
sql += " from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set "+sql_when_matched
sq2 = sql.rstrip().rstrip(",")
print(sq2)
cursor.execute(sq2)

#this is the printed sq2 and copy-pasted into execute() (and if copy-pasted to SQL Developer it is working properly)
cursor.execute("merge into ftab01 a using (select username , count (case when action ='friend' then 1 end) friendCount from pool_1 group by username) b on (a.USERNAME=b.username) WHEN MATCHED THEN update set  a.friendCount=b.friendCount")

Since it does not produce any error message I have no idea what could be wrong, any help is appreciated.

Was it helpful?

Solution

Are you commiting the update? Depending on your version of Oracle the your alter table may be autocommitted but the merge may be rolled back.

try adding:

Connection.commit()

after the merge and see if it works.

OTHER TIPS

As I understand, you are executing SQL in PYTHON that have MERGE statement. It required commit. For this kindly enable Auto commit after connection.

connconnection = connection.execution_options(autocommit=True)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top