I am updating multiple fields and my query works each statement correctly but errors when it goes to the next row. I comment out the preceeding row and rerun and runs the statement and errors on the next line.
I.e
UPDATE mydb
SET VendorMPD = replace(VendorMPD,'VENDOR Campaign MPD - ','');
SET VendorMPDHalf = replace(VendorMPDHalf,'Vendor MPD 1/2 Count - ','');
SET DieselMPD = replace(DieselMPD,'Diesel Pumps - ','')
Error that it generates each time:
[Err] 42000 - [SQL Server]Incorrect syntax near '='.
I had this working perfectly yesterday but needed to add additional fields and when I did this is what started happening. I looked at the new statements that I entered and with exception to the field names and what they are replacing. The coding is identical.
Try the following syntax to update multiple columns in a single statement:
UPDATE mydb
SET VendorMPD = replace(VendorMPD,'VENDOR Campaign MPD - ',''),
VendorMPDHalf = replace(VendorMPDHalf,'Vendor MPD 1/2 Count - ',''),
DieselMPD = replace(DieselMPD,'Diesel Pumps - ','');
Related
I have been trying to update some columns of a database table using cx_Oracle in Python. I created a list named check_to_process which is a result of another sql query. I am creating log_msg in the program based on success or failure and want to update same in the table only for records in check_to_process list. When I update the table without using bind variable <MESSAGE = %s>, it works fine. But when I try to use bind variable to update the columns it gives me error :
cursor.executemany("UPDATE apps.SLCAP_CITI_PAYMENT_BATCH SET MESSAGE = %s, "
TypeError: an integer is required (got type str)
Below is the code, I am using:
import cx_Oracle
connection = cx_Oracle.connect(user=os.environ['ORA_DB_USR'], password=os.environ['ORA_DB_PWD'], dsn=os.environ['ORA_DSN'])
cursor = connection.cursor()
check_to_process = ['ACHRMUS-20-OCT-2021 00:12:57', 'ACHRMUS-12-OCT-2021 16:12:01']
placeholders = ','.join(":x%d" % i for i,_ in enumerate(check_to_process))
log_msg = 'Success'
cursor.executemany("UPDATE apps.SLCAP_CITI_PAYMENT_BATCH SET MESSAGE = %s, "
"PAYMENT_FILE_CREATED_FLAG='N' "
"WHERE PAYMENT_BATCH_NAME = :1",
[(i,) for i in check_to_process], log_msg, arraydmlrowcounts=True)
Many thanks for suggestions and insights!
Your code has an odd mix of string substitution (the %s) and bind variable placeholders (the :1). And odd code that creates bind variable placeholders that aren't used. Passing log_msg the way you do isn't going to work, since executemany() syntax doesn't support string substitution.
You probably want to use some kind of IN list, as shown in the cx_Oracle documentation Binding Multiple Values to a SQL WHERE IN Clause. Various solutions are shown there, depending on the number of values and frequency that the statement will be re-executed.
Use only bind variables. You should be able to use execute() instead of executemany(). Effectively you would do:
cursor.execute("""UPDATE apps.SLCAP_CITI_PAYMENT_BATCH
SET MESSAGE = :1
WHERE PAYMENT_BATCH_NAME IN (something goes here - see the doc)""",
bind_values)
The bottom line is: read the documentation and review examples like batch_errors.py. If you still have problems, refine your question, correct it, and add more detail.
I have a field named field, and I would like to see if it is null, but I get an error in the query, my code is this:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
if field <> null then query1=query1 & " where id = '"& field &"',
exec= Oracle.Database("TESTING",[Query=query1])
in
exec
but I get an error in the condition, do you identify the mistake?
I got Expression.SyntaxError: Token Identifier expected.
You need to assign the if line to a variable. Each M line needs to start with an assignment:
let
Condition= Excel.CurrentWorkbook(){[Name="test_table"]}[Content],
field= Condition{0}[fieldColumn],
query1="select * from students",
query2 = if field <> null then query1 & " some stuff" else " some other stuff",
exec= Oracle.Database("TESTING",[Query=query2])
in
exec
In query2 you can build the select statement. I simplified it, because you also have conflicts with the double quotes.
I think you're looking for:
if Not IsNull(field) then ....
Some data types you may have to check using IsEmpty() or 'field is Not Nothing' too. Depending on the datatype and what you are using.
To troubleshoot, it's best to try to set a breakpoint and locate where the error is happening and watch the variable to prevent against that specific value.
To meet this requirement, I would build a fresh Query using the PQ UI to select the students table/view from Oracle, and then use the UI to Filter the [id] column on any value.
Then in the advanced editor I would edit the generated FilteredRows line using code from your Condition + field steps, e.g.
FilteredRows = Table.SelectRows(TESTING_students, each [id] = Excel.CurrentWorkbook(){[Name="test_table"]}{0}[fieldColumn])
This is a minor change from a generated script, rather than trying to write the whole thing from scratch.
In my Lotus Notes script, i do have this piece of logic as shown below. This is basically for two SELECT statements, followed by Fetch for each of the SELECT statement separately and the SELECT is for the same DB2 table with a variation in WHERE clause. The error i'm getting is for the second FETCH. The error i'm getting is ---> Field count mismatch error:
count = 0
If (srccon.Execute(selectstatement, fldLst1) = 0) Then
Goto GetNextDoc
End If
count = srccon.Fetch(fldLst1)
If ( count = 0 ) Then
Goto GetNextDoc
End If
The above cursor select and fetch does not give me any error.
The cursor down which is for the same DB2 table with a slight variation
in WHERE clause is causing the error:
count1 = 0
If (srccon.Execute(selectstatement1, fldLst) = 0) Then
Goto GetNextDoc
End If
count1 = srccon.Fetch(fldLst) ---> The error is pointing to this line
and the error is
I would appreciate any help in this regard. I would also thank the gentleman who
did excellent solution for my previous problem for current date minus 30 days.
With much thanks
I suspect it's because when you call Execute, you're reusing the same LCFieldList object from a previous call. Execute and Select statements append their list of result fields to the object you pass them, so you must pass them an empty fieldlist -- one you just created. Otherwise you get a combined fieldlist of all the fields in the result sets of multiple calls to Select or Execute.
You may find the LotusScript chapter of this Redbook useful.
I am following the hadoop_cql3_word_count example in Cassandra and have questions with the following code segment:
String query =
"UPDATE " + KEYSPACE + "." + OUTPUT_COLUMN_FAMILY +
" SET count_num = ? ";
CqlConfigHelper.setOutputCql(job.getConfiguration(), query);
My questions are:
What is the definition of the question mark (i.e., ?) in the above query? Does Cassandra process it in a way such that the question mark is replaced by some value?
If I would like to update multiple columns of a row given its key, how should I modify the above update statement?
Thank you,
The ? represents a slot for a variable in a prepared statement. When your MR job completes the values will be placed into the ?s in order.
If your MR results looked like (key=key1, 1) (key=key2, 2) (key=key3, 3)
Then the statements executed would be
Update Keyspace.columnfamily SET count_num = 1 where key=key1
Update Keyspace.columnfamily SET count_num = 2 where key=key2
Update Keyspace.columnfamily SET count_num = 3 where key=key3
To update multiple columns you just need to write a larger prepared statement and make sure your map reduce job is providing all of the appropriate values.
In the WC example
keys.put("row_id1", ByteBufferUtil.bytes(partitionKeys[0]));
keys.put("row_id2", ByteBufferUtil.bytes(partitionKeys[1]));
...
keys.put("word", ByteBufferUtil.bytes(word.toString()));
variables.add(ByteBufferUtil.bytes(String.valueOf(sum)));
...
context.write(keys, getBindVariables(word, sum));
This makes the reducer output look like
({row_id1=1,row_id2=3,word=pizza},4)
And the prepared statement will be executed like
UPDATE cql3_worldcount.output_words SET count_num = 4 where row_id1=1 AND row_id2=3 AND word=pizza ;
If I wanted a prepared statement with multiple columns it would look like
UPDATE test SET a =?,b=?,c=?,d=? (This gets filled in by the connector: where key=...)
With a real prepared statement we would also fill in the key as well, but here the connector to Cassandra will just use whatever mappings you have in your reducer output.
({key='mykey'},(1,2,3,4))
becomes
UPDATE test SET a =1,b=2,c=3,d=4 where key=mykey
For more information on prepared statements in general check
SO Question about Prepared Statements in CQL
I have this SAS sample code:
data BEFORE;
input v1 v2;
datalines;
1 2
;
data AFTER;
put 'Before IF: ' _ALL_;
if _N_ = 1 then set BEFORE;
put 'After IF : ' _ALL_;
run;
The output is:
BEFORE: v1=. v2=. _ERROR_=0 _N_=1
AFTER : v1=1 v2=2 _ERROR_=0 _N_=1
BEFORE: v1=1 v2=2 _ERROR_=0 _N_=2
AFTER : v1=1 v2=2 _ERROR_=0 _N_=2
And the output file contains:
Obs v1 v2
1 1 2
2 1 2
I know that the SET will import and RETAIN the BEFORE dataset's variables, but why BEFORE's record gets duplicated?
I ran your sample code, and you omitted a crucial piece of information: This message was in the SAS log: "NOTE: DATA STEP stopped due to looping.". Googling on that message led me to a SAS paper describing the error. It suggested not using an IF statement before the SET statement, but to use the OBS= data set option to restrict the number of observations read.
So you would change the line:
if _N_ = 1 then set BEFORE;
to:
set BEFORE(obs=1);
When I ran your code with this change, the "Before IF:" line still printed twice, and I'm not sure why that is so. But the looping NOTE did not occur, so I believe that is the solution.
The SET is an executable statement, that is, unless being executed, it does not reset variables or load the next observation's data, when the data step is executed. (It sets up or alter PDV when the data step is compiled, though.) Because of the if condition, it is executed only once.
The implicit OUTPUT statement at the bottom outputs an observation per iteration. SAS, monitoring to see if a data step loops infinitely, stops the data step after the second iteration and generates the note.