I am trying to find the created by user for a table in Snowflake. Information_Schema tables only shows the role that owns it.
Going through the Query_History is laborious.
Thanks,
Sam
Information_Schema and Show tables command.
random searches in snowflake.account_usage.query_history can be laborious, but targeted ones are fairly quick and easy, this should work for you.
SET dbName='DATABASE NAME HERE';
SET schemaName='SCHEMA NAME HERE';
SET tableName='TABLE NAME HERE';
SET create_dt=(
SELECT MIN(created)
FROM snowflake.account_usage.tables
WHERE table_catalog = $dbName
AND table_schema = $schemaName
AND table_name = $tableName
AND deleted is null);
SELECT *
FROM snowflake.account_usage.query_history
WHERE query_text iLike '%CREATE%TABLE%'||$tableName||'%'
AND execution_status = 'SUCCESS'
AND start_time = $create_dt;
Posting the query I came up with to find user/role of the person who created a table for anyone that wants to adapt it.
// anything older than 20 days ago
SET cutoff = current_date() - 20;
SELECT h.query_text,
h.database_name,
h.user_name,
h.role_name,
t.table_name,
u.login_name
FROM snowflake.account_usage.query_history h
LEFT JOIN snowflake.account_usage.users u
ON u.name = h.user_name
AND u.disabled = FALSE
AND u.deleted_on IS NULL
JOIN user_stage.information_schema.tables t
ON t.created = h.start_time
WHERE execution_status = 'SUCCESS'
AND start_time < $cutoff
AND query_type LIKE '%CREATE_TABLE%'
AND CONTAINS(UPPER(h.query_text), UPPER(t.table_name))
AND t.created < $cutoff;
Related
In Postgres I may use something like
select count(p1.*) from pg_partitions p1
where p1.tablename = 'table_name';
How to write query that counts NO of partitions for specific table in oracle?
What is oracle equivalent of pg_partitions ?
Depending on which privileges you have
Partitions of a table you own
select count(*) from user_tab_partitions where table_name = yourtable;
Partitions of a table you own or you have privilege to see it
select count(*) from all_tab_partitions where table_name = yourtable and table_owner = ownerofthetable ;
If you have privilege select any dictionary or select catalog role, or a granted privilege over the specific dba_tab_partitionsview , then
select count(*) from dba_tab_partitions where table_name = yourtable and table_owner = ownerofthetable ;
I am trying to associate an object (table) back to the user that created it. Since objects are owned by the active role, how can I get a list of tables created by a single user? I've searched QUERY_HISTORY for the create table statements, but looking for a better solution.
Does something like this help or is this what you are doing now? I think it's the main solution available right now. You can, of course, change the filters as desired including filtering by a specific user or timeframe.
use role accountadmin;
use schema snowflake.account_usage;
select q.start_time created_time, q.user_name, t.table_name, q.query_text, t.table_schema schemas_name, T.table_catalog database_name from query_history q
inner join tables t
on q.start_time = t.created
and q.schema_name = t.table_schema
and q.database_name = t.table_catalog
and q.query_text ilike 'create%table%'
and q.start_time > '2020-11-04 11:13:54'
order by q.start_time;
You should be able to do it with a query like this
use schema snowflake.account_usage;
select *
from QUERY_HISTORY
where
database_name = 'TPCDS'
and query_type = 'CREATE'
;
I have a problem.
I want to show field_name at my query 'select .. from .. where' clause.
In my logging table, the table only keep the [date],[table_id],[field_id] .. fields, i need to show the 'field_name' as well in my select but there is no any other table that i can 'join' to get the [field_name] based on [field_id] in my 'select .. from'. As I search from google it seems I can get the field_name from SQL system called 'sys.columns' table. but that is too complicated for my SQL level because this sys.coloums is not straight forward. it mixed all the table before all each field number depend on the table id itself.
anyone who know how to write a good query to select out the field_name pls help :)
log_list table
[date],[table_id],[field_id],[company_name],...
my current query :
SELECT
date , table_id , name as table_name
FROM
log_list join table_name_list on log_list.table_id = table_name_list.table_id
and log_list.company_name like '%something%'
WHERE
date between #startdate and #enddate
thank you for your attention.
Your tag didn't say what version of SQL Server you are using, assuming you are using 2008+ there is a system function
COL_NAME ( table_id , column_id )
which if you supply table_id and column_id would get you the name, for detailed reference check it out here: COL_NAME (Transact-SQL)
If that is not a valid option for you, look closer to sys.columns, the object_id it returns is actually the table's object_id and should be your table_id, so if you do something like following it should work:
select C.name AS column_name
...
FROM ...
...
join sys.columns C on yourtable.table_id = C.object_id and yourtable.column_id = C.column_id
How do I list tables in redshift ?
I am using SQLWorkbench.
I tried SHOW TABLES; and \dt are not recognized commands.
Any help?
On Sql workbench,
Go to Tools -> New DBexplorerWindow
it will load all the tables and click on table name shown the table schema as well
Hope this is helpfull.
You can use this query to get the list of tables
SELECT DISTINCT tablename
FROM pg_table_def
WHERE schemaname = 'public'
ORDER BY tablename;
More info here - http://docs.aws.amazon.com/redshift/latest/dg/c_join_PG_examples.html
This should work.
select datname, nspname, relname, sum(rows) as rows
from pg_class, pg_namespace, pg_database, stv_tbl_perm
where pg_namespace.oid = relnamespace
and pg_class.oid = stv_tbl_perm.id
and pg_database.oid = stv_tbl_perm.db_id
and datname = '**db_name**' and nspname = '**schema_name**'
group by datname, nspname, relname
order by datname, nspname, relname;
So This is a school question similar to another I was given a code:
USE AP
SELECT VendorName, FirstInvoiceDate, InvoiceTotal
FROM Invoices JOIN
(SELECT VendorID, MIN(InvoiceDate) AS FirstInvoiceDate
FROM Invoices
GROUP BY VendorID) AS FirstInvoice
ON (Invoices.VendorID = FirstInvoice.VendorID AND
Invoices.InvoiceDate = FirstInvoice.FirstInvoiceDate)
JOIN Vendors
ON Invoices.VendorID = Vendors.VendorID
ORDER BY VendorName, FirstInvoiceDate
I need to change this to create a view instead of a derived table I was thinking something more like
IF NOT EXISTS (SELECT * FROM sys.views
Where name = ‘EarliestInvoiceandTotalVEW’)
CREATE View AS
Select VendorName, FirstInvoiceDate, InvoiceTotal
From Invoices JOIN
(Create VIEW FirstInvoice AS
SELECT VendorID, MIN(InvoiceDate) AS FirstInvoiceDate
From Invoices
Group By VendorID) As FirstInvoice
ON (Invoices.VendorID = FirstInvoice.VendorID AND
Invoices.InvoiceDate = FirstInvoice.FirstInvoiceDate)
JOIN Vendors
ON Invoices.VendorID = Vendors.VendorID
ORDER BY VendorName, FirstInvoiceDate
This would make a view to do the same thing and check if it exists so it is not recreated/defined each time.
Thanks for any input on this!
I apologize about the format I took it directly out of SQL Server and it normally is formatted well...
This is what I got to work:
USE AP
Declare #Create1 varchar(8000)
IF EXISTS (SELECT * FROM sys.views Where sys.views.name = 'EarliestInvoiceandTotalVIEW')
drop view EarliestInvoiceandTotalVIEW;
SET #CREATE1 = 'CREATE View EarliestInvoiceTotalVIEW AS
Select VendorName, FirstInvoiceDate, InvoiceTotal
From Invoices JOIN
(SELECT VendorID, MIN(InvoiceDate) AS FirstInvoiceDate
FROM Invoices
GROUP BY VendorID) AS FirstInvoice
ON (Invoices.VendorID = FirstInvoice.VendorID AND
Invoices.InvoiceDate = FirstInvoice.FirstInvoiceDate)
JOIN Vendors
ON Invoices.VendorID = Vendors.VendorID'
Exec (#CREATE1)
I had to set the where to sys.views.name =... as well as set an exec command so that the create view is running 'first'.
In response to your comment, be sure to surround your view definition with GO statements:
if exists (select * from sys.views where name = 'ThreeDigitsOfPi')
drop view dbo.ThreeDigitsOfPi
GO
create view dbo.ThreeDigitsOfPi
as
select 3.14 as PI
GO
Some other points:
You should use single quotes ' instead of ‘ style quotes
Your view has to have a name (create view NameOfView as ...)
An view definition cannot follow an if statement, unless the view is created in dynamic SQL. For example: if not exists (...) exec('create view ...')
There are multiple ways to check if a view exists. However, often the approach is to delete a view if it exists and then recreate it:
IF EXISTS (SELECT * FROM sys.views Where name = ‘EarliestInvoiceandTotalVEW’)
drop view EarliestInvoiceandTotalVEW;
Then, your create view statement has two flaws. First it is lacking a name. Second it has an embedded create view in it. You have to decide which one you want to create -- a view for the subquery or a view for the entire statement (it is not clear to me from the question which is the right approach).