2
我在SQL Server 2000數據庫中有一個表,它存儲NTEXT
列中的SQL語句。我需要讀取其中一個值,使用UPDATETEXT
對其執行一些替換,然後使用sp_executesql
執行它。我知道你不能在SQL Server 2000中聲明NTEXT
變量,所以爲了解決這個問題,我已經聲明瞭一個臨時表來存儲該值並對其進行操作。最後一步是將該值傳遞給sp_executesql
,但我無法弄清楚如何做到這一點。目前爲止的代表代碼如下:如何讀取NTEXT值,然後使用SQL Server 2000中的sp_executesql執行它?
/*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
如何獲取和執行SQL語句?投射到varchar可能會截斷語句。
注:我意識到這是一種艱苦的做事方式;系統的設計超出了我的控制範圍。