oracle evalname function equivalent in postgres - postgresql

Oracle supports dynamic XMLElement name with evalname function. Is there a similar feature in postgres to get the XMLElement name dynamically instead of using constant?
Example in ORACLE:
select xmlelement(evalname(ENAME),EMPNO) from EMP;
This statement will result in list of enames as separate xml elements.
<SMITH>7369</SMITH>
<ALLEN>7499</ALLEN>
<WARD>7521</WARD>
Not sure if postgres has something similar.
Thanks.

I was able to get a workaround to construct xml with dynamic element names in Postgres using execute format. Posting this just in case if anyone had same issue.
execute format('SELECT XMLElement(NAME %I, $1)', emp_name) USING empno from emp;
<SMITH>7369</SMITH>
<ALLEN>7499</ALLEN>
Same worked with XMLForest and having XMLAttributes inside XMLElement.

There is no such function as far as I know.
The closest you can get is adding an attribute with the empname:
select xmlelement(name emp, xmlattributes(empname), empno)
from emp;
Generates:
<emp empname="Smith">7369</emp>
<emp empname="Allend">7499</emp>
<emp empname="Ward">7521</emp>
Personally I would find that format much easier to parse e.g. in XSLT or an XML parser. Because in order to process a tag you would need to know the tag name, which you don't if the tag changes for each row - but this might just be me.

Related

PostgreSQL, allow to filter by not existing fields

I'm using a PostgreSQL with a Go driver. Sometimes I need to query not existing fields, just to check - maybe something exists in a DB. Before querying I can't tell whether that field exists. Example:
where size=10 or length=10
By default I get an error column "length" does not exist, however, the size column could exist and I could get some results.
Is it possible to handle such cases to return what is possible?
EDIT:
Yes, I could get all the existing columns first. But the initial queries can be rather complex and not created by me directly, I can only modify them.
That means the query can be simple like the previous example and can be much more complex like this:
WHERE size=10 OR (length=10 AND n='example') OR (c BETWEEN 1 and 5 AND p='Mars')
If missing columns are length and c - does that mean I have to parse the SQL, split it by OR (or other operators), check every part of the query, then remove any part with missing columns - and in the end to generate a new SQL query?
Any easier way?
I would try to check within information schema first
"select column_name from INFORMATION_SCHEMA.COLUMNS where table_name ='table_name';"
And then based on result do query
Why don't you get a list of columns that are in the table first? Like this
select column_name
from information_schema.columns
where table_name = 'table_name' and (column_name = 'size' or column_name = 'length');
The result will be the columns that exist.
There is no way to do what you want, except for constructing an SQL string from the list of available columns, which can be got by querying information_schema.columns.
SQL statements are parsed before they are executed, and there is no conditional compilation or no short-circuiting, so you get an error if a non-existing column is referenced.

Get all except built-in schema names from a specific database using T-SQL query

As the title entails, is this possible?
Using the following code returns a list of the built-in schemas as well as all the other schemas:
select name from MyDbName.sys.schemas
What I want is only the schemas I created for each table. I also tried doing the following code but its not very solid.
select name from MyDbName.sys.schemas where name not like '%db_%'
select distinct SCHEMA_NAME(schema_id) from sys.objects
I think this will work for you.

Execute SQL query in Binary Form

I have seen executing something like this in SQL Server
EXEC (0x53454C45435420312041532054)
or simply like
0x53454C45435420312041532054
Above binary form is equal to SELECT 1 AS T
But I don't remember the exact way how to do this.
Does anyone know executing query like this?
Update:
I know how to convert Binary into Varchar and Varchar into Binary. What I am asking here is how to execute the query in Binary Form?
This is one way,
Declare #q as nvarchar(1000)
-- 0x530045004C004500430054002000310020004100530020005400 = SELECT 1 AS T
SET #q = CAST(0x530045004C004500430054002000310020004100530020005400 as nvarchar(1000));
EXEC (#q)
Any other way?
No, I am pretty sure there is no other way to do this.
You cannot simply execute binary data.
You can use exec, as you already do, or you could use sp_executesql, but both expect you to pass the query to be executed as a string (NVARCHAR).

How to get data from specific column in firebird using rdb$field_name

how to get data from a specific column in fire bird? something like.
select from rdb$relation_fields
where rdb$relation_name = 'table' and rdb$field_name = 'code'
Your question doesn't quite make sense to me - if you already know the table and field name (as you do in your example) then why not select directly from the table? Anyway, you can create SQL statement dynamically in PSQL as string and then execute it using EXECUTE STATEMENT. The EXECUTE BLOCK might also be of intrest, depends where and what exactly youre tring to achieve.
EDIT after reading the comment
So just build the SELECT statement at the client side, selecting the field currently selected in combobox. You don't mention the language you use but generaaly it something like
query.SQL := 'SELECT '+ comboField.Text +' FROM '+curTableName;
query.Open();
// read the resultset

Refer to column/table names using strings?

is it possible to refer to a column/table name by using a string? Something like SELECT * FROM 'my_table'::table_name_t?
The reason I'm asking: I have a table geometry_columns with some geometry tables. And I would like to know which objects are within a certain radius.
Thanks, Philip
You will need a (stored) function to achieve this. The function takes the table name as an argument, creates the SQL dynamically and then returns the result of the SELECT based on that query.
here are some examples (not exactly what you need, but they should get you headed in the right direction):
http://forums.devshed.com/postgresql-help-21/plpgsql-variable-representing-table-name-137201.html
Dynamic column in SELECT statement postgres
I don't think you can do that directly. I think you would have to build the select statement from another statement or piece of code, then execute the resulting statement.