SQL Expected end of input but got keyword WHERE - sql

I am performing a SQL query using Python for a Big Query data set. I am getting an error and I could not figure out what do I need to fix on my own. This is the error log:
Expected end of input but got keyword WHERE
The variable vector_score is a float (I also tried with an int and does the same thing)
Query:
sql = f"""
SET lauder = lauder * %f
WHERE keyword= '%s'
AND (date BETWEEN '%s' AND '%s')
""" % (vector_score, keyword, starting_date, ending_date)
Parameters:
vector_score = 1.05
keyword = food
starting_date = '2020-01-10'
ending_date = '2020-01-17'

You are missing the actual update command in your execution
UPDATE TARGET_NAME
SET lauder = lauder * %f
WHERE keyword= '%s'
AND (date BETWEEN '%s' AND '%s')

Related

Setting a filter clause as a variable in impala

Is it possible to set a filtering condition as a variable in impala?
What I am doing right now is -
select * from demo
where gender != 'M' and age != 22;
What I want to achieve (this code fails) is
set var:filter_clause = gender != 'M' and age != 22;
select * from demo
where ${var:filter_clause}
;
This currently fails with an error -
Error: Unknown variable FILTER_CLAUSE
Could not execute command: set var:filter_clause = ...

GETTING ERROR-- ORA-00936:MISSING EXPRESSION for below query please help on this

SELECT CASE (SELECT Count(1)
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(
SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988')
)
WHEN 0 THEN
(
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27' )
WHEN 1 THEN
(
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
ELSE NULL
END
FROM dual;
You need to recreate your query and make sure to follow the flow of the clauses properly, please check the next two links to get a better understanding :
[ORA-00936: missing expression tips]
How do I address this ORA-00936 error?
Answer: The Oracle oerr utility notes this about the ORA-00936 error:
ORA-00936 missing expression
Cause: A required part of a clause or expression has been omitted. For example, a SELECT statement may have been entered without a list of columns or expressions or with an incomplete expression. This message is also issued in cases where a reserved word is misused, as in SELECT TABLE.
Action: Check the statement syntax and specify the missing component.
The ORA-00936 happens most frequently:
1 - When you forget list of the column names in your SELECT statement.
2. When you omit the FROM clause of the SQL statement.
ora-00936-missing-expression
I hope this can help you.
You cannot use a simple select query like this. You have to use a PL/SQL block like below -
DECLARE NUM_CNT NUMBER := 0;
BEGIN
SELECT Count(1)
INTO NUM_CNT
FROM wf_item_activity_statuses_v t
WHERE t.activity_label IN ('WAITING_DISB_REQ',
'LOG_DDE',
'LOG_SENDBACK_DDE')
AND t.item_key IN(SELECT r.i_item_key
FROM wf_t_item_xref r
WHERE r.sz_appl_uniqueid = '20400000988');
IF NUM_CNT = 0 THEN
delete
from t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_item_key = '648197'
AND p.i_document_srno = '27';
ELSIF NUM_CNT = 1 THEN
DELETE
FROM t_col_val_document_uploaded p
WHERE p.sz_application_no = '20400000988'
AND p.sz_collateral_id = 'PROP000000000PRO1701'
AND p.i_document_srno = '28' )
END IF;
END;

SpringJPA - inconsistent datatypes: expected NUMBER got BINARY

Im using SPRING JPA in my application and trying to run a native query as follows:
#Query (value="select max(ts) from abc.test s \n" +
"where abc.getTest(s.user_id) = :#{#userId} \n" +
"and upper(app_name) = 'TAX' and INSTR(s.user_id, '.') > 0 \n" +
"group by user_id, app_name", nativeQuery=true)
Timestamp getLastLogin(BigDecimal userId);
I am getting exception as follows:
Caused by: Error : 932, Position : 110, Sql = select max(ts) from abc.test s
where ac.getTest(s.user_id) = :1
and upper(app_name) = 'TAX' and INSTR(s.user_id, '.') > 0
group by user_id, app_name, OriginalSql = select max(ts) from abc.test s
where abc.getTest(s.user_id) = ?
and upper(app_name) = 'TAX' and INSTR(s.user_id, '.') > 0
group by user_id, app_name, Error Msg = ORA-00932: inconsistent datatypes: expected NUMBER got BINARY
at oracle.jdbc.driver.T4CTTIoer11.processError(T4CTTIoer11.java:514)
... 109 more
<org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <SqlExceptionHelper> <logExceptions> <SQL Error: 932, SQLState: 42000>
<org.hibernate.engine.jdbc.spi.SqlExceptionHelper> <SqlExceptionHelper> <logExceptions> <ORA-00932: inconsistent datatypes: expected NUMBER got BINARY
>
I tried to convert the max(ts) to string using to_char and changed the coresponding getter/setter to String still getting the same issue.
Update:
I fixed it with this small change
String getLastLogin(#Param("userId") BigDecimal userId);
Didnt add #Param for the arguments.
That query return null value thats why that null values can not assign to number. We can not assign null to int(primitive) in java.

How to use variable in an sql statement when the variables have special characters

I am attempting to select columns of data from several different tables in a for loop, and each of the table names begin with numbers, titled 0_training_market through 155_training_market.
To use a table beginning with a number in an sql statement, I have to use an extra pair of quotations around that title, which just turns my variable into a string and doesn't read the actual variable. Here is my code and error:
for j in list(range(156)):
generation = str(j)+'_training_market'
energy_source = db.query(" SELECT energy_source FROM " + generation + " WHERE id < 100 ORDER BY id ")
quantity = db.query(" SELECT quantity FROM " + generation + " WHERE id < 100 ORDER BY id ")
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "0"
LINE 1: SELECT energy_source FROM 0_training_market WHERE id < 100 ...
^
[SQL: SELECT energy_source FROM 0_training_market WHERE id < 100 ORDER BY id ]
I'm not sure how to work around this, so any help is appreciated. Thanks
PostgreSQL needs to have doublequotes around badly named objects:
for j in list(range(156)):
generation = str(j)+'_training_market'
energy_source = db.query(""" SELECT energy_source FROM "{}" WHERE id < 100 ORDER BY id """.format(generation))
quantity = db.query(""" SELECT quantity FROM "{}" WHERE id < 100 ORDER BY id """.format(generation))

How to run procedure without parameter in Teradata

How to run procedure without parameter in Teradata
I'm trying with : call db1.proc_dbOSA()
Error Msg:
Call failed 3707: PROC_DBOSA: Syntax error, expected something
like a name or a Unicode delimited identifier between ‘(‘ and ‘)’
New Procedure with error result.
When i run only code then everything works ok.
REPLACE PROCEDURE db1.proc_dbOSA()
BEGIN
DELETE FROM db1.LOG_dbOSA;
INSERT INTO
db1.LOG_dbOSA
(StoreNo, IDX, Flow, Status, MRP, OSA_IDX, OSA_AC)
WITH a AS (
SELECT
c.StoreCode,
CAST(SUBSTRING(c.ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
CASE
WHEN c.MinimumTargetStockWrpl >= l.MinimumTargetStockWrpl THEN CAST(l.MinimumTargetStockWrpl AS INT)
WHEN c.MinimumTargetStockWrpl < l.MinimumTargetStockWrpl THEN CAST(c.MinimumTargetStockWrpl AS INT)
End AS StoreMin,
c.ValUnrestrictedStock
FROM
db1.tab1 c
INNER JOIN
(
SELECT
StoreCode,
ArticleCode,
MinimumTargetStockWrpl
FROM
db1.tab1
WHERE
ProcessingDate = CURRENT_DATE - 14
) l ON c.StoreCode = l.StoreCode AND c.ArticleCode = l.ArticleCode
WHERE
c.ProcessingDate = CURRENT_DATE AND c.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl IS NOT NULL AND l.MinimumTargetStockWrpl > 0
)
, t AS
(
SELECT
CAST(SUBSTRING(ArticleCode FROM 13 FOR 6) AS INT) AS IDX,
RpType,
ArticlesPlanner
FROM
DWH_db_V.STK_B_ARTICLE_DAY_V
WHERE
ProcessingDate = CURRENT_DATE AND StoreCode = 'DR04'
)
SELECT
a.StoreCode,
a.IDX,
t.RpType,
t.ArticlesPlanner,
a.RpType,
CASE
WHEN a.ValUnrestrictedStock > 0 THEN 1
WHEN a.ValUnrestrictedStock <= 0 THEN 0
End AS OSA_IDX,
CASE
WHEN a.ValUnrestrictedStock >= StoreMin THEN 1
WHEN a.ValUnrestrictedStock < StoreMin THEN 0
End AS OSA_AC
FROM
a
LEFT JOIN
t ON t.IDX = a.IDX;
End;
BTEQ Error:
+---------+---------+---------+---------+---------+---------+---------+----
Call proc_dbOSA;
Call proc_dbOSA;
$
* Failure 3707 Syntax error, expected something like '(' between the word
'proc_dbOSA' and ';'.
Statement# 1, Info =18
* Total elapsed time was 1 second.
Call proc_dbOSA();
* Failure 3707 PROC_DBOSA:Syntax error, expected something like a name or
a Unicode delimited identifier between '(' and ')'.
* Total elapsed time was 1 second.
Stored procedures do not support the following:
EXPLAIN and USING request modifiers within a stored procedure
EXECUTE macro statement
WITH clause within a stored procedure.
Stored procedures, as well as macros, do not support the following query logging statements:
BEGIN QUERY LOGGING
END QUERY LOGGING
FLUSH QUERY LOGGING
REPLACE QUERY LOGGING