首页 > 代码库 > SQLServer数据库表字段超长,找到超长字段脚本

SQLServer数据库表字段超长,找到超长字段脚本

平时开发系统时偶尔会遇到数据超长导致往数据库中保存时出错。

使用下边的脚本可以方便的找出超长的字段。

1.通过正式表创建临时表,修改临时表中varchar、nvarchar的长度为max

declare @temp_table_name varchar(50);
declare @table_name varchar(50);
declare @sql varchar(max);
set @table_name=TableName;--正式表表名:此处需要修改
set @temp_table_name = @table_name+_temp;--临时表表名:此处需要修改

--根据正式表创建临时表
set @sql = select * into +@temp_table_name+ from +@table_name + where 1<>1;;
exec(@sql);

--修改varchar/nvarchar临时表字段长度为max
set @sql = ‘‘;
select @sql=@sql+(alter table +@temp_table_name+ alter column +b.name+ +c.name+(max);)  
from sysobjects a,syscolumns b,systypes c 
where a.id=b.id and a.name=@temp_table_name 
and a.xtype=Uand b.xusertype=c.xusertype
and c.name in (varchar,nvarchar)
order by b.colid;

exec(@sql);

--手动往临时表中写入数据

2.数据手动写入临时表后,查找超长字段

declare @temp_table_name varchar(50);
declare @table_name varchar(50);
declare @sql varchar(max);
set @table_name=TableName;--正式表表名:此处需要修改
set @temp_table_name = @table_name+‘_temp‘;--临时表表名:此处需要修改
--校验临时表是哪个字段超长
create table #col_tab
(
    id int,
    col_name varchar(100),
    col_condition varchar(500)
);

insert into #col_tab(id,col_name,col_condition)
select ROW_NUMBER() over(order by b.colid) id,
b.name,
(case c.name when nvarchar then len when varchar then datalength end)+
(+b.name+)>+cast((case c.name when nvarchar then b.length/2 when varchar then b.length end) as varchar) 
from sysobjects a,syscolumns b,systypes c 
where a.id=b.id and a.name=@table_name 
and a.xtype=Uand b.xusertype=c.xusertype
and c.name in (varchar,nvarchar)
order by b.colid;

select * from #col_tab ;

declare @cnt int ;
select @cnt = COUNT(*) from #col_tab;

declare @index int;
declare @col_condition varchar(500);
declare @col_name varchar(100);

set @index=1;
while @index<=@cnt
begin
    select @col_condition = col_condition,@col_name=col_name from #col_tab where id = @index;
    set @sql = declare @condition_cnt int;;
    set @sql = @sql+select @condition_cnt=COUNT(*) from +@temp_table_name+ where +@col_condition+;;
    --set @sql = @sql+‘print @condition_cnt;‘;
    set @sql = @sql+if(@condition_cnt>0)
    begin
        print ‘‘[+@col_name+]字段超长!‘‘;
    end;;
exec(@sql);
    set @index=@index+1;
end;

drop table #col_tab;

 3.新建测试表

CREATE TABLE [dbo].[USERS](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [varchar](30) NULL,
    [password] [varchar](30) NULL,
    [roleid] [int] NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

表截图如下:

技术分享

修改表名,运行 1.通过正式表创建临时表,修改临时表中varchar、nvarchar的长度为max 脚本

declare @temp_table_name varchar(50);
declare @table_name varchar(50);
declare @sql varchar(max);
set @table_name=USERS;--正式表表名:此处需要修改
set @temp_table_name = @table_name+_temp;--临时表表名 

--根据正式表创建临时表
set @sql = select * into +@temp_table_name+ from +@table_name + where 1<>1;;
exec(@sql);

--修改varchar/nvarchar临时表字段长度为max
set @sql = ‘‘;
select @sql=@sql+(alter table +@temp_table_name+ alter column +b.name+ +c.name+(max);)  
from sysobjects a,syscolumns b,systypes c 
where a.id=b.id and a.name=@temp_table_name 
and a.xtype=Uand b.xusertype=c.xusertype
and c.name in (varchar,nvarchar)
order by b.colid;

exec(@sql);

--手动往临时表中写入数据

生成临时表如下:

技术分享

可以看出varchar的长度修改为了max.

4.修改表名后运行脚本2

declare @temp_table_name varchar(50);
declare @table_name varchar(50);
declare @sql varchar(max);
set @table_name=USERS;--正式表表名:此处需要修改
set @temp_table_name = @table_name+_temp;--临时表表名 
--校验临时表是哪个字段超长
create table #col_tab
(
    id int,
    col_name varchar(100),
    col_condition varchar(500)
);

insert into #col_tab(id,col_name,col_condition)
select ROW_NUMBER() over(order by b.colid) id,
b.name,
(case c.name when nvarchar then len when varchar then datalength end)+
(+b.name+)>+cast((case c.name when nvarchar then b.length/2 when varchar then b.length end) as varchar) 
from sysobjects a,syscolumns b,systypes c 
where a.id=b.id and a.name=@table_name 
and a.xtype=Uand b.xusertype=c.xusertype
and c.name in (varchar,nvarchar)
order by b.colid;

select * from #col_tab ;

declare @cnt int ;
select @cnt = COUNT(*) from #col_tab;

declare @index int;
declare @col_condition varchar(500);
declare @col_name varchar(100);

set @index=1;
while @index<=@cnt
begin
    select @col_condition = col_condition,@col_name=col_name from #col_tab where id = @index;
    set @sql = declare @condition_cnt int;;
    set @sql = @sql+select @condition_cnt=COUNT(*) from +@temp_table_name+ where +@col_condition+;;
    --set @sql = @sql+‘print @condition_cnt;‘;
    set @sql = @sql+if(@condition_cnt>0)
    begin
        print ‘‘[+@col_name+]字段超长!‘‘;
    end;;
exec(@sql);
    set @index=@index+1;
end;

drop table #col_tab;

生成where条件是关键,运行后如下图:

技术分享

之后循环where条件查找临时表中数据超长字段,使用print打印出超长字段的名字。

此脚本在字段较多的情况下,排查问题非常方便。

 

SQLServer数据库表字段超长,找到超长字段脚本