好吧,例如我有 varchar 200 列,但它有最大 25 varchar 值。所以这个查询应该返回这个列。此查询应遍历选定表中的所有列并返回所有此类结果。所以我们可以检查列结构并采取适当的措施。
我的意思是:“找到所有定义为比其中最大实际数据值更宽的列”
你没有指出什么 RDBMS,所以我尽可能地采用了一种通用的方法。大型 RDBMS 安装通常支持 INFORMATION_SCHEMA 表(这是一个标准,尽管它很有价值)。如果没有,请调整它以适应可用的元数据以及语法怪异。方法本身是合理的。
SET NOCOUNT ON;
-- Create a local table for processing
DECLARE
@RESIZING TABLE
(
resizing_id int identity(1,1) NOT NULL PRIMARY KEY
, schemaname sysname
, tablename sysname
, columnname sysname
, datatype sysname
, max_length int
, current_length int
);
-- Use the ANSI standard view for system meta data
-- to populate tables. Only focusing on varchar fields
-- as they are the only ones that will provide useful
-- sizing information.
-- To make it work with chars, update the query individual
-- queries to perform a trim operation before calling len
-- and obviously update the data_type to include char
INSERT INTO
@RESIZING
SELECT
ISC.TABLE_SCHEMA
, ISC.TABLE_NAME
, ISC.COLUMN_NAME
, ISC.DATA_TYPE
, ISC.CHARACTER_MAXIMUM_LENGTH
, NULL AS current_length
FROM
INFORMATION_SCHEMA.COLUMNS ISC
WHERE
ISC.DATA_TYPE = 'varchar';
-- Create a cursor
-- Kill a kitten
DECLARE
Csr CURSOR
FOR
SELECT
-- build out a query like
-- SELECT @current_len = MAX(LEN(X.[COLUMN_NAME])) FROM [dbo].[TABLE] X WITH(NOLOCK)
'SELECT @current_len = MAX(LEN(X.['
+ R.columnname
+ '])) FROM ['
+ R.schemaname
+ '].['
+ R.tablename
+ '] X WITH(NOLOCK) ' AS query
, R.current_length
FROM
@RESIZING R
FOR UPDATE OF current_length;
-- 2 local variables, one for the dynamic query
-- one to hold the results of said query
DECLARE
@query nvarchar(max)
, @current_length int;
OPEN
Csr;
FETCH NEXT
FROM Csr
INTO @query, @current_length;
WHILE @@FETCH_STATUS = 0
BEGIN
-- try before you buy
PRINT @query;
-- Run the query, assigning length to @current_length variable
EXECUTE sp_executesql @query, N'@current_len int OUTPUT', @current_len = @current_length OUTPUT;
-- Push results int our temporary table
UPDATE
R
SET
current_length = @current_length
FROM
@RESIZING R
WHERE
CURRENT OF Csr;
FETCH NEXT
FROM Csr
INTO @query, @current_length;
END
CLOSE Csr;
DEALLOCATE Csr;
-- Result the resultset for all the
-- tables with longer lengths than used
-- (NULLS included)
SELECT
*
FROM
@RESIZING R
WHERE
R.max_length > isnull(@current_length, 0)
结果(SQL Server 2008 R2)
resizing_id | schemaname | tablename | columnname | datatype | max_length | current_length
1 | dbo | DupesOk | name | varchar | 50 | 12
2 | dbo | SALES_HEADER | CCCode | varchar | 15 | 15
3 | lick | ABC | value | varchar | 50 | 8