SQL Server字符串连接与Varchar(最大值)

时间:2018-05-08 07:51:28

标签: tsql sql-server-2012

我正在尝试通过将字符串连接到varchar max来生成列名列表,以便在动态查询中使用。我一直在使用的方法一直很好,直到我遇到一个超过500列的表。使用大表时,当我将额外的字符串添加到列以生成sql select脚本时,我的列列表将被截断。 如果有人可以向我指出为什么我的最后4个选择(下面)不是全部产生600行(当字段被复制到新的查询窗口时,该字段包含换行符)我将非常感激。 Varchar(max)应该足够大以产生我想要的结果。我只是无法弄清楚截断发生的原因。

--CREATE TABLE [dbo].[Fact_NULLs_TEST]
--(
--My_really_quite_quite_quite_long_Column_name1 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name2 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name3 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name4 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name5 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name6 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name7 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name8 DECIMAL(18,2) NULL,
--My_really_quite_quite_quite_long_Column_name9 DECIMAL(18,2) NULL,
---- ......... etc up to My_really_quite_quite_quite_long_Column_name600
--)

DECLARE @Str AS VARCHAR(MAX)
DECLARE @Str2 AS VARCHAR(MAX)
DECLARE @Str3 AS VARCHAR(MAX)
DECLARE @Str4 AS VARCHAR(MAX)
SET @Str = ''
SET @Str2 = ''
SET @Str3 = ''
SET @Str4 = ''


SELECT 
    @Str = @Str + CAST(QUOTENAME(name) AS VARCHAR(MAX)) +  CAST(CHAR(13) AS VARCHAR(MAX))
FROM
MY_DB.sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')


SELECT 
    @Str2 = CAST(@Str2 AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) + CAST('my short text' AS VARCHAR(MAX)) + CAST(CHAR(13) AS VARCHAR(MAX))
FROM
MY_DB.sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')


SELECT 
    @Str3 = CAST(@Str3 AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) + CAST(' my long text my long text my long text my long text my long text ' AS VARCHAR(MAX)) + CAST(CHAR(13) AS VARCHAR(MAX))
FROM
MY_DB.sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')



SELECT 
    @Str4 = CAST(@Str4 AS VARCHAR(MAX)) + CAST('SUM(CASE WHEN ' AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) +  CAST(' IS NULL THEN 1 ELSE 0 END) AS ' AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) +  CAST(',' AS VARCHAR(MAX)) +  CAST(CHAR(13) AS VARCHAR(MAX))
FROM
MY_DB.sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')



SELECT @Str , LEN(@Str) --- 600 rows

SELECT @Str2, LEN(@Str2) --- 600 rows

SELECT @Str3 , LEN(@Str3) --- 378 rows ??????????????????

SELECT @Str4 , LEN(@Str4) --- 303 rows ??????????????????

1 个答案:

答案 0 :(得分:2)

这种情况会导致如果输入字符串超过其最大值128个字符,则引号返回null。 更多信息:

https://docs.microsoft.com/en-us/sql/t-sql/functions/quotename-transact-sql?view=sql-server-2017

在你的代码中

也不需要在每个部分上添加一个强制转换,因为你要连接一个声明的varchar(max)变量,Str你可以简单地使用plus,甚至是brakets添加所有内容,如下所示: / p>

@Str = @Str + '['+name+']'+ CHAR(13)

无论如何,从单个可选对象返回的最大长度看起来没有记录的限制,无论是使用SQLCMD还是使用SQL Server Management Studio作为客户端执行SQL而不是将所有内容都放在一个字符串您可以通过简单的直接选择获得正确的文本,或者如果您在程序中使用它,则需要打开游标并从结果集中逐个获取每一行。

我包含了用于测试的确切SQL,并注释了我的发现。

USE MY_DB

DECLARE @Str AS VARCHAR(MAX)
DECLARE @Str2 AS VARCHAR(MAX)
DECLARE @Str3 AS VARCHAR(MAX)
DECLARE @Str4 AS VARCHAR(MAX)
SET @Str = ''
SET @Str2 = ''
SET @Str3 = ''
SET @Str4 = ''


SELECT  
    @Str = @Str + '[' + name + ']'+ CHAR(13) 
FROM
sys.columns 
WHERE [object_id] = OBJECT_ID('[dbo].Fact_NULLs_TEST')

/*
SELECT 
    @Str2 = CAST(@Str2 AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) + CAST('my short text' AS VARCHAR(MAX)) + CAST(CHAR(13) AS VARCHAR(MAX))
FROM
sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')


SELECT 
    @Str3 = CAST(@Str3 AS VARCHAR(MAX)) +  CAST(QUOTENAME(name) AS VARCHAR(MAX)) + CAST(' my long text my long text my long text my long text my long text ' AS VARCHAR(MAX)) + CAST(CHAR(13) AS VARCHAR(MAX))
FROM
sys.columns 
WHERE [object_id] = OBJECT_ID('[MY_DB].[dbo].Fact_NULLs_TEST')


*/
SELECT 
    --top 50 
    @Str4 = @Str4 + 
    ' SUM(CASE WHEN ' +'['+ sc.name +']' + ' IS NULL THEN 1 ELSE 0 END) AS ' +'[' + sc.name +'],' + CHAR(13)
FROM
sys.columns sc
WHERE [object_id] = OBJECT_ID('[dbo].Fact_NULLs_TEST')

SELECT @Str , LEN(@Str) --- 29892 chars
/*
SELECT @Str2, LEN(@Str2) --- 

SELECT @Str3 , LEN(@Str3) ---
*/
SELECT @Str4 , LEN(@Str4) --- 87384 chars BUT in case you use Microsoft SQL Server Management Studio and copy /paste into an editor this is coming out truncated

PRINT @Str4 --This is truncated as well if displayed from Messages tab within Microsoft SQL Server Management Studio or grabbed by using SQLCMD 
--with -o on a text file, i.e. SQLCMD -S dbserver -E -i c:\temp\test1.sql -o c:\temp\test1out.txt

SELECT 
    ' SUM(CASE WHEN ' +'['+ sc.name +']' + ' IS NULL THEN 1 ELSE 0 END) AS ' +'[' + sc.name +'],' 
FROM
sys.columns sc
WHERE [object_id] = OBJECT_ID('[dbo].Fact_NULLs_TEST') 
--This last select works fine either from Microsoft SQL Server Management Studio or by using SQLCMD