Bind variables in the from clause for Postgres - sql

I'm attempting to write an extension for SQL Developer to better support Postgres. These are just XML configuration files with SQL snippets in them. To display the values for a postgres sequence, I need to run a simple query of the following form:
select * from schema.sequence
The trouble with this is that the Oracle SQL Developer environment provides the correct schema and node (sequence) name as bind variables. This would mean that I should format the query as:
select * from :SCHEMA.:NAME
The trouble with this is that bind variables are only valid in the select clause or the where clause (as far as I'm aware), and using this form of the query returns a "syntax error at or near "$1" error message.
Is there a way to return the values in the sequence object without directly selecting them from the sequence? Perhaps some obtuse joined statement from pg_catalog tables?

Try this:
select *
from information_schema.sequences
where sequence_name = :name
and sequence_schema = :schema;
It's not exactly the same thing as doing a select from the sequence, but the basic information is there.

Related

Accessing PostgreSQL Fields via Indexing

Is there a way in PostgreSQL to access fields without explicitly providing the column name? For example, instead of the statement:
select (col1,col2,col3,...col42) from foo_table;
there's a (possible) alternative of:
select (1:42) from foo_table;
There is no such syntax in Postgres' SQL.
You could always resort to having code that dynamically constructs the query for you before executing it, but that's about as good as it will get.

order by direction as a parameter

i want to sort my result based on parameter:
select ... group by some_column :paramter
and the parameter should be asc or desc. but when i try it, i get error:
ERROR: syntax error at or near "$6"
is it possible in postgres? if i send the query from the server, i can just concatenate the query but what if it's inside a stored procedure? i would prefer to avoid concatenating strings to build a query
SQL parameters cannot be placed in that location. They are meant to apply scalar values to the SQL template, but not to modify the SQL query structure itself.
What you need is to implement "Dynamic SQL" here, but that's a different strategy.

What does "SELECT INTO" do?

I'm reading sql code which has a line that looks like this:
SELECT INTO _user tag FROM login.user WHERE id = util.uuid_to_int(_user_id)::oid;
What exactly does this do? The usual way to use SELECT INTO requires specifying the columns to select after the SELECT token, e.g.
SELECT * INTO _my_new_table WHERE ...;
The database is postgresql.
This line must appear inside of a PL/pgSQL function. In that context the value from column tag is assigned to variable _user.
According to the documentation:
Tip: Note that this interpretation of SELECT with INTO is quite different from PostgreSQL's regular SELECT INTO command, wherein the INTO target is a newly created table.
and
The INTO clause can appear almost anywhere in the SQL command. Customarily it is written either just before or just after the list of select_expressions in a SELECT command, or at the end of the command for other command types. It is recommended that you follow this convention in case the PL/pgSQL parser becomes stricter in future versions.

SQL select from table with alias (Oracle9i)

According to Oracle documentation for statement SELECT it should be possible to alias table names with aliases without or with keyword AS. However, aliasing tables with keyword AS leads to error:
ORA-00933: SQL command not properly ended
For example, the following statement fails with the above error:
SELECT COUNT(*) FROM MY_TABLE AS A;
Once keyword AS is removed it executes as expected.
Could anyone please comment on this. Is there a way to make the application of AS for table aliasing work?
P.S. I'm using a code generation utility that translate some Java code into SQL statements at runtime. This utility enforces the use of aliases with AS.
Oracle does not accept AS for table aliases and I see no way to make it work.
Can't you do anything in Java? AS for column aliases is optional in Oracle, so you could look for all " AS " in the generated string and remove them (thus removing AS for column aliases as well as for table aliases). Is this an option?

Parse Oracle Query using perl

I have to perform lexical analysis on the oracle query and separate the query to various parts (based on the clauses)in perl. For example,Consider :
Select deleteddate,deletedby from temptable where id = 10;
I need to print
select : deleteddate , deletedby
from : temptable
where : id = 10
I used this code snippet :
my $parser= SQL::Statement->new();
$parser->{PrinteError}=1;
my $query = SQL::Statement->new("select deleteddate,deletedby from temptable where id =10",$parser);
my #columns = $query->columns();
print $columns[0]->name();
Though this prints deleteddate, this fails when i give a subquery inside the select clause:
Select deleteddate,deletedby,(select 1+1 from dual) from temptable where id = 10;
Can you please point me in the correct direction.
Thanks.
It looks to be a limitation of that package; it seems to be a general purpose parser and not something that can understand advanced features like subqueries or Oracle-specific constructs like "from dual".
What are the constraints of your system? If python is an option it looks like this is a more fully-featured library:
http://code.google.com/p/python-sqlparse/
The other option would be to use the actual Oracle database, if that's an option. You would:
use the DBI and DBD::Oracle modules to create a connection to Oracle & get a database handle,
create a statement handle by calling prepare() on the database handle using your query,
execute the query (there may be an option in Oracle to execute in "test only" or "parse only" mode),
examine the statement handle (such as the NAMES_hash property) to get the column names.
Otherwise it seems the SQL::Statement module unfortunately just isn't up to the task...