I have a simple select query which is inside an openquery
SELECT
MyColumn,
RIGHT(MyColumn,2)
FROM MyTable
however this gives error
Cause . . . . . : A syntax error was detected at token ,2. Token ,2
is not a valid token. A partial list of valid tokens is ) ,.
In case someone has this issue.
RIGHT(MyColumn,2) gives token error
RIGHT(MyColumn, 2) is correct
Outcome of the actual query
SELECT * FROM OPENQUERY(linkServer, 'SELECT RIGHT(MyColumn, 2) FROM MyTable')
Related
I want to run the following query in by db2:
CALL spDrpObj('DE','T') ;
CREATE TABLE QTEMP.DE
(
Ano int
);
CREATE OR REPLACE VARIABLE g int;
set g=1;
Begin
WHILE(g<=100) DO
INSERT INTO QTEMP.DE
SELECT g
SET g=g+1;
END WHILE;
But it's showing me error like:-
SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token G was not valid. Valid tokens: , FROM INTO. Cause . . . . . : A syntax error was detected at token G. Token G is not a valid token. A partial list of valid tokens is , FROM INTO. This list assumes that the statement is correct up to the token. The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery . . . : Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token G. Correct the statement. The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. -- If the error token is , correct the SQL statement because it does not end with a valid clause.
Long time lurker and learner, first time question here. I'm working a project for a local library to pull some data from their database to help the employees pull books put on hold for patrons. The catch here is the access to the database is read only. I cannot create any temporary tables or views.
We've created a long query to generate the data needed, I used a couple CTEs to whittle things down and then there's some logic based on which location the book may reside in to show where it should pick from. All in all, we're happy with the query results.
When I try to implement it using jdbcTemplate, I can't seem to find a way to get anything other than SELECT * to work.
WITH holdCTE1 (holdID, itemID, ...) AS (
SELECT *
FROM
table 1,
table 2,
table 3
WHERE
yada yada
This will give me results if I then do a SELECT * FROM holdCTE1.
If instead I specify columns, like this
WITH holdCTE1 (holdID, itemID, ...) AS (
SELECT t1.holdID, t2.itemID, t3.title
FROM
table 1,
table 2,
table 3
WHERE
yada yada
I get a syntax error at the first from table, regardless. I've tried adjusting all my table JOINs and using aliases and just about everything I can come across, but it doesn't seem to help.
A couple of things we're using to help the filtering and such, since we can't create a view to make it easy, is to call out the subqueries, so for example, in CTE1 we add a final column
'bib hold' AS hold_type
Even with the SELECT *, 'bib hold AS hold_type, I get the same syntax error. The actual wording of the error is:
StatementCallback; bad SQL grammar ... then my query ... nested exception is org.postgresql.util.PSQLException: ERROR: syntax error at or near ... the first thing under the FROM line
The library is using a Postgresql database, and I'm using Spring with jdbcTemplate for the SQL side.
Thanks in advance for looking.
Sorry for the poorly worded question, but I think I stumbled across an answer this evening. I was checking all of my queries I'd noted in pgAdmin, and I did a copy/paste into my java class.
With the escape characters, the code seems to run just fine. So this looks like it is passing info correctly to the query
WITH hold_bib (hold_id, item_id, birl_bib, patron, status, hold_type) AS (SELECT\n"
+ " h.id,\n"
+ " i.record_id,\n"
+ " birl.bib_record_id,\n"
+ " h.patron_record_id,\n"
+ " i.item_status_code,\n"
+ " 'bib'AS hold_type\n"
The function looks like this at the beginning now:
public List<Hold> libraryNotPicked() {
String sql = "\n"
+ "WITH hold_bib (hold_id, item_id, birl_bib, patron, status, hold_type) AS (SELECT\n"
+ " h.id,\n"
+ " i.record_id,\n"
+ " birl.bib_record_id,\n"
+ " h.patron_record_id,\n"
+ " i.item_status_code,\n"
+ " 'bib'AS hold_type\n"
...
+ " ;
List<Hold> holds = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Hold.class));
return holds;
Seems to me the key is the \n type characters in the query.
From,
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/db2/rbafzregexp_like.htm
SELECT PID FROM PRODUCT
WHERE NOT REGEXP_LIKE(pid,'[0-9]{3}-[0-9]{3}-[0-9]{2}')
But when I run a similar command,
SELECT MYCOLUMN FROM MYTABLE
WHERE NOT REGEXP_LIKE(MYCOLUMN,'[0-9]{3}-[0-9]{3}-[0-9]{2}')
I get
SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token
was not valid. Valid tokens: < > = <> <= !< !> !=
= ¬< ¬> ¬= IN. Cause . . . . . : A syntax error was detected at token . Token is not a valid
token. A partial list of valid tokens is < > = <> <= !< !> != >= ¬<
¬> ¬= IN. This list assumes that the statement is correct up to the
token. The error may be earlier in the statement, but the syntax of
the statement appears to be valid up to this point. Recovery . . . :
Do one or more of the following and try the request again: -- Verify
the SQL statement in the area of the token .
Correct the statement. The error could be a missing comma or
quotation mark, it could be a misspelled word, or it could be related
to the order of clauses.
-- If the error token is , correct the SQL statement because it does not end with a valid clause.
Is there any reason why this example does not work? Is IBM v7r1 not the same as 7.1?
7.1 and v7r1 are the same thing...
The regular expression support was added as part of technology refresh (TR) level 9
From a command line, use the Work with PTF Groups (WRKPTFGRP) command
WRKPTFGRP PTFGRP(SF99707) PTFGRPLVL(*INSTALLED)
Also, you need to have 5770-SS1 Option #39 International Components for Unicode insallted. You can use the Display Software Resources (DSPSFWRSC) to check for that.
If that's not installed, you'd see a message:
SQL0204 - QQQSVREG in QSYS type *SRVPGM not found.
I have two .txt files. The first file (out.txt) has data as below:
empno,ename
'7369','SMITH'
'7499','ALLEN'
'7521','WARD'
'7566','JONES'
'7654','MARTIN'
'7698','BLAKE'
'7782','CLARK'
And my second file (Output.txt) has data as below:
empno,ename,hiredate,comm
'7369','SMITH','1980-12-17',NULL
'7499','ALLEN','1981-02-20','300.00'
'7521','WARD','1981-02-22','500.00'
'7566','JONES','1981-04-02',NULL
'7654','MARTIN','1981-09-28','1400.00'
'7698','BLAKE','1981-05-01',NULL
'7782','CLARK','1981-06-09',NULL
Now when I execute the following statement for the first file, I get my desired output:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\Data Migration;','SELECT * FROM out.txt'
But when I execute the same code for the second file, I get an error:
SELECT * FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0','Text;Database=D:\Data Migration;','SELECT * FROM Output.txt'
The error that I get is:
Msg 7399, Level 16, State 1, Line 1
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" reported an
error. The provider did not give any information about the error.
Msg 7321, Level 16, State 2, Line 1
An error occurred while preparing the query "SELECT * FROM Output.txt" for execution
against OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)".
What I understand is that, since my second file has some NULL values that would be the reason for the error. So can some one help me in handling NULL in this case?
You can use the ISNULL option and replace with your desired value.
<cfquery name="LOCAL.qrySelEvents" datasource="#variables.datasourceSettings.getDatasourceName()#" result="LOCAL.qryData" >
SELECT evt_id,
acnt_dba_name,
form_id,
'#application.portals.data[request.applicationName].profileRootPath#form/index.cfm'
|| CHAR(63)
|| 'PKformID= '
|| #preserveSingleQuotes(LOCAL.formIdSql)# AS primaryFormURL,
FROM events
</cfquery>
I have to concat #application.portals.data[request.applicationName].profileRootPath#form/index.cfm with char(32) and PKformID= ' || #preserveSingleQuotes(LOCAL.formIdSql)#. I have used the || operator of postgres. But it is giving me an error:
ERROR: syntax error at or near "||"
Can you please help me in this?
The last line of your select clause has a comma at the end.
OK, as well as the error message, if you have ROBUST EXCEPTION HANDLING switched on (it's in CFAdmin), you should get back the SQL that CF was passing to the server. This should show you where the syntax error is in your SQL. You should also always post this info in your question, so even if you can't see the problem, someone else might be able to.
Now I suspect it's because this:
#preserveSingleQuotes(LOCAL.formIdSql)#
needs to be treated as a string (which it is), in which case you'll need to quote it for the DB to see it as a string. IE:
'#preserveSingleQuotes(LOCAL.formIdSql)#'
Given there is no DB-centric references in that whole value:
`'#application.portals.data[request.applicationName].profileRootPath#form/index.cfm' || CHAR(63) || 'PKformID= ' || '#preserveSingleQuotes(LOCAL.formIdSql)#'`
I question why you need to include it in your SELECT query. You're basically just passing the value to the SQL server, and just getting it back again afterwards. So I suspect something is amiss here. What are you actually trying to do here?
Ya finally got the answer of this , CHAR(63) is creating problem in this as in Sql-server CHAR is a function which gives Character from ASCII and in postgresql CHR() function is available in order to give character from ASCII. So , new Query is :
<cfquery name="LOCAL.qrySelEvents" datasource="#variables.datasourceSettings.getDatasourceName()#" result="LOCAL.qryData" >
SELECT evt_id,
acnt_dba_name,
form_id,
'#application.portals.data[request.applicationName].profileRootPath#form/index.cfm'
|| CHR(63)
|| 'PKformID= '
|| #preserveSingleQuotes(LOCAL.formIdSql)# AS primaryFormURL
FROM events
</cfquery>
Thanks for all your support.