データベースを分析するために python33 と cx_oracle (Oracle 11g を使用) を使用していますが、問題が発生しました。問題はこの 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
SQL Developer でこのコマンドを実行すると、すべてが完全に機能しますが、これを実行する
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")
と、失敗します (エラーなしで! - テーブルが変更されないだけです)。他のコマンドは正常に動作しています (例:
cursor.execute('alter table '+self.tabname + ' add ('+column_name+' number)')
- このコードは、問題のあるコードの 2 行上にあります。何が問題なのかわかりません。長時間グーグルを試しましたが、何も見つかりませんでした (おそらく、この問題に名前を付ける方法を知っている)
私が使用したコード:
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")
エラーメッセージが表示されないため、何が問題なのかわかりません。助けていただければ幸いです。