开发者

How can I read an NTEXT value and then execute it using sp_executesql in SQL Server 2000?

I have a table in a SQL Server 2000 database which stores SQL statements in an NTEXT column. I need to read one of these values, perform some replacements on it using UPDATETEXT and then execute it using sp_executesql. I'm aware that you can't declare NTEXT variables in SQL Server 2000, so to workaround this I have decla开发者_开发技巧red a temporary table to store the value and manipulate it. The last step is to pass that value to sp_executesql, but I can't figure out how to do that. Representative code so far is below:

/*Create a temp table to store the NTEXT SQL statement*/
CREATE TABLE #temp
(
    SqlStatement NTEXT
)

/*Get the statement*/
INSERT INTO #temp (SqlStatement)
SELECT [SqlStatement]
FROM [Reports]
WHERE ID = @ID

-- Format placeholders in statement
DECLARE @placeholder VARCHAR(20)
DECLARE @placeholderIndex INT           
SET @placeholder = '@param1'

SELECT @placeholderIndex = (CHARINDEX(placeholder, SqlStatement) - 1) FROM #temp

/*Get a pointer to the NTEXT field*/
DECLARE @textPtr VARBINARY(16)
SELECT @textPtr = TEXTPTR(SqlStatement) FROM #temp

IF @placeholderIndex > 0
BEGIN         
UPDATETEXT #temp.SqlStatement @textPtr @placeholderIndex 7 'paramValue'
END

/*
Get the statement and execute
DECLARE @SqlText NTEXT -- This is not possible in SQL 2000
*/

exec sp_executesql @SqlText

How can I get and execute the SQL statement? Casting to a varchar would potentially truncate the statement.

NB: I realise this is a laborious way of doing things; the design of the system is out of my control.


sp_executesql and Long SQL Strings in SQL 2000

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜