SQL query to get reserved words used in postgres sql db - sql

I need a SQL query that returns all the reserved words used in PostgreSQL database.

Postgres provides the set-returning function pg_get_keywords() for that:
select *
from pg_get_keywords()
The columns of the result are documented in the manual

Related

In Oracle, what does [select * from table()] mean?

I am trying to search for information on this topic in sql but search is not showing me results.
SELECT * FROM TABLE(package/procedure/function);
Which topic does the above sql statement come under? Can I have a link to a doc?
The TABLE collection expression is described here in the Oracle documentation.
In short, it is used to convert a collection or pipelined function into a table that can be queried by a SELECT statement.
Typically, the collection must be of a datatype that is defined at the database level (i.e. a datatype that was created by a create or replace type ... statement).
e.g.
select *
from table(sample_pkg.func_that_rtrns_array);

How to store the result of select statement into the temporary table in Oracle?

We can write select column1,column2 into #temp from tableName in SQL Server. But I am unable to write the same query in an Oracle database.
I want to store the result of select/insert/delete/update or any result set into a local temporary table in oracle database. How I can do this?
I am executing below query in my Oracle sql developer tool:
select * into #temp
from bmi;
but I am getting the error as follow please help to find this error.
when I execute the same query in Microsoft SQL Server it get executed & #temp table get created which is not present in the database but it can hold the data for that particular session. so i want same scenario in ORACLE database.
ORA-00911: invalid character
00911. 00000 - "invalid character"
*Cause: identifiers may not start with any ASCII character other than
letters and numbers. $#_ are also allowed after the first
character. Identifiers enclosed by doublequotes may contain
any character other than a doublequote. Alternative quotes
(q'#...#') cannot use spaces, tabs, or carriage returns as
delimiters. For all other contexts, consult the SQL Language
Reference Manual.
*Action:
Error at Line: 1 Column: 15
I want to store the result of select/insert/delete/update or any result set into a local temporary table in oracle database,How I can Do This?
You can't. Oracle doesn't have local temporary tables, it doesn't work like that. But it doesn't need to. Oracle has a very different internal model from SQL Server which means a lot of SQL Server practices are unnecessary in Oracle. (To be fair SQL Server has neat things which Oracle doesn't, like ANSI 92 Joins for DML.)
The key insight is: you don't want to store the result of select/insert/delete/update or any result set into a local temporary table. That is something you had to do in T-SQL to achieve the end goal of implementing some business logic. But what you actually wanted to do in SQL Server and what you want to do in Oracle is write some code which delivers value to your organisation.
So, with that mindset in place, what do you need to do?
If you want to loop round a result set then perhaps a Cursor Loop is what you're looking for?
for rec in ( select * from some_table
where the_date = date '2018-02-01' )
loop
...
If you want to work on some data prior to inserting it into a data then perhaps you should use a PL/SQL collection:
type l_recs is table of some_table%rowtype;
But maybe you just need to understand Oracle's Transaction Management model. A lot of things are possible in pure SQL without any need for procedural framework.
Create temporary table :
create global temporary table
results_temp (column1, column2)
on commit preserve rows;
and then insert to it from your table:
insert into results_temp (column1, column2 )
SELECT column1,column2
FROM source_table
create global temporary table temp_table_name
on commit preserve rows as select column1,column2,columnN from your_table;

Is there an equivalent of table wildcard functions in BigQuery with standard SQL?

In legacy SQL, users can use table wildcard functions like TABLE_DATE_RANGE, TABLE_QUERY and TABLE_DATE_RANGE_STRICT.
Is there a similar feature with standard SQL?
In legacy SQL, users can reference data from a subset of tables in a dataset using table wildcard functions. In standard SQL, users can achieve the same result using UNION ALL. However, this approach may not be convenient when users want to dynamically determine the set of tables using, for example, either a date range (supported using TABLE_DATE_RANGE and TABLE_DATE_RANGE_STRICT in legacy SQL) or other complex criteria (supported by TABLE_QUERY in legacy SQL). With Standard SQL, BigQuery offers an equivalent to this described below.
The following legacy SQL query that uses the TABLE_QUERY wildcard function can be rewritten using standard SQL.
Legacy SQL query (using TABLE_QUERY):
SELECT SUM(value1)
FROM TABLE_QUERY([myproject:mydataset],"table_id = 'mydailytable_20150105' OR
table_id = 'mydailytable_20150106' OR table_id = 'maydailytable_20150110'")
GROUP BY value2;
Legacy SQL query (using TABLE_DATE_RANGE):
SELECT SUM(value1)
FROM TABLE_DATE_RANGE([myproject:mydataset], TIMESTAMP("2015-01-05"), TIMESTAMP("2015-01-10"))
Standard SQL query:
SELECT SUM(value1)
FROM `myproject.mydataset.mydailytable_*`
WHERE _TABLE_SUFFIX = '20150105'
OR _TABLE_SUFFIX = '20150106'
OR _TABLE_SUFFIX = '20150110'
GROUP BY value2;
In the above query, the wildcard table myproject.mydataset.mydailytable_* matches all tables in the dataset myproject.mydataset that have table_id starting with mydailytable_. For example, to match all tables in the dataset the user can use an empty prefix for the wildcard. So, myproject.mydataset.* matches all tables in the dataset.
Since * is a special character, wildcard table names must be quoted when using them in a query.
The _TABLE_SUFFIX pseudo column:
The _TABLE_SUFFIX pseudo column has type STRING and can be used just like any other column. It is a reserved column name, so it needs to be aliased when using it as part of the SELECT list.
Official documentation for this feature is available here:
https://cloud.google.com/bigquery/docs/wildcard-tables
https://cloud.google.com/bigquery/docs/querying-wildcard-tables

Common SQL to compare dates in SQL Server and ORACLE

I need to include a date comparison in my query where I compare a date string to a DATETIME column but i need it to work on both ORACLE and SQL Server and not have two separate queries.
Are there any date comparissons which will work on both oracle and sql?
ORACLE:
Select * from MyTable where myDate > DATE '2013-04-10'
SQL Server:
Select * from MyTable where myDate > convert(DATETIME,'2013-04-10', 126)
This portability issue only applies with literals.
I do not believe there is a fully portable way to do this because SQL Server does not support the ANSI literal DATE keyword and all the CAST, CONVERT, TO_DATE, and date functions are not identical on all platforms.
Note that your second query can also be written as Select * from MyTable where myDate > '20130410'.
It would be nice if there was support in SQL Server for the ANSI DATE literal feature (DB/2 and Teradata both have this).
I could not find a Connect item on this, nor anything about why SQL Server doesn't support the ANSI DATE, TIME and TIMESTAMP literal keywords.
I'm wondering in your scenario, whether it would be possible to use a parameter instead?
The solution in Jack's comment Common SQL to compare dates in SQL Server and ORACLE will make this code portable, but you will have to have a non-portable scalar function. This might be a viable option for you - note that in SQL Server, you will need to prefix a scalar function with its schema - this might introduce another wrinkle between the Oracle code and SQL code if you can't make the schemas the same name (note in Oracle, the prefix could be the name of a package instead of a schema if you put the function inside a package)
Is defining your own user-defined functions allowed? You could create a function which abstracts the DBMS specific code and returns a literal 'True' or 'False' result.
[MSSQL User Defined Functions: ]http://msdn.microsoft.com/en-ca/library/ms186755.aspx
[Oracle User Defined Functions]http://ocs.oracle.com/cd/B19306_01/server.102/b14200
Oracle apparently does not allow user defined functions to return a Boolean type, so a string (or numeric) result is required.

concatenate string in sql

I have a database table "create table t (s varchar, i int)" with 100 records.
When I want to sum all 'i' fields, I invoke something like "select sum(i) from t". Is there a way to concatenate the 's' fields? (select concatenate(s) from t)
In any sql dialect?
In any sql dialect?
There isn't an ANSI SQL specified way to do this across all SQL dialects. If you want specific solutions for a particular DBMS, then sure, some have a ready made solution, and others have generalized solutions that are more complicated.
e.g.
Oracle = WM_CONCAT
MySQL = GROUP_CONCAT
SQL Server = UDF / FOR XML PATH('') / recursive CTE
You need a question for each RDBMS you need the solution for, but you will find duplicate questions for each case already on StackOverflow.