Sqlite "WITH" syntax error - sqlite

I have this code, with this error: "Query Error: near "WITH": syntax error Unable to execute statement" from both SqliteMan and from Node-webkit (latest).
WITH RECURSIVE FT_CTE(*) AS (
SELECT pID, cID FROM brFTNode_Children
WHERE pID = 1
UNION ALL
SELECT e.pID, e.cID FROM brFTNode_Children e
INNER JOIN FT_CTE ftCTE ON (ftCTE.cID = e.pID)
)
SELECT * FROM FT_CTE;
The Sqlite document clearly stated that it supports "WITH" statement.
Any idea?

Related

Creating a sqlite table in terminal (iOS)

sqlite> create table t
...> SELECT * FROM sqlite_master WHERE type='table' AND name='t';
Error: near "SELECT": syntax error
sqlite>
I'm trying to create a table called 't' in terminal. I'm getting the error. Maybe I'm missing somewhere?
The syntax you need to use here is CREATE TABLE AS ... SELECT:
> CREATE TABLE t AS SELECT * FROM sqlite_master WHERE type = 'table' AND name = 't';

What's wrong with my query and what does the error mean?

I cannot figure what's wrong with this query and what the error means:
Query
select Total2014, Total2015
From 2014_Jan_Pivot
inner join 2015_Jan_Pivot on 2014_Jan_Pivot.PremiseCity2014=
2015_Jan_Pivot.PremiseCity2015
Error says:
unrecognized token: "2014_Jan_Pivot": select Total2014, Total2015, Total2016, Total2017
From 2014_Jan_Pivot
Your issue is because names in SQLite cannot start with a digit/number unless they are enclosed.
So 2014_Jan_Pivot is an invalid (aka unrecognised) token.
However, you can enclose the name and it will then be recognised e.g. :-
[2014_Jan_Pivot]
`2014_Jan_Pivot`
"2014_Jan_Pivot" or
'2014_Jan_Pivot'
So your query could be :-
SELECT
Total2014,
Total2015
FROM [2014_Jan_Pivot]
INNER JOIN [2015_Jan_Pivot] ON
[2014_Jan_Pivot].PremiseCity2014 = [2015_Jan_Pivot].PremiseCity2015

How can drop table if table exists in oracle?

I am trying to create database by my java application using my generated schema file. In schema I have included drop query also. But I want to do some improvements for DROP QUERY. So I want to check the existence of db objects before running drop query and drop only when if it exists.
I googled for it and found some oracle link, Some link suggest following syntax and some mentioned that ORACLE does not support such syntax.
SYNTAX A:
IF EXISTS DROP TABLE TABLE_NAME
SYNTAX B:
DROP [TEMPORARY] TABLE [IF EXISTS]
tbl_name [, tbl_name] ...
[RESTRICT | CASCADE]
I also tried following queries:-
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' )
DROP TABLE [BBB]
but it was giving error:-
Error starting at line 2 in command:
DROP TABLE [BBB]
Go
Error report:
SQL Error: ORA-00903: invalid table name
00903. 00000 - "invalid table name"
*Cause:
*Action:
Error starting at line 1 in command:
IF EXISTS (SELECT * FROM dba_objects WHERE OBJECT_NAME = 'BBB' ) DROP TABLE [BBB]
Error report:
Unknown Command
I refered following links:-
https://community.oracle.com/thread/2421779?tstart=0
Please suggest me if there any other queries to drop table with condition if table exists.
Drop table with no check. If any error exists you'll never know when something went wrong.
BEGIN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
EXCEPTION
WHEN OTHERS THEN
NULL;
END;
Or you can search in Oracle dictionary.
DECLARE
l_cnt NUMBER;
BEGIN
SELECT count(*)
INTO l_cnt
FROM user_tables
WHERE table_name = 'MY_TABLE';
IF l_cnt = 1 THEN
EXECUTE IMMEDIATE 'DROP TABLE my_table';
END IF;
END;
If you run following code you do not have to check if table exists and in case of errors (table is locked with now wait or any other you will know about it)
begin
for c1 in (select owner,table_name from dba_tables where table_name='MY_TABLE') loop
execute immediate 'drop table '||c1.owner||'.'||c1.table_name||'';
end loop;
end;
Try this : It will drop table 'table_name' if it is present .
declare
a varchar2(700) ;
begin
execute immediate ' SELECT CASE WHEN tab = 1
THEN ''DROP TABLE TABLE_NAME''
ELSE ''select 1 from dual''
END
FROM ( SELECT sum(case when table_name = ''TABLE_NAME'' then 1 else 0 end ) as tab FROM user_tables)' into a;
EXECUTE IMMEDIATE a;
end;

PL-SQL store query results into a variable

Good Day,
I have a PL-SQL query as follows. I'm trying to execute a query and store the results into a variable. So far I have this query which works:
SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
FROM
(SELECT
COUNT(*) AS TOTAL_COUNT
FROM DEV.BASEOBJECT INNER JOIN
DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
GROUP BY DEV.BASEOBJECT.ID,
DEV.BASEOBJECT.FIRST_NAME,
DEV.BASEOBJECT.LAST_NAME,
DEV.BASEOBJECT.CITY,
DEV.BASEOBJECT.STATE,
DEV.BASEOBJECT.ZIP,
DEV.ANIMAL.ID,
DEV.ANIMAL.NAME,
DEV.ANIMAL.BREED,
DEV.ANIMAL.DATE_OF_BIRTH,
DEV.ANIMAL.GENDER,
DEV.ANIMAL.SPECIES
HAVING (COUNT(*) > 1));
But when I try to save the results into a variable with this query:
DECLARE
v_name VARCHAR2(2);
BEGIN
SELECT DECODE(COUNT(*), 0, 'N', 'Y') REC_EXISTS
INTO v_name
FROM
(SELECT
COUNT(*) AS TOTAL_COUNT
FROM DEV.BASEOBJECT INNER JOIN
DEV.ANIMAL ON DEV.BASEOBJECT.ID = DEV.ANIMAL.BASEOBJECT_ID
GROUP BY DEV.BASEOBJECT.ID,
DEV.BASEOBJECT.FIRST_NAME,
DEV.BASEOBJECT.LAST_NAME,
DEV.BASEOBJECT.CITY,
DEV.BASEOBJECT.STATE,
DEV.BASEOBJECT.ZIP,
DEV.ANIMAL.ID,
DEV.ANIMAL.NAME,
DEV.ANIMAL.BREED,
DEV.ANIMAL.DATE_OF_BIRTH,
DEV.ANIMAL.GENDER,
DEV.ANIMAL.SPECIES
HAVING (COUNT(*) > 1));
END
I get an error:
ERROR
ORA-06550: line 1, column 8:
PLS-00103: Encountered the symbol "" when expecting one of the following:
begin function package pragma procedure subtype type use
<an identifier> <a double-quoted delimited-identifier> form
current cursor
The symbol "" was ignored.
Eventually, this is going to be a stored procedure, but I don't have that part ready yet, because I wanted to verify that what I have is working so far.
TIA,
coson
How are you running this? What tools are you using? I think that the problem is your tool/IDE is trying to compile this as a function/procedure. This code looks ok and would probably run with no problems if you ran it as a script in SQL*Plus. If you run this as a script, you should add a dbmbs_output.put_line(v_name) after the query to see the result.

Join query exception in Symfony 1.4.11/Propel 1.4.2

I need to run following query:
SELECT
m.TITLE,
m.MOMENTOIMAGE,
s.CREATED_AT,
s.UNREAD,
mem.FIRSTNAME,
mem.LASTNAME,
mem.MEMBER_PHOTO,
mem.ID
FROM `momento_send` s,
`send_distribution` sd,
`momento_distribution` d,
`momento` m,
`member` mem
WHERE
s.momento_idmember=6 AND
sd.id_send=s.id AND
sd.id_distribution=d.id AND
d.momento_id=m.id;
For that, I wrote following code in Symfony 1.4 (using Propel 1.4.2) (Thanks to #j0k)
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(MomentoPeer::TITLE);
$c->addSelectColumn(MomentoPeer::MOMENTOIMAGE);
$c->addSelectColumn(MomentoSendPeer::CREATED_AT);
$c->addSelectColumn(MomentoSendPeer::UNREAD);
$c->addSelectColumn(MemberPeer::FIRSTNAME);
$c->addSelectColumn(MemberPeer::LASTNAME);
$c->addSelectColumn(MemberPeer::MEMBER_PHOTO);
$c->addSelectColumn(MemberPeer::ID);
$c->addJoin(SendDistributionPeer::ID_SEND, MomentoSendPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(SendDistributionPeer::ID_DISTRIBUTION, MomentoDistributionPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_ID, MomentoPeer::ID, Criteria::INNER_JOIN);
$c->addJoin(MomentoDistributionPeer::MOMENTO_IDMEMBER, MemberPeer::ID, Criteria::INNER_JOIN);
$c->add(MomentoSendPeer::MOMENTO_IDMEMBER, $memberid);
//echo $c->toString();exit;
$records = SendDistributionPeer::doSelect($c);
Running this code generated following error
[wrapped: 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 'INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID) INNER JOI' at line 1]
Can someone suggest, what is the issue.
Just to give little more info, SQL printed with echo $c->toString();exit; was
SELECT momento.TITLE,
momento.MOMENTOIMAGE,
momento_send.CREATED_AT,
momento_send.UNREAD,
member.FIRSTNAME,
member.LASTNAME,
member.MEMBER_PHOTO,
member.ID
FROM
INNER JOIN momento_send ON (send_distribution.ID_SEND=momento_send.ID)
INNER JOIN momento_distribution ON (send_distribution.ID_DISTRIBUTION=momento_distribution.ID)
INNER JOIN momento ON (momento_distribution.MOMENTO_ID=momento.ID)
INNER JOIN member ON (momento_distribution.MOMENTO_IDMEMBER=member.ID)
WHERE momento_send.MOMENTO_IDMEMBER=6
Try with $c->setPrimaryTableName(MomentoPeer::TABLE_NAME);

Resources