Select the same multiple columns from multiple tables - sql

I am trying to extract specific columns of data from multiple tables which all have the same columns in a single sql query.
As a long winded method I am using the below query:
select application, service, serviceid, item
from table10_01
where service IN ('SERVICE12','SERVICE204') and application = 'My Application'
union
select application, service, serviceid, item
from table10_02
where service IN ('SERVICE12','SERVICE204') and application = 'My Application';
Is there a way I can use the table names available in the information_schema.tables to go through all tables? So where I can just select table wildcard like table10_% in one select query?
I understand i can script this query into a for loop but i was hoping to make the select query directly.
thanks you!

That's not possible with regular SQL. You can either use PL/pgSQL or do it in a loop with the programming language you're using.
You can refer to this link, there's an example of some functions written in PL/pgSQL to select from all tables.

Related

Query with multiple schemas - Dbeaver

I have 5 schemas under one db. 3 of them have exactly the same tables and fields.
I want to create queries that call those 3 schemas at once.
Im using DBeaver version 7.2.2
I already checked the box 'use global search' and it didn't help.
Maybe I need to do something in the sql editor itself?
for example, I want to call 'users' table and it will give me the data from all 3 tables.
I think to do that you will need to call all three schemas/tables individually. You can still return all results in one go, though.
For example:
SELECT * FROM schema1.users
UNION ALL
SELECT * FROM schema2.users
UNION ALL
SELECT * FROM schema3.users
;

Create SELECT procedure/trigger to decrypt data on ORACLE

I have this query
SELECT id, UTL_I18N.RAW_TO_NCHAR(DBMS_CRYPTO.DECRYPT(password,
pkg_so_42979606.cipher_type(),
UTL_ENCODE.BASE64_DECODE(UTL_I18N.STRING_TO_RAW('pMV3D4xhyfNxp3YyfLWzAErGcKkIjK3X6uc/WIeVTls=', 'AL32UTF8'))),
'AL32UTF8') password
FROM customeren;
I want to select this table. But i don't want to write this code again and again. What i need to do? How select data from this table in easier way? Oracle 11g XE
You could create a view with the selection criteria above and then just call your select query on the view. Not sure if Oracle syntax is the same but in SQL Server it would be
CREATE VIEW myschema.SomeViewName AS
SELECT id, UTL_I18N.RAW_TO_NCHAR(DBMS_CRYPTO.DECRYPT(password,
pkg_so_42979606.cipher_type(),
UTL_ENCODE.BASE64_DECODE(UTL_I18N.STRING_TO_RAW('pMV3D4xhyfNxp3YyfLWzAErGcKkIjK3X6uc/WIeVTls=', 'AL32UTF8'))),
'AL32UTF8') password
FROM customeren;
Then you can call SELECT * FROM myschema.SomeViewName, although using wildcards is generally not a good idea because you return all columns, when you may not require them. If you want to pass in parameters dynamically then you could consider using a function instead.

T-SQL: Exclude Columns from a SELECT statement based on string

My overall goal is to create new tables by selecting columns in existing tables with certain patterns/tags in their column names. This is in SQL Server.
For example, I have a common need to get all contact information out of a company table, and into its own contact table.
I haven't been able to find a programmatic approach in SQL to express excluding columns from a SELECT statement based on string.
When looking to options like the COL_NAME function, those require an ID arg, which kills that option for me.
Wishing there was something built in that could work like the following:
SELECT *
FROM TABLE
WHERE COL_NAME() LIKE 'FLAG%'
Any ideas? Open to anything! Thanks!!
One trick could be to firstly using the following code to get the column names you desire:
select * from information_schema.columns
where table_name='tbl' and column_name like 'FLAG%'
Then concatenate them into a comma-delimited string and finally create a dynamic sql query using the created string as the column names.

Dynamically update queries as new database comes into existence

Platform: SQL Server 2008
Language: TSQL
I have a number of queries that currently take the general form of (for simplicity sake)
-- Sample begin results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL --many other databases follow with same syntax
How can I modify these queries such that, when a new database comes into existence (named, say DB39C), I ensure that my queries already includes those new records?
--Sample end results
SELECT * from DB01.dbo.table UNION ALL
SELECT * from DB02.dbo.table UNION ALL
SELECT * from DB39C.dbo.table -- this was created as soon as a new database came into existence
I am looking to make sure programmatically, that this happens without my awareness as new databases are added quite regularly and I need the queries I rely on to keep pace.
You might want to have a look at using something like
SELECT name AS DATABASENAME
FROM master.dbo.sysdatabases
and creating dynamic queries
sys.databases (Transact-SQL)

SQL/JDBC : select query on variable tablenames

I'm using Oracle DB and I would like to write a SQL query that I could then call with JDBC. I'm not very familiar with SQL so if someone can help me, that could be great ! Here is the problem. I have a table MY_TABLE wich contains a list of another tables, and I would like to keep only the nonempty tables and those that their names start by a particular string.
The query I wrote is the following :
select TABLE_NAME
from MY_TABLE
where TABLE_NAME like '%myString%'
and (select count(*) from TABLE_NAME where rownum=1)<>0
order by TABLE_NAME;`
The problem comes from the second SELECT, but I don't know how can I do to use the TABLE_NAME value.
Does someone have an idea ?
Thanks.
[Added from comments]
Actually, I need to test the V$ views contained in the ALL_CATALOG table. But if I can find another table where all these views are contained too and with a NUM_ROWS column too, it would be perfect !
Standard versions of SQL do not allow you to replace 'structural elements' of the query, such as table name or column name, with variable values or place-holders.
There are a few ways to approach this.
Generate a separate SQL statement for each table name listed in MY_TABLE, and execute each in turn. Brute force, but effective.
Interrogate the system catalog directly.
Investigate whether there are JDBC metadata operations that allow you to find out about the number of rows in a table without being tied to the system catalog of the specific DBMS you are using.
Can you use oracle view USER_TABLES? then query will be much easier
select TABLE_NAME
from USER_TABLES
where TABLE_NAME like '%myString%'
and Num_ROWS > 0
order by TABLE_NAME;`