Regex put name schema for 'table' in select query syntax - sql

How can I put with regex, schema name table, for query select?
Query doesn't have fixed size.
Example:
select * from t1 union select * from t2
Result:
select * from schema1.t1 union select * from schema1.t2
Thanks!

(1) If you are using it in a code, maybe you can do a search and replace at front end as Luk suggested.
(2) Alternatively, in your current session you can set default schema as schema1 so your query would work without schema name. But it would depend on your database name.
Like in Oracle you can do
ALTER SESSION SET CURRENT_SCHEMA=schema1
Now select * from t1 would effectively mean select * from schema1.t1.
You can search how to set default schema in <your database> in google and you would get syntax for other databases as well.

Related

UNION tables with wildcard in BigQuery

I have over 40 tables I want to append in BigQuery using standard SQL. I have already formatted them to have the exact same schema. When I try to use the '*' wildcard at the end of table name in my FROM clause, I get the following error:
Syntax error: Expected end of input but got "*" at [95:48]
I ended up manually doing a UNION DISTINCT on all my tables. Is this the best way to do this? Any help would be appreciated. Thank you!
CREATE TABLE capstone-320521.Trips.Divvy_Trips_All AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_*;
--JOIN all 2020-21 trips data
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION DISTINCT
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06
UNION DISTINCT
Syntax error: Expected end of input but got "*"
I think the problem is in missing ticks around the table references. Try below
CREATE TABLE `capstone-320521.Trips.Divvy_Trips_All` AS
SELECT * FROM `capstone-320521.Trips.Divvy_Trips_*`
Note: The wildcard table name contains the special character (*), which means that you must enclose the wildcard table name in backtick (`) characters. See more at Enclose table names with wildcards in backticks
I am unaware of any such UNION DISTINCT syntax. If your intention is to do a union of the 3 tables and remove any duplicate records in the process, then just using UNION should suffice:
CREATE TABLE capstone-320521.Trips.Divvy_Trips_Raw_2020_2021 AS
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_04
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_05
UNION
SELECT * FROM capstone-320521.Trips.Divvy_Trips_2020_06;
Note that in general it is bad practice to use SELECT * with union queries. The reason has to do with that a union between two (or more) tables is generally only valid if the two queries involved have the number and types of columns. Using SELECT * obfuscates what columns are actually being selected, and so it is preferable to always explicitly list out the columns.

How to make WHERE clause case insensitive in oracle sql database?

select * from table_Name where name ="red"
I need to fetch both "red" and "RED".
For example: I need to use both upper and lower in same statement.
How can I do this?
You could use case insensitive parameter at session 'NLS_SORT=BINARY_CI'. There are two parameters at session level:
NLS_COMP
NLS_SORT
Let's see a demo:
Normal scenario:
SQL> with names as
(
select 'Vishnu' name from dual
)
-- Your query starts here
select * from names where name='vIsHnU';
no rows selected
Case insensitive approach:
SQL> alter session set nls_comp='LINGUISTIC';
Session altered
SQL> alter session set nls_sort='BINARY_CI';
Session altered
SQL> with names as
(
select 'Vishnu' name from dual
)
-- Your query starts here
select * from names where name='vIsHnU';
NAME
------
Vishnu
One more example:
SQL> with names as
(
select 'red' name from dual union all
select 'RED' from dual
)
-- Your query starts here
select * from names where name='rEd';
NAME
----
red
RED
To improve the performance, you could also create a case insensitive INDEX.
For example:
create index names_ci_indx on names(NLSSORT(name,'NLS_SORT=BINARY_CI'));
Now, there are ways to improve performance of above transaction. Please read Oracle – Case Insensitive Sorts & Compares
select * from table_Name where lower(name) ="red"
or
select * from table_Name where upper(name) ="RED"
This cannot use an index on name. It might be appropriate to create a function based index on the expression used.

Use select query for the time as of in SQL Server temporal table

I have a temporal table in my database which allows me to do things like:
select *
from tableName as of '01-nov-2019'
I can also do this:
declare #dateToView dateTime
select #dateToView = top 1 dateColumn
from tableHoldingDates
select *
from tableName as of #dateToView
However, I'm rewriting a View, which uses temporal tables but cannot declare variables.
I'd like to try to write something like:
select *
from tableName as of (top 1 dateColumn from tableHoldingDates)
But cannot. Does anyone know whether it's possible to use a select for the 'as of' of the temporal tables?

Oracle: Query identical table in multiple schema in single line

In Oracle, is there a way to query the same, structurally identical table out of multiple schema within the same database in single line? Obviously assuming the user has permissions to access all schema, I could build a query like:
select * from schema1.SomeTable
union all
select * from schema2.SomeTable
But is it possible, given the right permissions to say something like:
select * from allSchema.SomeTable
...and bring back all rows for all the schema? And related to this, is it possible to pick which schema, such as:
select * from allSchema.SomeTable where schemaName in ('schema1','schema2')
The simplest option, as far as I can tell, is to create a VIEW (as UNION of all tables across all those users), and then SELECT FROM VIEW.
For example:
create or replace view my_view as
select 'schema_1' source_schema, id, name from schema_1.table union
select 'schema_2' source_schema, id, name from schema_2.table union
...
-- select all
select * from my_view;
-- select all that belongs to one of schemas
select * from my_view where source_schema = 'schema_1';

How can I Retrieve a List of All Table Names from a SQL Server CE database?

Is there some SQL that will either return a list of table names or (to cut to the chase) that would return a boolean as to whether a tablename with a certain pattern exists?
Specifically, I need to know if there is a table in the database named INV[Bla] such as INVclay, INVcherri, INVkelvin, INVmorgan, INVgrandFunk, INVgobbledygook, INV2468WhoDoWeAppreciate, etc. (the INV part is what I'm looking for; the remainder of the table name could be almost anything).
IOW, can "wildcards" be used in a SQL statement, such as:
SELECT * tables
FROM database
WHERE tableName = 'INV*'
or how would this be accomplished?
This should get you there:
SELECT *
FROM INFORMATION_SCHEMA.TABLES
where table_name LIKE '%INV%'
EDIT:
fixed table_name
To check for exists:
--
-- note that the sql compiler knows that it just needs to check for existence, so this is a case where "select *" is just fine
if exists
(select *
from [sys].[tables]
where upper([name]) like N'INV%')
select N'do something appropriate because there is a table based on this pattern';
You can try the following:
SELECT name FROM sys.tables where name LIKE 'INV%';