自動清理 MSSQL Server Table Collation問題的建站知識
導讀:1建站知識Cannot resolve the collation conflict between Latin1_General_CI_AS and SQL_Latinseo網站關鍵詞優化網seo優化趨勢。
在一個團隊項目中,沒有約定好Collation, 在 MS SQL Server中編程就會遇到這樣的問題:Cannot resolve the collation conflict between "Latin1_General_CI_AS" and "SQL_Latin1_General_CP1_CI_AS" in the equal to operation.
因為編碼問題,不同編碼的字符串并不能直接進行比較,這種有兩個解決方法,1是在 query中 指定 用某一個collation進行比較, 另外一個就是修改column的 collation type 來避免這種錯誤。
第二種方法,一個一個column改起來很累,寫了個script,除了 被當作 constraints比如 primary key, foreign key之外的 varchar, char,nvarc網站優化seo培訓har 都可以統一修改成一個collation…
完整sql代碼:
復制代碼 代碼如下:
declare @CollationName varchar(500);set @CollationName = 'SQL_Latin1_General_CP1_CI_AS'create table #tmp (sqlStr varchar(max));inse營銷型網站建設rt into #tmpselect 'alter table [' + o.name + '] alter column [' + c.name + ']' + (case c.system_type_id when 167 then ' varchar(' when 175 then ' char(' else ' nvarchar(' end) + convert(varchar,c.max_length) + ') collate ' + @CollationNamefrom sys.columns c, sys.objects o where c.object_id=o.object_id and o.type='U' and c.system_type_id in (167,175,231) and collation_name<>@CollationNameand c.name not in ( select cc.COLUMN_NAME from INFORMATION_SCHEMA.TABLE_CONSTRAINTS pk ,INFORMATION_SCHEMA.KEY_COLUMN_USAGE ccwhere cc.TABLE_NAME = pk.TABLE_NAMEand cc.CONSTRAINT_NAME = pk.CONSTRAINT_NAME)while (exists (select * from #tmp))begi企業網站建設n declare @sqlStr varchar(max); select @sqlStr=(select top 1 sqlstr from #tmp); exec(@sqlStr) delete from #tmp where sqlStr=@sqlStrenddrop table #tmp;
相關seo網站關鍵詞優化網seo優化趨勢。聲明: 本文由我的SEOUC技術文章主頁發布于:2023-05-24 ,文章自動清理 MSSQL Server Table Collation問題的建站知識主要講述標簽,標題,SQL網站建設源碼以及服務器配置搭建相關技術文章。轉載請保留鏈接: http://www.bifwcx.com/article/web_6612.html