Find all views containing specific table names - sql

I have several views in my database that are built on tables.
Is it possible to query for view names which contain specific tables in it?
For example by using:
Where tablename like '%TableA%'
I tried
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME LIKE '%TableA%'
But this gives list of tables created using this table.

Use the following query to search views for a table:
SELECT Objects.name, Modules.definition
FROM sys.sql_modules AS Modules INNER JOIN sys.objects AS Objects ON
Modules.object_id = Objects.object_id
WHERE definition like '%TableA%'

Related

Command to distinguish a table from a view

Is there a command to determine whether a given relation is a table or a view?
The trial-and-error method I'm using is to do:
SHOW CREATE VIEW table_name
If it's a table, not a view, my database software (Presto in DBeaver) will give me an error:
Relation table_name is a table, not a view
I presume this error is coming from a similar command being run internally to vet my query. If it's a view, the command of course works.
The following will show a table of information about all tables available, including which are views and which are tables:
SELECT * FROM information_schema.tables
See Information Schema for more details.
In SQL Server, you can use column 'Type' in Sys.Objects to differentiate a Table or View. If value in column Type is 'V' then its a View, otherwise if value in column Type is 'U' then its a table
To check if it is a table use below query
SELECT DISTINCT NAME, O.TYPE FROM SYS.OBJECTS O WHERE O.NAME LIKE 'Prefix%' AND O.TYPE='U'
To check if it is a view use below query
SELECT DISTINCT NAME, O.TYPE FROM SYS.OBJECTS O WHERE O.NAME LIKE 'Prefix%' AND O.TYPE='V'

Translating query from Firebird to PostgreSQL

I have a Firebird query which I should rewrite into PostgreSQL code.
SELECT TRIM(RL.RDB$RELATION_NAME), TRIM(FR.RDB$FIELD_NAME), FS.RDB$FIELD_TYPE
FROM RDB$RELATIONS RL
LEFT OUTER JOIN RDB$RELATION_FIELDS FR ON FR.RDB$RELATION_NAME = RL.RDB$RELATION_NAME
LEFT OUTER JOIN RDB$FIELDS FS ON FS.RDB$FIELD_NAME = FR.RDB$FIELD_SOURCE
WHERE (RL.RDB$VIEW_BLR IS NULL)
ORDER BY RL.RDB$RELATION_NAME, FR.RDB$FIELD_NAME
I understand SQL, but have no idea, how to work with this system tables like RDB$RELATIONS etc. It would be really great if someone helped me with this, but even some links with this tables explanation will be OK.
This piece of query is in C++ code, and when I'm trying to do this :
pqxx::connection conn(serverAddress.str());
pqxx::work trans(conn);
pqxx::result res(trans.exec(/*there is this SQL query*/));//and there is a mistake
it writes that:
RDB$RELATIONS doesn't exist.
Postgres has another way of storing information about system content. This is called System Catalogs.
In Firebird your query basically returns a row for every column of a table in every schema with an additional Integer column that maps to a field datatype.
In Postgres using system tables in pg_catalog schema something similar can be achieved using this query:
SELECT
TRIM(c.relname) AS table_name, TRIM(a.attname) AS column_name, a.atttypid AS field_type
FROM pg_class c
LEFT JOIN pg_attribute a ON
c.oid = a.attrelid
AND a.attnum > 0 -- only ordinary columns, without system ones
WHERE c.relkind = 'r' -- only tables
ORDER BY 1,2
Above query does return system catalogs as well. If you'd like to exclude them you need to add another JOIN to pg_namespace and a where clause with pg_namespace.nspname <> 'pg_catalog', because this is the schema where system catalogs are stored.
If you'd also like to see datatype names instead of their representative numbers add a JOIN to pg_type.
Information schema consists of collection of views. In most cases you don't need the entire SQL query that stands behind the view, so using system tables will give you better performance. You can inspect views definition though, just to get you started on the tables and conditions used to form an output.
I think you are looking for the information_schema.
The tables are listed here: https://www.postgresql.org/docs/current/static/information-schema.html
So for example you can use:
select * from information_schema.tables;
select * from information_schema.columns;

Programmatically get all tables of a database owned by a user

I created the following query:
select
is_tables.table_name
from information_schema.tables is_tables
join pg_tables
on is_tables.table_name=pg_tables.tablename
where
is_tables.table_catalog='<mydatabase>'
and is_tables.table_schema<>'information_schema'
and is_tables.table_schema<>'pg_catalog'
and pg_tables.tableowner='<myuser>';
I assume there is no database vendor independent way of querying this. Is this the easiest/shortest SQL query to achieve what I want in PostgreSQL?
I think you're pretty close. Object owners don't seem to appear in the information_schema views, although I might have overlooked it.
select is_tables.table_schema,
is_tables.table_name
from information_schema.tables is_tables
inner join pg_tables
on is_tables.table_name = pg_tables.tablename
and is_tables.table_schema = pg_tables.schemaname
where is_tables.table_catalog = '<mydatabase>'
and is_tables.table_schema <> 'information_schema'
and is_tables.table_schema <> 'pg_catalog'
and pg_tables.tableowner = '<myuser>';
You need to join on both the table name and the schema name. Table names are unique within a schema; they're not unique within a database.

Searching for a table based on values contained in the table

I have a table with a reason_id foreign key. I need to map that back to its primary table. I have searched our databases for matching/similar column names but I wasn't able to find the table.
I have a list of values that would be associated with the reason_id. I'd like to search for tables that contain the list I have. Any help would be appreciated.
Here's the query I was running to search for columns:
select
t.name as Table_Name,
SCHEMA_NAME(schema_id) as schema_name,
c.name as Column_Name
from
sys.tables as t
inner join
sys.columns c
on
t.OBJECT_ID = c.OBJECT_ID
where
c.name like '%reason%'
There is no easy way to find the related data in other tables.
I'd try with tools such as ApexSQL Search or SQL Search. Both are free and you won’t go wrong with any of these.
If you want to do it with SQL only then identify all columns in all tables that have the same data type. To do so use sys.columns, sys.types and sys.tables views. Once you find all columns just try start writing queries for each table until you find the right one.
I’d go with something like this
select COUNT(*)
from tableX
where tableX.matchedColumn in
(
-- take 100 or more random rows from the original table
-- if result gives you a number that is near to the number of values listed here then you are most probably on the right track
)

Transact SQL - View Data Type Schronization Question

I working on an auto synchronization project where I want to get the data types from a View on SQL Server 2008 R2 and compare it to a table in the same database. I am familiar with syscolumns, However, this only appears to work with tables, not Views. Any suggestions or references would be greatly appreciated.
Essentially, if View_A has a new column added to it, I need to add the column to Table_A with the same properties as View_A so that all data types, lengths, and fields on Table_A are always the same as View_A.
Thanks
I'm not sure about syscolumns only showing columns of base tables, but you can use this query:
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourView'
sys.columns includes column information for views:
CREATE VIEW dbo.Test_View
AS
SELECT * FROM sys.objects
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('dbo.Test_View')
The columns in sys.columns are for both views and tables.
So, you should be able to do the same as a table, i.e.
SELECT *
FROM sys.columns
WHERE object_id = OBJECT_ID('MyViewName')