来自 sp_OACreate 'Scripting.FileSystemObject' 的错误

问题描述 投票:0回答:1

我正在使用以下函数将文本从 SQL-Server 存储过程写入文件:-

USE [mydb]
GO
/****** Object:  StoredProcedure [dbo].[spWriteStringToFile]    Script Date: 03/01/2012 15:51:27 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spWriteStringToFile]
(
@String Varchar(max), --8000 in SQL Server 2000
@Path VARCHAR(255),
@Filename VARCHAR(100)
)
AS
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

使用此存储过程会导致错误消息:-

Msg 50000, Level 16, State 1, Procedure spWriteStringToFile, Line 48
Error whilst opening the File System Object, 

现在我不太了解 SQL Server,尤其是存储过程;谁能告诉我问题可能出在哪里?我尝试从 sp_OACreate 调用中获取错误号,但没有成功。看着这个问题here我检查了 scrrun.dll 是否存在,它确实存在。

编辑顺便说一下,存储过程的名称带有红色下划线,并显示“无效的对象名称”;该对象必须存在,否则我无法编辑它。我应该担心这个吗?

sql-server stored-procedures
1个回答
2
投票

看起来您是从 Simple Talk 的网站复制了该存储过程,并且代码中存在一些问题。

首先,您需要先创建存储过程,然后才能执行它 - 将顶部的 ALTER 更改为 CREATE,然后您就可以运行它了。

其次,如果您有一个区分大小写的数据库,则变量(@HR 和 @hr)的混合大小写也会出错 - 它们需要全部相同。

这应该可以解决问题:

CREATE PROCEDURE spWriteStringToFile
 (
@String NVarchar(max), --8000 in SQL Server 2000
@Path VARCHAR(255),
@Filename VARCHAR(100)

--
)
AS
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 @objFileSystem 
© www.soinside.com 2019 - 2024. All rights reserved.