August 25, 2005 at 3:02 am
I have a query which is automatically generated and stored in a text field. The query is > 8000 bytes long. If it was <4000 I could assign it to a variable and use sp_executesql @sql.
Alternatively I could call a procedure declaring a variable as ntext which is passed in (not declared locally) and still use sp_execute @sql. However the problem is how do I initialise the ntext variable in the stored procedure in the first place.?
Thanks in advance.
August 25, 2005 at 8:28 am
If you do not care about the context, you can pass the file name as a parameter in your SP and call isql to run it.
CREATE PROCEDURE dbo.usp_RunScript @ScriptFile nvarchar(255)
AS
DECLARE @Command nvarchar(2000)
DECLARE @ServerName nvarchar(255)
SELECT @ServerName=CAST(SERVERPROPERTY('servername') as nvarchar(255))
SET @Command=N'isql -S '+@ServerName+' -d '+DB_NAME()+' -E -i"'+@ScriptFile+'"'
EXEC master..xp_cmdshell @command
You can also sspecify an output file name. Please check with BOL
August 25, 2005 at 8:34 am
Sorry, I read the question as query stored in text file instead of text field. The above mentioned solution does not work for text field.
If you know the possible maximum length of the text field, you can define several nvarchar(4000) local variables and use substring function to get the chunks from the text field. Then run the dynamic query.
EXEC (@V1+@V2+@v3...)
August 25, 2005 at 8:42 am
Anotther solution is query the text field to a file and then run it in my previous suggested way.
To query the result to a file:
DECLARE @Command nvarchar(255)
DECLARE @ServerName nvarchar(255)
SELECT @ServerName=CAST(SERVERPROPERTY('servername') as nvarchar(255))
SET @Command=N'isql -S '+@ServerName+' -d your db -E -Q "SET NOCOUNT ON SELECT TOP 1 TextField AS [--Script] FROM YourTable" -o"C:\Test.Sql"'
EXEC master..xp_cmdshell @command
The trick is you have to use [--XXXX] as the alias name. Or use -h-1 switch to specify no headers in the output.
August 27, 2005 at 3:28 am
August 27, 2005 at 10:23 pm
Just so you know, you can use VARCHAR(8000) for this... won't help with things over 8000 characters like Peter's suggestion but you don't have to limit yourself to 4000 characters using NVARCHAR.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 29, 2005 at 9:16 pm
Ok. I don't know xp_cmdshell is disallowed in your environment.
If the query in th text field is >4000chars, sp_execsql does not work. However, you can concatenate multiple 8000bytes string and run it in EXEC. As for return value, you can get it in a tempory table.
CREATE TABLE #Result (RtnVal int NOT NULL)
SET @V3=' INSERT #Result (RtnVal) VALUES (0)'
EXEC (@V1+@V2+@v3)
SELECT RtnVal FROM #Result
You can use the tempory table to return whatever values you want.
August 30, 2005 at 3:55 pm
CREATE PROCEDURE dbo.MyProc @Script ntext
AS
......
GO
EXEC dbo.MyProc ''
and assign your SQL string to @Script inside SP.
_____________
Code for TallyGenerator
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply