I need to change my Oracle query to SQLite.
It is some kind of calendar.
Oracle query, which works fine:
SELECT TRUNC(sysdate,'DD') - level AS d
FROM dual
CONNECT BY level <= 180
SQLite query, which I have written:
WITH RECURSIVE
dates(day_date) AS (
SELECT date('now','-180 day')
UNION ALL
SELECT day_date+1
FROM dates WHERE day_date < date('now')
)
select * from dates;
It throws an error, when I am executing it.
Error while executing query: near "WITH": syntax error
What is wrong with my code? I used this page to check syntax: https://www.sqlite.org/lang_with.html
Common table expressions are not available before SQLite version 3.8.3.
Related
I know this isn't a specific bit of code or problem, but I am having trouble with a very similar issue to the person asking this (except theirs is for SQL Server): Combining INSERT INTO and WITH/CTE ...and I can't seem to find it out there on any SAP HANA help forums etc. so thought there may be an expert on here who can just give me a simple yes or no answer.
The SQL statement I am using contains multiple CTEs, but when I try to insert it tells me there is a Syntax error around the word INSERT. It is definitely laid out exactly the same as in the question I've linked above (spent hours checking), and I can post code samples if necessary but I simply want to know whether it is supported first! Thanks
Short answer:
No, CTEs are not supported for INSERT/UPDATE statements.
Longer answer:
SQLScript's INSERT/UPDATE commands are actually "borrowed" SQL commands as the documentation explains.
Checking the documentation for SQL INSERT we find that it supports a subquery as a source of values.
The subquery term is defined as part of the SQL SELECT statement. Checking the documentation for SELECT shows that <subquery> and <with_clause> are different, non-overlapping terms.
This means, that CTEs cannot be used in subqueries and therefore not be part of the subqueries used in INSERT/UPDATE commands.
You can, however, use SQLScript table variables in INSERT statements in your SQLScript blocks, which is very similar to CTEs:
DO BEGIN
te_a := SELECT 10, 'xyz' as VAL from dummy;
te_b := SELECT 20, 'abc' as VAL from dummy;
te_all := SELECT * from :te_a
UNION ALL SELECT * from :te_b;
INSERT INTO VALS
(SELECT * from :te_all);
END;
You can convert the CTE into a Sub-Select statement in many cases
You can use following
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from (
-- CTE Expression as subselect
select * from city
-- end (CTE)
) cte
Instead of using following valid CTE command combined with INSERT (on SQL Server)
with cte as (
select * from city
)
insert into city (city, countryid, citycode)
select
city, countryid, citycode
from cte
SAP HANA includes this posibility, the order of the code is different than SQL Server:
INSERT INTO EXAMPLE (ID)
WITH cte1 AS (SELECT 1 AS ID FROM DUMMY)
SELECT ID FROM cte1;
I'm using SQlite in an Windows application (done with Visual C#); while inserting columns into the table I'm getting the following error:
Error while executing SQL query on database 'database': row value misused
The following is my insertion query:
INSERT INTO d_logindetails (userid,registration_no,logintime,expected_logout,machinesno,is_uploaded)VALUES (234,'1233',CURRENT_TIMESTAMP,(CURRENT_TIMESTAMP,'+60 minutes'),'s12452',0);
'+60 minutes' is just a string.
When used with the built-in date/time functions, it is interpreted as a modifier.
So to do this computation, you have to call such a function:
INSERT ... VALUES (..., CURRENT_TIMESTAMP, datetime('now', '+60 minutes'), ...);
I'm guessing it's the part where you have the following:
(CURRENT_TIMESTAMP,'+60 minutes')
Personally I'd just do:
dateadd(HOUR, 1, getdate())
Really enjoyed using Rmarkdown/Knitr execution of SQL chunks recently, however it seems there is a limitation for more complex queries. I've been using a Redshift data base with an RJDBC powered connection.
Specifically using Common Table expressions with INNER JOIN:
```{sql, connection=redshift, output.var="Field_count"}
WITH
cte AS (
SELECT DISTINCT field
FROM
table
WHERE date >= '2017-01-01'
)SELECT count(DISTINCT field)
FROM cte
INNER JOIN table_2 t2 ON t2.join_here = cte.join_here;
```
I've successfully used CTE and INNER JOIN in individual queries, but combining them leads to an error:
Error in .verify.JDBC.result(r, "Unable to retrieve JDBC results set for ", : Unable to retrieve JDBC result set for WITH
cte AS (
SELECT DISTINCT field
FROM
table
WHERE date >= '2017-01-01'
)SELECT count(DISTINCT field)
FROM cte
INNER JOIN table_2 t2 ON t2.join_here = cte.join_here; ([Amazon] (500335) One query is expected: WITH
cte AS (
SELECT DISTINCT field
FROM
table
WHERE date >= '2017-01-01'
)SELECT count(DISTINCT field)
FROM cte
INNER JOIN table_2 t2 ON t2.join_here = cte.join_here;) Calls: <Anonymous> ... dbSendQuery -> dbSendQuery -> .local -> .verify.JDBC.reslt Execution Halted
It appears that this is being translated wrongly/duplicated on the querying out of the RStudio session.
Is this a known limitation? If so is it documented anywhere?
Is this a known bug? If so is it in an existing issue?
EDIT: This is only visibly happening within the knit process, individually running chunks interactively returns expected results.
I'd suggest trying another version on Redshift JDBC driver.
I got the same error, however, it was while using Pentaho so mileage may vary. I didn't find any documentation or other info related to this. My problem got solved by switching away from the newest driver version. I was using RedshiftJDBC4-1.2.1.1001.jar (JDBC 4.0 compatible) and switched to RedshiftJDBC4-1.1.10.1010.jar.
I want to select data from table using native query and DATE_ADD but always I got an errors,
this is my sql request:
$qb = $this->_em->createNativeQuery("select Coalesce (sum(p.nb_sms*p.nb_destination),0) as multiplesms , sum(p.nb_fax) as nbFax, sum(p.nb_Mail) as nbMail FROM push p WHERE p.id_user=$id and DATE_ADD(p.date_send,7,'DAY') > CURRENT_DATE() and p.statut>0 order by p.date_send Desc", $rsm);
I got an error
SQLSTATE[42000]: Syntax error or access violation: 1064 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 '7,'DAY') > CURRENT_DATE() and p.statut>0 order by p.date_send Desc' at line 1
any idea please
mistake in DATE_ADD function syntax
change DATE_ADD(p.date_send,7,'DAY') to DATE_ADD(p.date_send, INTERVAL 7 DAY)
I am trying to convert column data to xml format, but I get this error message:
The query fails because all columns types are currently not supported.
CREATE TABLE EMP(NAME VARCHAR2(10 BYTE))
INSERT INTO EMP VALUES ('C');
INSERT INTO EMP VALUES ('A');
INSERT INTO EMP VALUES ('T');
SELECT xmlelement("NAME",NAME) FROM EMP;
I am using:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
SQLTools 1.5.0 Beta build 9 as EDITOR
Why is this error arising??? What is the solution for this?
I've found the answer:
select dbms_xmlquery.getxml('select * from EMP') from dual;
This is more of a workaround and not a solution.
I was having the same problems as sam - also running a SELECT xmlelement statement, also using SQLTools. One difference is that I was running Oracle DB version 11.2.0.2.0.
I found that if I ran the statement in SQLPlus, it was able to display the result.
SQL> SELECT XMLELEMENT("name",ename) FROM scott.emp WHERE ROWNUM < 3;
XMLELEMENT("NAME",ENAME)
--------------------------------------------------------------------------------
<name>SMITH</name>
<name>ALLEN</name>
If I ran the statement in SQL Developer, it tried to display the results, but only showed (XMLTYPE).