我编辑了 MyItchyChin 的脚本,因为我发现了其中的一些缺陷。当@part END”)。我也做了其他小的改动。我的问题现在解决了!非常感谢 MyItchyChin 的初始脚本。
Obs:我在 SQL Server 2008 R2 中使用此脚本。该脚本还可用于编写函数脚本。
SET NOCOUNT ON
DECLARE @procs AS TABLE( nome varchar(200),object_id INT
, definition NVARCHAR(MAX)
, uses_ansi_nulls BIT
, uses_quoted_identifier BIT
)
INSERT INTO @procs
SELECT o.name
,m.object_id
, m.definition
, m.uses_ansi_nulls
, m.uses_quoted_identifier
FROM sys.sql_modules AS m
INNER JOIN sys.objects AS o
ON m.object_id = o.object_id
WHERE 1=1
--and o.type = 'P'
AND o.name IN ('proc1')
DECLARE @endStmt NCHAR(6)
, @object_id INT
, @definition NVARCHAR(MAX)
, @uses_ansi_nulls BIT
, @uses_quoted_identifier BIT
DECLARE @crlf VARCHAR(2), @len BIGINT, @offset BIGINT, @part BIGINT
SELECT @object_id = MIN(object_id)
, @endStmt = CHAR(13) + CHAR(10) + 'GO' + CHAR(13) + CHAR(10)
FROM @procs
declare c cursor for SELECT definition
, uses_ansi_nulls
, uses_quoted_identifier
FROM @procs
order by nome asc
open c
fetch next from c into @definition,@uses_ansi_nulls,@uses_quoted_identifier
while @@fetch_status<>-1
begin
IF @uses_ansi_nulls = 1
PRINT 'SET ANSI_NULLS ON' + @endStmt;
ELSE
PRINT 'SET ANSI_NULLS OFF' + @endStmt;
IF @uses_quoted_identifier = 1
PRINT 'SET QUOTED_IDENTIFIER ON' + @endStmt;
ELSE
PRINT 'SET QUOTED_IDENTIFIER OFF' + @endStmt;
--PRINT @definition;
IF LEN(@definition) <= 4000
PRINT @definition
ELSE
BEGIN
SELECT @crlf = CHAR(13)+CHAR(10)
, @len = LEN(@definition)
, @offset = 1
, @part = CHARINDEX(@crlf,@definition)-1
WHILE @offset <= @len AND @part>=0
BEGIN
--PRINT @offset
--PRINT @part
--PRINT LEN(@crlf)
--PRINT @len
PRINT SUBSTRING(@definition,@offset,@part)
SET @offset = @offset + @part + LEN(@crlf)
SET @part = CHARINDEX(@crlf,@definition,@offset)-@offset
--PRINT @offset
--PRINT @part
--PRINT @len
IF @part < 0
PRINT SUBSTRING(@definition,@offset,100)
END
END
PRINT @endStmt;
fetch next from c into @definition,@uses_ansi_nulls,@uses_quoted_identifier
end
close c
deallocate c