Oracle CONNECT_BY query. - sql

SELECT LPAD('*', 2*level-1)||SYS_CONNECT_BY_PATH(unit_data, '/') "battle_unit_id"
FROM battle_units
START WITH battle_unit_id= 600
CONNECT BY PRIOR parent_id = battle_unit_id;
returns
/Doctrine
/Doctrine/Air
/Doctrine/Air/Jet powered aircraft
/Doctrine/Air/Jet powered aircraft/F-16
All i want is just /Doctrine/Air/Jet powered aircraft/F-16 without the other three results. Is there a way for that?
Edit:
My oracle version:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production

If I understand you correctly, you just want the leafs of the tree:
SELECT LPAD('*', 2*level-1)||SYS_CONNECT_BY_PATH(unit_data, '/') "battle_unit_id"
FROM battle_units
WHERE connect_by_isleaf = 1 -- <<< this selects only the leaf nodes
START WITH battle_unit_id= 600
CONNECT BY PRIOR battle_unit_id = parent_id;
See the manual for details: http://docs.oracle.com/cd/E11882_01/server.112/e26088/pseudocolumns001.htm#SQLRF00251

If you know the exact level where that line is going to occur this is doable.
Please see this fiddle.
Edit:
Since you are going for the max, this may work for you.

Related

Oracle SQL alternative to using DEFINE

I'm currently using
Oracle Database 12c Enterprise Edition Release 12.1.0.2.0
PL/SQL Release 12.1.0.2.0
The following doesn't work for me ...
-- Since PL/SQL 20.2.0.175
DEFINE usr = 'YourName';
SELECT * FROM Department WHERE CreatedBy = '&usr';
Can someone give me an equivalent for my version?
Thanks!

How would I obtain the versions/dialects of SQL used for these online SQL editors?

I am learning SQL by running test queries using online SQL editors below.
https://www.w3schools.com/sql/trysql.asp?filename=trysql_asc
https://www.mycompiler.io/new/sql
http://sqlfiddle.com/
I've noticed that each editor has some different keywords/syntax that it supports/ doesn't support. For example, 1 supports "WITH temp_table AS (some_query) SELECT ... " which I couldn't get to work on the other two. Also, 3 supports copying a table using "CREATE TABLE new_table SELECT * FROM old_table" which I couldn't get to work on the other 2.
Can someone shed some light on the different dialects of SQL and which are used for these online editors?
WITH is not supported in SQLite until v3.8.3, and not on MySQL until v8, which is why that only works on 1, and SQLite lacks the SELECT INTO ability, which is why that only works on 3. Some of those SQL engines on sqlfiddle should support the WITH construction, though. I tested with this:
WITH temp_table AS (select * from test)
SELECT * FROM temp_table;
and every engine allowed it EXCEPT mySQL, which is expected because it didn't support WITH in 5.6.
Thanks to Jeroen Mostert for pointing out that not every browser supports WebSQL, so the version shown for those (the SQLite engines) will depend.
Number 1 (w3schools) as of 11/24/2021, SQLite 3.36.0.
Number 2 (mycompiler), SQLite 3.7.17.
Number 3 (sqlfiddle), lets you choose the engine, and they are:
MySQL 5.6.48;
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production;
PostgreSQL 9.6.17 on x86_64-pc-linux-gnu (Debian 9.6.17-2.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit;
PostgreSQL 9.3.25 on x86_64-pc-linux-gnu (Debian 9.3.25-1.pgdg90+1), compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit;
SQLite Web SQL: 3.36.0 (this is using whatever your browser uses, as pointed out by Jeroen Mostert in comments);
SQLite js: 3.15.1;
Microsoft SQL Server 2017 (RTM-CU2) (KB4052574) - 14.0.3008.27 (X64) Nov 16 2017 10:00:49 Copyright (C) 2017 Microsoft Corporation Express Edition (64-bit) on Linux (Ubuntu 16.04.3 LTS);
And I determined these with the following methods:
MySQL, SQL Server: SELECT ##version;
Oracle: SELECT * FROM V$VERSION;
PostGRE: SELECT version();
SQLite: SELECT sqlite_version()

SP2-0552: Bind variable not declared - Oracle Version Dependant

There are lots of solution for the same error in PLSQL. I am not facing this in PLSQL, but in SQL. This happens in one version of oracle and not in another.
I have created a table create table test(id number(10), empid number(10))
Now when I am inserting data
SQL> insert into test(id,empid) values (1, &empid)
2 /
SP2-0552: Bind variable "EMPID" not declared.
SQL>
The details of the database installation are
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production
PL/SQL Release 12.2.0.1.0 - Production
CORE 12.2.0.1.0 Production
TNS for 64-bit Windows: Version 12.2.0.1.0 - Production
NLSRTL Version 12.2.0.1.0 - Production
Now the same query is working fine and data is getting inserted in
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
PL/SQL Release 11.2.0.4.0 - Production
CORE 11.2.0.4.0 Production
TNS for 64-bit Windows: Version 11.2.0.4.0 - Production
NLSRTL Version 11.2.0.4.0 - Production
EDIT :
I did not do any "define" anywhere. It happens in new sessions also. This 12c version was previously updated from 11g. Define shows
SQL> define
DEFINE _DATE = "06-AUG-18" (CHAR)
DEFINE _CONNECT_IDENTIFIER = "mydbname" (CHAR)
DEFINE _USER = "myusername" (CHAR)
DEFINE _PRIVILEGE = "" (CHAR)
DEFINE _SQLPLUS_RELEASE = "1101000600" (CHAR)
DEFINE _EDITOR = "Notepad" (CHAR)
DEFINE _O_VERSION = "Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production" (CHAR)
DEFINE _O_RELEASE = "1202000100" (CHAR)
DEFINE 1 = "to" (CHAR)
DEFINE 2 = "be" (CHAR)
DEFINE 3 = "pathToMyInstallation\product\11.1.0\client_1\sqlplus\admin\glogin.sql" (CHAR)
SQL>

Oracle WITH clause returns no data

I am trying to use a WITH clause in Oracle, but it is not returning any data.
This is the query I am trying to run...
with test as
(select count(*)
from my_table)
select *
from test;
When I run this code, I get back the count of the records in my_table
select count(*)
from my_table
I am on Oracle 10g so the query should work...
select * from v$version;
yields
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Could it a permissions issue or something?
*EDIT: *
I believe my question is clear. Using the WITH statement will not return any records for me, even though the "select count(*) from my_table" statement inside the WITH statement works correctly, which would lead me to believe that there is another issue that I am unable to figure out, hence this question :)
EDIT 2
OK, so if I try and execute the query from a linked server from SQL server management studio I get some error information back:
sg 7357, Level 16, State 2, Line 1
Cannot process the object "with test as
(select count(*)
from v$version)
select * from test;". The OLE DB provider "MSDAORA" for linked server "MyServer" indicates that either the object has no columns or the current user does not have permissions on that object.
Maybe the optimizer is materializing the count query (dumb, I agree). It's a shot in the dark but do you have these privileges?
grant query rewrite to youruser;
grant create materialized view to youruser;
Try giving the aggregate an alias name.
with test as
(select count(*) as MyCount
from my_table)
select MyCount
from test;
The following worked just fine for me (10gR2)
SQL> with test as
2 (select count(*)
3 from user_tables)
4 select *
5 from test;
COUNT(*)
----------
593
SQL>
What client are you using?
This question is confusing. Are you saying you are or are not getting back the count from my_table?
You should be getting back the count because that's exactly what you asked for in the with clause.
It's analogous to writing:
select *
from (select count(*) from my_table);
Some people at my company ran into this the other day - we traced it down to the Oracle client version [and thus the OCI.dll] version that was being picked up by PL/SQL developer. Some of our dev PCs had Oracle 8 (!) client installs still knocking around on them as well as more recent versions.
The symptom was that not only were queries written using a WITH clause returning no rows, they were returning no columns either! If you manually set the app to pick up the Oracle 11 oci.dll then it all worked.
I think what is going on is that Oracle 8 predates the WITH clause (introduced in Oracle 9, and enhanced subsequently). Now, mostly you can get different versions of the Oracle client and server to talk to one another. However because the client has a certain amount of 'intelligence', it is supposed to be semi-aware of what sort of operation it is submitting to the database, and so does some form of primitive parse of the SQL. As it doesn't recognize the command as a SELECT, it treats it as some unknown command [e.g. possibly a DDL command] and doesn't recognize it as returning a resultset. If you turn on SQL_TRACE for the session you can see the SQL gets PARSEd and EXECUTEd fine on the server, but that no calls to FETCH are made.
I had a similar thing myself recently when trying to use the new WITH syntax in Oracle 12 that allows an inline function definition. If you try simple examples using an Oracle 11 thick client-based application, such as PL/SQL developer or SQL*Plus, then you get an error. If you use an Oracle 12 client, or a thin-client application that doesn't rely a client-side install, then it works.

Detemining a database's OS with a SQL query?

I'm writing a tool to gather customer configuration information. One of the questions I want to answer, what OS is the customer database running on.
I haven't found a generic way to find the OS with SQL and I can't create stored procedures on the customer's database.
If there is a way, it's probably vendor specific.
Suggestions? Thanks in advance.
Yes, it will be vendor specific. For Oracle you can obtain it via this query:
SQL> select banner from v$version;
BANNER
----------------------------------------------------------------
Oracle9i Enterprise Edition Release 9.2.0.8.0 - 64bit Production
PL/SQL Release 9.2.0.8.0 - Production
CORE 9.2.0.8.0 Production
TNS for Solaris: Version 9.2.0.8.0 - Production
NLSRTL Version 9.2.0.8.0 - Production
The 4th row of output shows that my 9i database is running on Solaris (well, it shows that it is running "TNS for Solaris", which implies that the OS is Solaris anyway).
For Oracle, you could use
SELECT DBMS_UTILITY.PORT_STRING FROM dual;
(From Ask Tom)
how about:
select platform_name from v$database;
sybase ASE & Sybase IQ are the same as sqlserver: select ##version
eg
Sybase IQ/12.7.0/090824/P/ESD 7/Sun_Sparc/OS 5.9/64bit/2009-08-24 16:17:12
Adaptive Server Enterprise/12.5.3/EBF 12455 ESD#2/P/Sun_svr4/OS 5.8/ase1253/1904/64-bit/FBO/Wed Mar 23 03:04:04 2005