Snowflake - Storing Column Business name - snowflake-cloud-data-platform

How can I export and store column business name on snowflake ? Eg: "LOAN REPAYMENT METHOD CODE" would be the business friendly name for column "LOAN_RPYMT_MTHD_CD" . I can very well start my column comments with the business name Eg: "LOAN REPAYMENT METHOD CODE - The method by which loan will be repaid . Lumpsum, installments etc.. " so that it shows up in the COMMENT property.
However I am wondering is there a "dedicated" property in a snowflake table where I can export this to from my data modeling tool (powerdesigner) so that it shows up in INFORMATION_SCHEMA.columns
Thanks
Sunil

You can use the COMMENT property to store this data, and then retrieve it from the INFORMATION_SCHEMA.COLUMNS view or use
Example of adding comments to a column:
alter table t1 alter LOAN_RPYMT_MTHD_CD comment 'LOAN REPAYMENT METHOD CODE';

You can add comments to your column names as description, this will show up in information_schema.columns.
create table temp.some_loan_table
(
LOAN_RPYMT_MTHD_CD varchar(200)
);
ALTER TABLE TEMP.some_loan_table modify
LOAN_RPYMT_MTHD_CD COMMENT 'LOAN REPAYMENT METHOD CODE - The method by which loan will be repaid . Lumpsum, installments etc.. '
SELECT COMMENT FROM DBNAME.INFORMATION_SCHEMA."COLUMNS"
WHERE TABLE_CATALOG = 'DBNAME' AND TABLE_SCHEMA = 'TEMP' AND TABLE_NAME = 'SOME_LOAN_TABLE'

Related

Snowflake show tables not accessed in last 20 days

There is a situation where I need to clean up my database in snowflake.
we have around 40 database and each database has more than 100 table. Some are getting loaded everyday and some are not, but used everyday.
However, There has been lots of table added for testing and other purpose (by lots of developer and user).
Now we are working on cleaning up un-used table.
We have query_history table which gives us the information of query run in past, however it has field such as database, warehouse, User etc. but not table.
I was wondering is there is any way we can write a query which give us table name not used (DDL and DML b0th) in last 10 days.
select obj.value:objectName::string objName
, max(query_start_time) as QUERY_DATE_TIME
from snowflake.account_usage.access_history
, table(flatten(direct_objects_accessed)) obj
group by 1
order by QUERY_DATE_TIME desc;
The information schema has a tables view and in that you have a last altered column, will that work with you? It will not give you the last accessed table but will give the last altered table. Other than this, there are no easy way to get this information from snowflake at this time. I also needed this feature, I think we should request for this feature.
select table_schema,
table_name,
last_altered
from information_schema.tables
where table_type = 'BASE TABLE'
and last_altered < dateadd( 'DAY', -10, current_timestamp() )
order by table_schema,
table_name;

Changed column name to upper case when create a new table through script in Oracle how to resolve this?

I am using Oracle tool (Oracle SQL Developer - Version 19.2.1.247).
When i create new table in Oracle Db then it change all column name in uppercase i.e.(CUSTOMERID), but i want to keep column name i.e.(CustomerId).I am looking for solutions how to resolve this.
I did some try to change formatting of editor as well code setting in Tools -> Preference but not found any proper things.
Thanks in advance.
You should avoid doing that. Every object ( table, column, index, sequence, trigger... ) is stored in uppercase in the Oracle dictionary.
However, if you want to store the name in lowercase, you must use double quotation
SQL> create table test ( c1 number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
C1
SQL> create table test ( "c1" number );
Table created
SQL> select column_name from all_tab_columns where table_name = 'TEST';
COLUMN_NAME
c1
Keep in mind that if you store the value in lowercase, any search or program that uses the dictionary will have to take this in consideration. That is why I believe it is not a good practice.
Oracle has a default functionality where it will convert all unquoted table/column identifiers to upper case, therefore add double quotes around the names should resolve your issue.

How to access the a table ABC_XXX constantly in Teradata where XXX changes periodically?

I have a table in Teradata ABC_XXX where XXX will change monthly basis.
For Ex: ABC_1902, ABC_1812, ABC_1904 etc...
I need to access this table in my application without modifying the code every month.
Is that any way to do in Teradata or any alternate solution.??
Please help
Can you try using DBC.TABLES in subquery like below:
with tbl as (select 'select * from ' || databasename||'.'||tablename as tb from
dbc.tables where tablename like 'ABC_%')
select * from tbl;
If you can get the final query executed in your application, you will be able to query the required table without editing the query.
The above solution is with expectation that the previous month's table gets dropped whenever a new month's table is created.
However, if previous table is not being dropped, then you can try the below approach:
select 'select * from db.ABC_' ||to_char(current_date,'YYMM')
Output will be
select * from db.ABC_1902
execute the output in your application, you will be able to query dynamic table.

Add a new column to a view in SQL

This is my view
CREATE VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity,
FROM flight
And I want to add 2 new columns named 'bookedseats' and 'availableseats' which don't exist in any tables but are columns I need to add.
I've done my research online and some say you can alter views by using:
ALTER VIEW
And some have said that you can't do that and have to edit from the view you've just created.
I've tried this:
CREATE VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity, bookedseats varchar(10), availableseats varchar(10)
FROM flight
which gave this error:
ERROR: syntax error at or near "varchar" LINE 2: ...ECT
flightid,flightdate, maxcapacity, bookedseats varchar(10...
I've also tried ALTER VIEW:
ALTER VIEW seat_availability AS
SELECT flightid,flightdate, maxcapacity, bookedseats varchar(10), availableseats varchar(10)
FROM flight
And I got this error:
ERROR: syntax error at or near "AS" LINE 1: ALTER VIEW
seat_availability AS
It would be easy to add columns if they existed in other tables but because I need to add 2 columns that don't exist in any table, it's proving difficult to do. If someone could help it would be very appreciated. Thank you.
Perhaps I may need to drop the view? and start again with two new columns added but how do I add them since they don't exist in any table in my database??
You don't define the datatype of a column like that. You let the view use the underlying datatype like this.
ALTER VIEW seat_availability AS
SELECT flightid
, flightdate
, maxcapacity
, bookedseats
, availableseats
FROM flight
Or if you need to explicitly change the datatype you need to use CONVERT like this.
ALTER VIEW seat_availability AS
SELECT flightid
, flightdate
, maxcapacity
, bookedseats = convert(varchar(10), bookedseats)
, availableseats = convert(varchar(10), availableseats)
FROM flight
Try this to add fake columns to the view:
ALTER VIEW [dbo].[view_seat_availability]
AS
SELECT flightid
,flightdate
,maxcapacity
,CAST(NULL AS VARCHAR(10)) AS 'bookedseats'
,CAST(NULL AS VARCHAR(10)) AS 'availableseats'
FROM flight

SQL server : Inserting/updating missing data from one table to another

I have two table "Container" and "Control". These are existing tables and there is no foreign key relationship between the two. These are also very old tables so are not normalized. And I cannot change the structure now.
Below is the structure of the two tables.
Container table :
Control Table :
The Name field in Control table contains CTableName+CPName from Container table.
I want to update the columnName field of Control table with the value of CID column of Container table. and also want to insert one more record (for ctable2 i.e the fourth row in final Control table below) in Control table.
The tablename and columnname columns have will always be have default values.
The final Control table should look like this:
How do I do this?
I hope you want to apply this fix because you want normalize your table structure.
Try this:
First step:
In this way you'll UPDATE all Control rows with the value of Container table where the couple fields CTableName and CPName are the same of Name (excluding the rows of Container with the same couple fields)
UPDATE Control
SET ColumnValue = (
SELECT c.CID
FROM Container c
WHERE c.CTableName + '+' + c.CPName = Control.Name
AND NOT EXISTS(
SELECT 'PREVIOUS'
FROM Container c2
WHERE c.CTableName = c2.CTableName
AND c.CPName = c2.CPName
AND c.CID < c2.CID
)
),
TableName = 'default', ColumnName = 'default'
WHERE ColumnValue IS NULL
Second step:
Adding elements don't present in Control table
INSERT INTO Control (field list)
SELECT field list
FROM Container co
WHERE NOT EXISTS(
SELECT 'in_control'
FROM Control ct
WHERE co.CID = ct.ColumnValue
)
After these two steps you can drop column Name in Control table
I am an Oracle plsql programmer and worked with Sql-server as well.
First you should describe the relationship between the 2 tables, in the end i could figger it out but it's better you explain it yourself.
To update a table with information from another table you should ask yourself:
- when should the update take place?
- what are the conditions to start the update?
- how should the update be done?
In Oracle there is a database object called a trigger. It's quite a handy object and probably just what you need. I believe that sql-server has it too.
Pls fee free to ask any questions but do read the sql-server appropriate manual as well.
Good luck, Edward.

Resources