Hi,
Have a bit of a problem with a couple of stored procedure (one calls the other) which I use to extract routines from a SQL 2012 database into individual files. I get an error message about creating the output files when I attempt to extract all procedures, however when I run this for a single procedure (which previously failed) it works.
This is the error:
Msg 50000, Level 16, State 1, Procedure usp_WriteStringToFile, Line 56
Error whilst Creating file "D:\SQLs\USP_EVENTFUNCTION_UPD.SQL",
Can't be down to permissions as I get the first 250 of 1000 procedures extracted, then each one fails with the same error. I wonder if it could be memory, however the error does not back that up.
Here is the code.
Calling procedure
ALTER PROCEDURE [Admin].[usp_StoredProcedures_Dump] ( @Path nvarchar(max), @ProcedureName NVARCHAR(MAX) = NULL, @Schema nvarchar(64) = NULL, @RoutineType nvarchar(64) = NULL ) AS BEGIN DECLARE @Filename nvarchar(512) = ''; DECLARE @SPECIFIC_NAME nvarchar(512); DECLARE @ROUTINE_SCHEMA nvarchar(50); DECLARE @ROUTINE_TYPE nvarchar(10); DECLARE @ROUTINE_DEFINITION nvarchar(max); DECLARE RoutinesCusor CURSOR FAST_FORWARD READ_ONLY FOR SELECT SPECIFIC_NAME, ROUTINE_SCHEMA, ROUTINE_TYPE, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE (@ProcedureName IS NULL OR @ProcedureName = SPECIFIC_NAME) AND (@Schema IS NULL OR @Schema = ROUTINE_SCHEMA) AND (@RoutineType IS NULL OR @RoutineType = ROUTINE_TYPE) ORDER BY ROUTINE_NAME ASC; OPEN RoutinesCusor; FETCH NEXT FROM RoutinesCusor INTO @SPECIFIC_NAME, @ROUTINE_SCHEMA, @ROUTINE_TYPE, @ROUTINE_DEFINITION; WHILE (@@FETCH_STATUS = 0) BEGIN SET @Filename = ''; IF (ISNULL(@ROUTINE_DEFINITION,'') <> '') BEGIN PRINT @SPECIFIC_NAME; SET @Filename = UPPER(RTRIM(LTRIM(@SPECIFIC_NAME))) + '.SQL'; EXEC Admin.usp_WriteStringToFile @ROUTINE_DEFINITION, 'D:\SQLs', @Filename --SELECT @@error PRINT '---------------------------------------------'; PRINT ''; END FETCH NEXT FROM RoutinesCusor INTO @SPECIFIC_NAME, @ROUTINE_SCHEMA, @ROUTINE_TYPE, @ROUTINE_DEFINITION; END CLOSE RoutinesCusor; DEALLOCATE RoutinesCusor; END
This is the child procedure (taken from the internet).
ALTER PROCEDURE [Admin].[usp_WriteStringToFile] ( @String varchar(max), @Path varchar(255), @Filename varchar(100) ) AS BEGIN DECLARE @objFileSystem int ,@objTextStream int, @objErrorObject int, @strErrorMessage Varchar(1000), @Command varchar(1000), @hr int, @fileAndPath varchar(80) set nocount on select @strErrorMessage='opening the File System Object' EXECUTE @hr = sp_OACreate 'Scripting.FileSystemObject' , @objFileSystem OUT Select @FileAndPath=@path+'\'+@filename if @HR=0 Select @objErrorObject=@objFileSystem , @strErrorMessage='Creating file "'+@FileAndPath+'"' if @HR=0 execute @hr = sp_OAMethod @objFileSystem , 'CreateTextFile' , @objTextStream OUT, @FileAndPath,2,True if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='writing to the file "'+@FileAndPath+'"' if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Write', Null, @String if @HR=0 Select @objErrorObject=@objTextStream, @strErrorMessage='closing the file "'+@FileAndPath+'"' if @HR=0 execute @hr = sp_OAMethod @objTextStream, 'Close' if @hr<>0 begin Declare @Source varchar(255), @Description Varchar(255), @Helpfile Varchar(255), @HelpID int EXECUTE sp_OAGetErrorInfo @objErrorObject, @source output,@Description output,@Helpfile output,@HelpID output Select @strErrorMessage='Error whilst '+coalesce(@strErrorMessage,'doing something')+', '+coalesce(@Description,'') raiserror (@strErrorMessage,16,1) end --EXECUTE sp_OADestroy @objTextStream; EXECUTE sp_OADestroy @objTextStream; END