Uppercase all columns in all table in Snowflake - snowflake-cloud-data-platform

When I originally created tables in snowflake, I used quotes around the column names, which has made things case sensitive for subsequent auto-generated queries. All new table are now created with uppercase column names, so the new queries work fine. Is there a way I can convert all column names to uppercase in all tables in Snowflake?

I managed this by doing the following, to generate a load of ALTER TABLE commands:
select CONCAT('ALTER TABLE ', TABLE_NAME, ' RENAME COLUMN "', COLUMN_NAME, '" to ', UPPER(COLUMN_NAME), ';')
from information_schema.columns
WHERE TABLE_SCHEMA ilike 'SchemaName'
AND COLUMN_NAME != UPPER(COLUMN_NAME)

Related

Query a list of columns and tables from different databases in SQL Server

I have multiple databases and multiple tables in each one, how do I query a list that contains a column name, table name, and database name that matches with a key word in the column name?
Example: look for the database, table and column that has ID in the column name
WHERE column_name LIKE '%ID%'
master.sys.sp_MSforeachdb procedure executes the given command in all databases. Using that you can get columns of all databases. Question mark will be replace in every step with current database name. You can use ? everywhere in your command.
CREATE TABLE #Columns
(
database_name sysname,
table_name sysname,
column_name sysname
);
EXEC master.sys.sp_MSforeachdb '
USE [?]
INSERT #Columns (database_name, table_name, column_name)
SELECT DB_NAME(), OBJECT_NAME(c.object_id), c.name
FROM sys.columns AS c
WHERE c.name LIKE ''%ID%'''
SELECT * FROM #Columns AS c

List all columns wtih datatype in specific table in Snowflake

I am looking for a programmatic way to get the Snowflake table schema, is there a way for that?
You can use a SHOW command to list table columns e.g.
SHOW COLUMNS IN TABLE MYSCHEMA.MYTABLE;
Note that SHOW commands have the advantage that they do not require a running warehouse to execute, so are zero cost queries.
Use this query:
select ordinal_position as position,
column_name,
data_type,
case when character_maximum_length is not null
then character_maximum_length
else numeric_precision end as max_length,
is_nullable,
column_default as default_value
from information_schema.columns
where table_schema ilike 'schema' -- put your schema name here
and table_name ilike 'table' -- put your table name here
order by ordinal_position;
Please note, this would incur a cost as it will require a running warehouse. If you are looking for a no-cost solution check Nathan Griffiths' answer. If you are looking for a query-based predictable solution you can use this answer.

Retrieve column name through db link in Oracle 11g

I want to retrieve column names from a table through a db link but I'm not able to do it...
Although this query is working
SELECT *
FROM myTableName#myDbLink;
the following one is not:
SELECT column_name
FROM all_tab_columns#myDbLink
WHERE table_name = 'myTableName'
What's the correct way of retrieving the column names?
CaSE mATterS.
In Oracle, table names are - by default - in UPPERCASE, so - try with
SELECT column_name
FROM all_tab_columns#myDbLink
WHERE table_name = 'MYTABLENAME'

Deleting specific rows from all tables

I have a database with a lot of tables.
In each table there is a column named "LicenseID" (bigint). How can I delete all rows, from all tables, that contain the value "2" in the column "LicenseID" ?
Best regards!
You didn't mentioned which database are you working with.
I assume it is SQL Server
First check your result with following query
SELECT * FROM table WHERE LicenseID LIKE '%2%'
or
SELECT * FROM table WHERE Contains(LicenseID, 2) > 0
Then Delete all those rows with above condition.
DELETE FROM table WHERE LicenseID LIKE '%2%'
or
DELETE FROM table WHERE Contains(LicenseID, 2) > 0
I have not tested it and don't know what type of database are you using. Answer can be little different in different databases.
You can use INFORMATION_SCHEMA.COLUMNS to generate the delete statements dynamically.
Something like this should do the trick:
DECLARE #Sql varchar(max) = ''
SELECT #Sql = #Sql + 'DELETE FROM '+ TABLE_NAME +' WHERE LicenseID = 2; '
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'LicenseID'
EXEC(#Sql)
The reason I've used INFORMATION_SCHEMA.COLUMNS and not INFORMATION_SCHEMA.TABLES is to prevent an error in case there is a table that doesn't contain the LicenseID column.

How to get exact two columns of table(s) from a entair database in SQL server

There are two tables - Entity table and Account table. Entity table having EntityID and Account table having AccountID. There is a 3rd table EntityAccountAssociation which contains both EntityID and AccountID. - This I know.
My scenario is; suppose in a product support, I do not know about this 3rd table (and no one is there to tell me) , then, is there any query to find out this 3rd table to get the relationship in a huge database with 100s of table?
For Example: In that query, I will pass these two column name as parameter and it will show me on which particular table these two columns exists.
Please help and let me know. Thanks.
If all you're looking for is a query that returns table(s) with both columns, this would do:
-- DECLARE #col1 NVARCHAR(255) = 'X', #col2 NVARCHAR(255) = 'Y'
SELECT TABLE_SCHEMA + '.' + TABLE_NAME TablesContainingBoth
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME IN (#col1, #col2)
GROUP BY TABLE_SCHEMA + '.' + TABLE_NAME
HAVING COUNT(*) > 1
If only one table had both columns, it would return one result.

Resources