【发布时间】:2019-05-20 20:24:06
【问题描述】:
我创建了一个 SQL 脚本来循环抛出数据库并创建一个函数(该函数的目的是将 RTF 转换为纯文本)。 我将创建函数的脚本放在一个变量上,并尝试使用 exec 命令执行它。
我使用了 While 循环,并将请求放在 varchar 变量上,然后,exec @command
但我收到此错误:
declare @Total as int
select @Total = count(*) from Temp1
declare @counter as int
set @counter = 1
declare @CurrentVal as varchar(max)
declare @command varchar(max)
while (@counter <= @Total)
begin
select @CurrentVal = name from Temp1 where RowId = @counter
set @command=' use '+@CurrentVal+'
GO
CREATE FUNCTION dbo.fnParseTEXTRTF
(
@rtf VARCHAR(max)
)
RETURNS VARCHAR(max)
AS
BEGIN
DECLARE @Stage TABLE
(
Chr CHAR(1),
Pos INT
)
INSERT @Stage
(
Chr,
Pos
)
SELECT SUBSTRING(@rtf, Number, 1),
Number
FROM master..spt_values
WHERE Type = ''p''
AND SUBSTRING(@rtf, Number, 1) IN (''{'', ''}'')
DECLARE @Pos1 INT,
@Pos2 INT
SELECT @Pos1 = MIN(Pos),
@Pos2 = MAX(Pos)
FROM @Stage
DELETE
FROM @Stage
WHERE Pos IN (@Pos1, @Pos2)
WHILE 1 = 1
BEGIN
SELECT TOP 1 @Pos1 = s1.Pos, @Pos2 = s2.Pos
FROM @Stage AS s1
INNER JOIN @Stage AS s2 ON s2.Pos > s1.Pos
WHERE s1.Chr = ''{''
AND s2.Chr = ''}''
ORDER BY s2.Pos - s1.Pos
IF @@ROWCOUNT = 0
BREAK
DELETE
FROM @Stage
WHERE Pos IN (@Pos1, @Pos2)
UPDATE @Stage
SET Pos = Pos - @Pos2 + @Pos1 - 1
WHERE Pos > @Pos2
SET @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, '''')
END
SET @Pos1 = PATINDEX(''%\cf[0123456789][0123456789 ]%'', @rtf)
WHILE @Pos1 > 0
SELECT @Pos2 = CHARINDEX('' '', @rtf, @Pos1 + 1), @rtf = STUFF(@rtf, @Pos1, @Pos2 - @Pos1 + 1, ''''), @Pos1 = PATINDEX(''%\cf[0123456789][0123456789 ]%'', @rtf)
SELECT @rtf = REPLACE(@rtf, ''\pard'', ''''), @rtf = REPLACE(@rtf, ''\par'', ''''), @rtf = case when LEN(@rtf)>0 then LEFT(@rtf, LEN(@rtf) - 1) else @rtf end
SELECT @rtf = REPLACE(@rtf, ''\b0 '', ''''), @rtf = REPLACE(@rtf, ''\b '', '''')
SELECT @rtf = STUFF(@rtf, 1, CHARINDEX('' '', @rtf), '''')
RETURN @rtf
end'
set @counter = @counter + 1
exec @command
end
【问题讨论】:
标签: sql-server type-conversion sql-server-2016 rtf sql-function