Wednesday, April 23, 2025

SQL Server - Get all Tables and Columns from ALL Databases

 This query below gives you All Tables with Columns in All the database in SQL Server:

SET NOCOUNT ON
DECLARE @AllTables table (DbName sysname,SchemaName sysname, TableName sysname,
ColumnID sysname, ColumnName sysname, ColumnType sysname, IsNullable sysname)
DECLARE
@SearchDb nvarchar(200)
,@SearchSchema nvarchar(200)
,@SearchTable nvarchar(200)
,@SQL nvarchar(4000)
SET @SearchDb='%'
SET @SearchSchema='%'
SET @SearchTable='%'
SET @SQL='select ''?'' as DbName, s.name as SchemaName, t.name as TableName,
c.column_id, c.name ColumnName, tp.name ColumnType, c.is_nullable
from [?].sys.tables t
inner join [?].sys.schemas s
on t.schema_id=s.schema_id
inner join [?].sys.columns c
on t.object_id = c.object_id
inner join [?].sys.types tp
on c.system_type_id = tp.system_type_id
where ''?'' like '''+@SearchDb+'''
and s.name like '''+@SearchSchema+'''
and t.name like '''+@SearchTable+''''

INSERT INTO @AllTables (DbName, SchemaName, TableName, ColumnID, ColumnName, ColumnType, IsNullable)
EXEC sp_msforeachdb @SQL
SET NOCOUNT OFF
--SELECT DbName, SchemaName, count(1) FROM @AllTables
select DbName, SchemaName, TableName, ColumnName, ColumnType,
case when IsNullable = 0 then 'Not Null' else '' end "IsNullable"
from @AllTables
where DbName in ('list of dbs interested in or comment out this filter')
--group by DbName, SchemaName
ORDER BY DbName, SchemaName, TableName, ColumnID;

Wednesday, June 15, 2022

Microsoft Teams Takes forever to upload a file

Got annoyed with Teams taking forever to upload a simple file. 

Found that clearing Teams Cache would help and here are the steps to do so in Mac

1] Quit Teams

2] Go to Finder and get to ~/Library/Application Support/Microsoft

3] Delete the folder Teams from there

4] Go to Keychain Access App and search for Microsoft Teams and delete the entry.

5] Restart Teams 

Credit goes to: https://www.uvm.edu/it/kb/article/clearing-teams-cache/

Wednesday, March 23, 2022

Snowflake Constraint Column Details

 At the time of this post, Snowflake did not have any dictionary view that gives the column names for constraints that are defined for a table. 

So, here is what I found to get that list:

show primary keys; --and then get the query id from this and plug it in below query

select "database_name" db_name, "schema_name" schema_name, "table_name" table_name, "column_name" column_name, "key_sequence" key_seq
  from TABLE(RESULT_SCAN('query id from show command above'))
 where not "table_name" like any ('SAN_%', '%_STAGE', '%_STAGE_CLONE', 'BACKUP_%', 'raw_%', 't_raw_%', '_202%')
 order by 1,2,3,5;

Repeat same for unique keys and imported keys(foreign)