Created
February 24, 2025 14:26
-
-
Save MarkPryceMaherMSFT/b9f735684b3bc968ab9a95c7682b5e73 to your computer and use it in GitHub Desktop.
Script to create views so a custom collation can be added to varchar columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT | |
SchName, | |
tblName, | |
'CREATE view [' + SchName + '].[vw_' + tblName + '] as SELECT ' + STRING_AGG(colname + ' '+ | |
case coltype | |
when 'varchar' then ' COLLATE Latin1_General_100_CI_AI_SC as ' + colname | |
else '' | |
end | |
, ', ') + ' from [' + SchName + '].[' + tblName + '];' AS DDLScript | |
FROM ( | |
select tbl.object_id, convert(varchar, sc.name) SchName, convert(varchar, tbl.name) tblName , c.column_id colid, convert(varchar,c.name) colname, convert(varchar,t.name) as coltype, convert(varchar,c.collation_name) collation_name | |
from sys.columns c | |
join sys.tables tbl on tbl.object_id=c.object_id | |
join sys.types t on t.user_type_id = c.user_type_id | |
inner join sys.schemas sc on tbl.schema_id=sc.schema_id | |
left join sys.default_constraints dc on c.default_object_id =dc.object_id and c.object_id =dc.parent_object_id) a | |
GROUP BY SchName, tblName; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment