Syntax Error in query expression with vb 6.0 - vb6

sql = "update Attendance set Attended=Attended+1 where Student ID like 15001"
db.Execute (sql)
is is showing :
"syntax error(missing operator) in query expression 'Student ID like
15001'

If realy you have a space between Student and ID tehn your query must be :
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)

sql should always be written with an _. For Example Student ID should always be Student_ID so that it has the idea of reading which column it needs to filter its search. For future purposes always create a column name with an _.
sql = "update Attendance set Attended=Attended+1 where Student_ID like 15001"
db.Execute (sql)
or
sql = "update Attendance set Attended=Attended+1 where [Student ID] like 15001"
db.Execute (sql)

Try something that looks a bit cleaner...
sql = "Update tblAttendance SET "
sql = sql & " Attended = Attended + 1 "
sql = sql & " Where Student_ID = 15001 "
db.execute SQL <-- no paranthesis

Related

How to update row in table with condition is refer to another table

How can I update a table which the condition is refer to other table in spring jpa ?
I have table Task which relation to TaskProgress (many to one). And TaskProgress has some value eg: CREATE, INPROGRES, DONE, ARCHIVED,...
Here is my query in repostitory:
#Modifying
#Query("UPDATE Task t SET t.responsibleUser = null WHERE t.responsibleUser.id = :userId " +
"AND t.deleted = false AND t.taskProgress.name <> 'ARCHIVED'")
void removeResponsibleUserId(UUID userId);
But I got the error:
Hibernate: update task cross join set responsible_user_id=null where responsible_user_id=? and deleted=0 and name<>'ARCHIVED'
2020-11-25 00:38:51 - SQL Error: 1064, SQLState: 42000
2020-11-25 00:38:51 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'set responsible_user_id=null where responsible_user_id=x'152B59C33A0035003500000' at line 1
Use an exists predicate like this:
#Modifying
#Query("UPDATE Task t SET t.responsibleUser = null WHERE t.responsibleUser.id = :userId " +
"AND t.deleted = false AND NOT EXISTS (SELECT 1 FROM t.taskProgress p WHERE p.name = 'ARCHIVED')")
void removeResponsibleUserId(UUID userId);

in select statement how we compare a string field which contain single quotes

i have a filed in stock table with title product. when i use the following statement
rs.Open "select * from stock where product='" & product_name & "'
",db,2,1
id = rs!sub_head_id
rs.Close
where product_name is string variable which contain a product name
for example:
product_name="alpha's cell"
this statement make an error, because of single quotes in string.
how we resolve this
note: i am using vb6
Use this and try:
rs.Open "select * from stock where product='" & replace(product_name,"'","''") & "' ",db,2,1
Ok , my problem is solved . some one suggest that set the Index of Criteria field like bno and productid .

Fetch dates (by month or year) stored in TIMESTAMP using JPA

I am facing a problem and I would like you to help me.
It turns out I have one table in my Oracle 11g database where I store failures of one electronic device. The table definition is following:
CREATE TABLE failure
( failure_id NUMERIC NOT NULL
, fecha TIMESTAMP NOT NULL
, module_id NUMERIC NOT NULL
, code NUMERIC
, PRIMARY KEY(failure_id)
);
Where 'fecha' means 'date'.
I need to fetch failures by YEAR or by MONTH for one specific module but I can't. My ORM maps the TIMESTAMP type to java.sql.Date but I don't know how to compare the month in the JPQL sentence. I have tried to use ORACLE functions with native queries but I front with another issue: to cast the results.
I am using JPA 2.0 with Eclipselink 2.3.2.
My doubts are:
Can I use Oracle functions with this version of Eclipselink library? My experience say no.
Query query = entityManager.createQuery("SELECT f FROM Failure f "
+ "WHERE EXTRACT(YEAR FROM f.fecha) = ?1 "
+ "AND f.moduleId.moduleId = ?2");
query.setParameter(1, year);
query.setParameter(2, idModule);
I get this error: Unexpected token [(]
Can I use Eclipselink functions? My experience say no.
Query query = entityManager.createQuery("SELECT f FROM Failure f "
+ "WHERE EXTRACT('YEAR', f.fecha) = ?1 "
+ "AND f.moduleId.moduleId = ?2");
query.setParameter(1, year);
query.setParameter(2, idModule);
Same error.
Do you know a simple way to fetch this data using only one query?
I know I can fetch one module and then check failures with loops but I think it is not the best performing solution.
Thanks.
My sources:
Eclipselink JPA functions link
Eclipselink Query Enhancements link
A native query is written in the SQL dialect of your DB so can use DB specific functionality see the createNativeQuery methods of the EntityManager.
However there is another solution, test the timestamp against a lower and upper value:
WHERE f.fecha >= '2012-9-1' AND f.fecha < '2012-10-1'
The syntax in EclipseLink 2.4 for EXTRACT is,
EXTRACT(YEAR, f.fecha)
http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL#Functions
I used Eclipselink v 2.4 functions and I am getting values using this:
Query query = entityManager.createQuery("SELECT f FROM Failure f "
+ "WHERE SQL('EXTRACT (YEAR FROM ?)', f.fecha) = ?1 "
+ "AND f.moduleId.moduleId = ?2 ");
Extracting year from date stored in database avoids to me one comparison between two dates.
Use
Query query = entityManager.createQuery("SELECT f FROM Failure f "
+ "WHERE EXTRACT(YEAR from f.fecha) = ?1 "
+ "AND f.moduleId.moduleId = ?2");
I was facing the same problem. I only checked for the correct syntax of that EXTRACT function for oracle and it worked for me! (Notice the FROM clause into the function syntax.
#NamedQuery(name = "Registros.findByFechacaptura", query = "SELECT s FROM Registros s WHERE EXTRACT(YEAR FROM s.fechacaptura) = EXTRACT(YEAR FROM :fechacaptura)")

jdbc result set metadata: getting physical column names on aliased columns

I'm using jdbc to execute query statements (in jruby)
# made-up example
sql = "select " +
"c.type as cartype, " +
"o.id as ownerid, " +
"o.type as ownertype " +
"from cars c " +
"inner join owners o " +
"on c.vin = o.vin"
# 'stmt' gotten with jdbc-connection.create_statement()
result_set = stmt.execute_query(sql)
meta_data = result_set.get_meta_data()
col_count = result_set.get_column_count()
I can query the various column aliases (get_column_name) and tables (get_table_name) for each column through the column indexes, but I also need the actual/physical names of the columns, un-aliased.
How do I get the physical/actual name of column, as it is defined in the schema ("ownerid" column alias is column "id", for instance)?
From tests with other database types, it looks as though this is database+driver specific. Using mysql get_column_name returns the actual/physical column name while get_column_label returns the alias. As an aside, both database types (mysql and sqlite) return the physical table name through get_table_name.

Update value from a select statement

I'm using an Access over Oracle database system (Basically using Access for the forms and getting into the tables using ADO code) and am trying to update a field in the product table with the value of the same named field in a load table.
The code I am using is:
.CommandText = "UPDATE " & strSchema & ".TBL_CAPITAL_MGMT_PRODUCT a INNER JOIN " & strSchema & ".TBL_CAPITAL_MGMT_TEMP_LOAD b ON a.AR_ID = b.AR_ID SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;"
Which returns an error about missing SET keyword.. So I changed it to:
.CommandText = "UPDATE (SELECT a.TOT_RWA_AMT, b.TOT_RWA_AMT As New_RWA_AMT FROM " & strSchema & ".TBL_CAPITAL_MGMT_TEMP_LOAD a INNER JOIN " & strSchema & ".TBL_CAPITAL_MGMT_PRODUCT b ON b.AR_ID = a.AR_ID Where a.New_Rec <> '-1' AND a.IP_ID Is Not Null) c SET c.New_RWA_AMT = c.TOT_RWA_AMT;"
Which returns an error about non key-preserved table. the b table has a pk of AR_ID but the a table has no primary key and it probably won't be getting one, I can't update the structure of any of the tables.
I tried using the /*+ BYPASS_UJVC */ which lets the code run, but doesn't actually seem to do anything.
Anyone got any ideas where I should go from here?
Thanks
Alex
Ignoring the irrelevant ADO code, the update you are trying to do is:
UPDATE TBL_CAPITAL_MGMT_PRODUCT a
INNER JOIN
SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;
This isn't supported by Oracle (though maybe this undocumented BYPASS_UJVC hint is supposed to overcome that, but I wasn't aware of it till now).
Given that your inline view version fails due to lack of constraints you may have to fall back on the traditional Oracle approach using correlated subqueries:
UPDATE TBL_CAPITAL_MGMT_PRODUCT a
SET a.TOT_RWA_AMT = (SELECT b.TOT_RWA_AMT
FROM TBL_CAPITAL_MGMT_TEMP_LOAD b
WHERE a.AR_ID = b.AR_ID
)
WHERE EXISTS (SELECT NULL
FROM TBL_CAPITAL_MGMT_TEMP_LOAD b
WHERE a.AR_ID = b.AR_ID
);
The final WHERE clause is to prevent TOT_RWA_AMT being set to NULL on any "a" rows that don't have a matching "b" row. If you know that can never happen you can remove the WHERE clause.
If you're using Oracle 10g or higher, an alternative to Tony's solution would be to use a MERGE statement with only a MATCHED clause.
MERGE INTO TBL_CAPITAL_MGMT_PRODUCT a
USING TBL_CAPITAL_MGMT_TEMP_LOAD b
ON (a.AR_ID = b.AR_ID)
WHEN MATCHED THEN
UPDATE SET a.TOT_RWA_AMT = b.TOT_RWA_AMT;

Resources