如果您被迫使用 SQL Server 2000,您几乎必须使用text
orntext
字段来分别超过 4000/8000 个字符的nvarchar
限制varchar
。
您不能在text
和ntext
字段上使用常规字符串命令,这使它变得一团糟。
如果您真的希望每个存储过程都在一行中,您可以尝试以下操作。
下面的代码是可憎的,没有两种方法。我接受了您的基本查询,并使用游标READTEXT
和UPDATETEXT
,在一行中创建了存储过程列表。代码中的注释应该有望帮助确定我正在尝试做什么。
这还没有经过 100% 的测试,所以如果有任何问题,请告诉我。
-- Gonzalo's original code --
SELECT sm.id, COUNT(sm.colid) AS Cantidad
INTO #SPrepeated
FROM syscomments AS sm INNER JOIN
sysobjects AS so ON sm.id = so.id
WHERE (so.status >= 0) AND (so.xtype = 'P') AND (so.category = 0)
GROUP BY sm.id, so.name
HAVING (COUNT(sm.colid) > 1)
SELECT sm.id, sm.colid, OBJECT_NAME(sm.id) AS object_name,
cast(sm.text as ntext) as [text]
into #Tresult
FROM syscomments AS sm
JOIN sysobjects AS so ON sm.id = so.id
JOIN #SPrepeated as spr ON so.id = spr.id
WHERE (so.status >= 0) AND (so.xtype = 'P')
-- Create our #TresultSingle temporary table structure --
SELECT TOP 1 [id], object_name, cast([text] as ntext) as [text]
INTO #TresultSingle
FROM #Tresult
-- Clear out the table, ready to insert --
TRUNCATE TABLE #TresultSingle
DECLARE @id int, @previd int, @colid int, @objectname nvarchar(4000),
@text nvarchar(4000)
DECLARE @ptrval varbinary(16), @offset int
SET @text = ''
-- Begin cursor, and start praying --
DECLARE ResultCursor CURSOR
FOR
SELECT [id], colid, [object_name], [text]
FROM #Tresult
ORDER BY [id], colid
OPEN ResultCursor
FETCH NEXT FROM ResultCursor
INTO @id, @colid, @objectname, @text
INSERT INTO #TresultSingle
SELECT @id, @objectname, @text
WHILE @@FETCH_STATUS = 0
BEGIN
-- If the ID has changed, create a new record in #TresultSingle --
IF @id <> @previd
BEGIN
INSERT INTO #TresultSingle
SELECT @id, @objectname, @text
END
ELSE
BEGIN
-- Get the textpointer of the current @id --
SELECT @ptrval = TEXTPTR(text)
FROM #TresultSingle
WHERE [id] = @id
-- Set our offset for inserting text --
SET @offset = 4000 * (@colid - 1)
-- Add the new text to the end of the existing text --
UPDATETEXT #TresultSingle.text @ptrval @offset 0 @text
END
SET @previd = @id
FETCH NEXT FROM ResultCursor
INTO @id, @colid, @objectname, @text
END
CLOSE ResultCursor
DEALLOCATE ResultCursor
SELECT * FROM #TresultSingle
DROP TABLE #TresultSingle
drop table #Tresult
drop table #SPrepeated