[英]How to Change All Sql Columns of One DataType into Another
I have a database ( Sql Server 2005
) where there are dozens of tables
, each of which has a number of columns
(on average 10-20) with datatype set to nvarchar(max)
.我有一个数据库 (
Sql Server 2005
),其中有几十个tables
,每个表都有许多columns
(平均 10-20),数据类型设置为nvarchar(max)
。 This is absolutely killing performance (some of these columns are being used for joins
and some of the tables have 100K+ rows).这绝对会降低性能(其中一些列用于
joins
,一些表有 100K+ 行)。 I would like to change all of these columns to be varchar(250)
.我想将所有这些列更改为
varchar(250)
。 What would be the best way to automate this?自动化这个的最好方法是什么? (I could use
Management Studio
, or I could create a utility to perform this through an ASP.net
website that has access to the db, whichever is easier). (我可以使用
Management Studio
,或者我可以创建一个实用程序来通过可以访问数据库的ASP.net
网站执行此操作,以更容易的为准)。
Here's a working script that uses INFORMATION_SCHEMA.COLUMNS
to find all of the *varchar(max)
columns and converts them to varchar(255)
:这是一个工作脚本,它使用
INFORMATION_SCHEMA.COLUMNS
查找所有*varchar(max)
列并将它们转换为varchar(255)
:
declare @schema nvarchar(255)
declare @table nvarchar(255)
declare @col nvarchar(255)
declare @dtype nvarchar(255)
declare @sql nvarchar(max)
declare maxcols cursor for
select
c.TABLE_SCHEMA,
c.TABLE_NAME,
c.COLUMN_NAME,
c.DATA_TYPE
from
INFORMATION_SCHEMA.COLUMNS c
inner join INFORMATION_SCHEMA.TABLES t on
c.TABLE_CATALOG = t.TABLE_CATALOG
and c.TABLE_SCHEMA = t.TABLE_SCHEMA
and c.TABLE_NAME = t.TABLE_NAME
and t.TABLE_TYPE = 'BASE TABLE'
where
c.DATA_TYPE like '%varchar'
and c.CHARACTER_MAXIMUM_LENGTH = -1
open maxcols
fetch next from maxcols into @schema, @table, @col, @dtype
while @@FETCH_STATUS = 0
begin
set @sql = 'alter table [' + @schema + '].[' + @table +
'] alter column [' + @col + '] ' + @dtype + '(255)'
exec sp_executesql @sql
fetch next from maxcols into @schema, @table, @col, @dtype
end
close maxcols
deallocate maxcols
This is about the only use of cursors that I ever condone, but it's a good one.这是我唯一容忍的游标用法,但它是一个很好的用法。 Essentially, it finds all of the
*varchar(max)
, builds the alter
statement, and then executes it using sp_executesql
.本质上,它找到所有
*varchar(max)
,构建alter
语句,然后使用sp_executesql
执行它。
Enjoy!享受!
You can easily find them, using:您可以使用以下方法轻松找到它们:
select 'alter table ' + quotename(o.name) + ' alter column ' + quotename(c.name) + ' varchar(250); '
from sys.columns c
join
sys.objects o
on o.object_id = c.object_id
where o.type = 'U'
and c.user_type_id = 231
and c.max_length = -1
So now just grab the results of your query and run it.所以现在只需获取查询结果并运行它。
Rob抢
I think the best way to do this task is to generate script for the current database and then replace nvarchar(max) with varchar(250) on the text file, and then create the new database.我认为完成此任务的最佳方法是为当前数据库生成脚本,然后将文本文件中的 nvarchar(max) 替换为 varchar(250),然后创建新数据库。 after that use the Import/export utilities to transfer the data to the new database.
之后使用导入/导出实用程序将数据传输到新数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.