如何使用Print语句打印Varchar(Max)?
我有一个代码:
DECLARE @Script VARCHAR(MAX)
SELECT @Script = definition FROM manged.sys.all_sql_modules sq
where sq.object_id = (SELECT object_id from managed.sys.objects
Where type = 'P' and Name = 'usp_gen_data')
Declare @Pos int
SELECT @pos=CHARINDEX(CHAR(13)+CHAR(10),@script,7500)
PRINT SUBSTRING(@Script,1,@Pos)
PRINT SUBSTRING(@script,@pos,8000)
脚本的长度约为10,000个字符,并且由于我使用的是只能容纳最大8000的打印语句。因此,我使用两个打印语句。
问题是当我有一个脚本为18000个字符的脚本时,我曾经使用3个打印语句。
因此,有一种方法可以根据脚本的长度设置打印语句的数量吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
如果有人感兴趣的话,我最终是用PowerShell生成文本文件,执行标量代码:
此脚本是为了获得一个大字符串,其中所有触发器都来自DB。
If someone interested I've ended up as generating a text file with powershell, executing scalar code:
This script it's for getting a big string with all the triggers from the DB.
我阅读了上面的内容,并尝试了自己的简化版本,该版本以正确的垂直间距输出过程文本。
可变声明
在我的实现中,我希望输出以前查询中列出并保存在临时表中的过程列表。然后,我将它们设置为要输出的过程。我将变量@DEF设置为过程定义
,然后我使用一个简单的“雕刻循环”来缩小输出的大小,以与SSMS消息输出窗口兼容,
一旦它低于4K字符限制,我简单地输出了什么。我们绕过了通过打印命令提供的中间CR/LF。这没有什么革命性的,但是有直接且易于阅读的好处。
I read through the above and tried my own simplified version that outputs the procedure text with the correct vertical spacing.
Variable declares
in my implementation I am looking to output a list of procedures listed in a prior query and held in a temp table. I then loop through them setting the variable @tp to the procedure I want to output. I set the variable @def to the procedure definition
then I use a simple "carving loop" to size down the output to be compatible with the SSMS message output window and print statement
once it is below the 4k character limit I simply output what is remaining. We bypass the intervening CR/LF as that is supplied via the Print command. It is nothing revolutionary but has the benefit of being straight-forward and quite easy to read.
我知道这是一个古老的问题,但是我在这里没有提及。
对我来说,以下工作(最多可容纳16K字符),
如果您有超过16k的炭,可以与 @yovav这样的答案结合使用(64k应该足以对任何人来说都足够;)
I know it's an old question, but what I did is not mentioned here.
For me the following worked (for up to 16k chars)
If you have more than 16k chars you can combine with @Yovav's answer like this (64k should be enough for anyone ;)
以下解决方法不使用
print
语句。与SQL Server Management Studio结合使用,它可以很好地工作。您可以单击返回的XML以在内置的XML查看器中展开它。
显示的大小有一个相当大的客户端限制。转到
工具/选项/查询结果/SQL Server/结果到Grid/XML数据
在需要时进行调整。The following workaround does not use the
PRINT
statement. It works well in combination with the SQL Server Management Studio.You can click on the returned XML to expand it in the built-in XML viewer.
There is a pretty generous client side limit on the displayed size. Go to
Tools/Options/Query Results/SQL Server/Results to Grid/XML data
to adjust it if needed.这是应该做到的:
从 http://ask.sqlservercentral.com/questions/3102/any-way-around-the-print-the-print-limit-o-nvarcharmax-in-s.html
Here is how this should be done:
Taken from http://ask.sqlservercentral.com/questions/3102/any-way-around-the-print-limit-of-nvarcharmax-in-s.html
您可以根据脚本长度的计数除以8000
。
You could do a
WHILE
loop based on the count on your script length divided by 8000.EG:
遇到了这个问题,想要一些简单的东西...尝试以下内容:
Came across this question and wanted something more simple... Try the following:
我刚刚从答案:
I just created a SP out of Ben's great answer:
此PROC正确打印出
varchar(max)
参数考虑包装:This proc correctly prints out
VARCHAR(MAX)
parameter considering wrapping:我想使用打印语句来调试一些动态的SQL,因为我想象大多数人都出于Simliar原因而使用Print。
我尝试了一些列出的解决方案,发现Kelsey的解决方案可与次要镊子(@sql是我的@Script)一起使用NB长度不是一个有效的功能:
此代码确实是在注释中添加新的行,但用于调试这对我来说不是问题。
Ben B的解决方案是完美的,是最优雅的,尽管用于调试是很多代码行,因此我选择使用对Kelsey的稍作修改。对于Ben B的代码中,可以在MSDB中创建一个可以重复使用并列为一行的系统?
不幸的是,Alfoks的代码无法正常工作,因为这会更容易。
I was looking to use the print statement to debug some dynamic sql as I imagin most of you are using print for simliar reasons.
I tried a few of the solutions listed and found that Kelsey's solution works with minor tweeks (@sql is my @script) n.b. LENGTH isn't a valid function:
This code does as commented add a new line into the output, but for debugging this isn't a problem for me.
Ben B's solution is perfect and is the most elegent, although for debugging is a lot of lines of code so I choose to use my slight modification of Kelsey's. It might be worth creating a system like stored procedure in msdb for Ben B's code which could be reused and called in one line?
Alfoks' code doesn't work unfortunately because that would have been easier.
或简单:
Or simply:
你可以使用这个
You can use this
有很棒的功能称为 bennett dill /strong>。
这是稍微修改的版本,它使用临时存储过程来避免“模式polution”( https://github.com/toolien/sp_genmerge/blob/master/sp_genmerge.sql )
dbfiddle demo
编辑:
使用
创建或alter
我们可以避免两个exec呼叫:There is great function called PrintMax written by Bennett Dill.
Here is slightly modified version that uses temp stored procedure to avoid "schema polution"(idea from https://github.com/Toolien/sp_GenMerge/blob/master/sp_GenMerge.sql)
DBFiddle Demo
EDIT:
Using
CREATE OR ALTER
we could avoid two EXEC calls:db<>fiddle Demo
使用线条提要和空间作为一个很好的断点:
Uses Line Feeds and spaces as a good break point:
我的printmax版本可防止输出不良行断裂:
My PrintMax version for prevent bad line breaks on output:
这是另一个版本。这是从主字符串中提取每个子字符串,而不是在每个循环上将主字符串减少4000(这可能会在引擎盖下创建许多很长的字符串 - 不确定)。
Here's another version. This one extracts each substring to print from the main string instead of taking reducing the main string by 4000 on each loop (which might create a lot of very long strings under the hood - not sure).
这应该正常工作,这只是以前答案的改进。
This should work properly this is just an improvement of previous answers.
如果源代码将没有CRLF替换的LF问题,则通过以下简单的代码输出不需要调试。
If the source code will not have issues with LF to be replaced by CRLF, No debugging is required by following simple codes outputs.