--记录信息 declare @o int ,@f int ,@t int ,@ret int ,@info varchar(1000) --要记录的信息 ,@path varchar(255) --日志文件路径select @info='update表于 '+convert(varchar,getdate(),120) ,@path='c:\log.txt'exec sp_oacreate 'scripting.filesystemobject',@o outexec sp_oamethod @o,'openTextFile',@f out,@path,8,1exec @ret=sp_oamethod @f,'writeline',NULL,@info exec sp_OADestroy @o exec sp_oastop
--sfile_log(文件日志) if exists (select * from sysobjects where name='zd_log_sfile') drop table zd_log_sfile go begin create table zd_log_sfile ( log_time datetime not null, log_pathname varchar(200) not null,--处理的文件路径 log_filename varchar(200) not null,--处理的文件名 log_filetype int not null,--0发送,1接受 log_status int not null,--传输状态,0失败,1成功,2待处理 re varchar(100) null,--备注 log_content varchar(1000) null, constraint PK_zd_log_sfile primary key clustered(log_time,log_pathname,log_filename) ) end go--建立触发器 create trigger tr_logsfile on dbo.zd_log_sfile for insert as begin declare @cmd varchar(1000), @vfilename varchar(2000), @vfile varchar(4000), @vtype int, @vsta int, @o int, @f int, @t int, @ret intSELECT @vfilename=log_pathname+log_filename, @vfile=log_content, @vtype=log_filetype, @vsta=log_status FROM inserted i if @vtype=0 and @vsta=2 begin -- SET @cmd = 'echo ' + @vfile + ' > '+@vfilename --EXEC master..xp_cmdshell @cmd exec sp_oacreate 'scripting.filesystemobject',@o out exec sp_oamethod @o,'openTextFile',@f out,@vfilename,8,1 exec @ret=sp_oamethod @f,'writeline',NULL,@vfile exec sp_OADestroy @o exec sp_oastop end end --插入数据 insert zd_log_sfile values('2006-02-14 09:15:21.237' ,'d:\CFTP\13\sfile\', '20060214090521-zjff.txt', 0, 2, ' ', '1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')
declare @o int
,@f int
,@t int
,@ret int
,@info varchar(1000) --要记录的信息
,@path varchar(255) --日志文件路径select @info='update表于 '+convert(varchar,getdate(),120)
,@path='c:\log.txt'exec sp_oacreate 'scripting.filesystemobject',@o outexec sp_oamethod @o,'openTextFile',@f out,@path,8,1exec @ret=sp_oamethod @f,'writeline',NULL,@info
exec sp_OADestroy @o
exec sp_oastop
if exists (select * from sysobjects where name='zd_log_sfile')
drop table zd_log_sfile
go
begin
create table zd_log_sfile
(
log_time datetime not null,
log_pathname varchar(200) not null,--处理的文件路径
log_filename varchar(200) not null,--处理的文件名
log_filetype int not null,--0发送,1接受
log_status int not null,--传输状态,0失败,1成功,2待处理
re varchar(100) null,--备注
log_content varchar(1000) null,
constraint PK_zd_log_sfile primary key clustered(log_time,log_pathname,log_filename)
)
end
go--建立触发器
create trigger tr_logsfile on dbo.zd_log_sfile for insert as
begin
declare
@cmd varchar(1000),
@vfilename varchar(2000),
@vfile varchar(4000),
@vtype int,
@vsta int,
@o int,
@f int,
@t int,
@ret intSELECT
@vfilename=log_pathname+log_filename,
@vfile=log_content,
@vtype=log_filetype,
@vsta=log_status
FROM inserted i
if @vtype=0 and @vsta=2
begin
-- SET @cmd = 'echo ' + @vfile + ' > '+@vfilename
--EXEC master..xp_cmdshell @cmd
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'openTextFile',@f out,@vfilename,8,1
exec @ret=sp_oamethod @f,'writeline',NULL,@vfile
exec sp_OADestroy @o
exec sp_oastop
end
end
--插入数据
insert zd_log_sfile values('2006-02-14 09:15:21.237'
,'d:\CFTP\13\sfile\',
'20060214090521-zjff.txt',
0,
2,
' ',
'1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')
insert zd_log_sfile values(getdate(),@vfilepath1,@vfilename1,0,2,'',@vcontent1)
如果把zd_log_sfile中的触发器tr_logsfile删除后,则a中的数据也可以插入,而且上面的insert zd_log_sfile也可以成功.
但是如果我把zd_log_sfile中的触发器tr_logsfile建立后,对a表的插入操作都失败。
另外.如果不在a表中运行insert zd_log_sfile,而是单独运行--插入数据
insert zd_log_sfile values('2006-02-14 09:15:21.237'
,'d:\CFTP\13\sfile\',
'20060214090521-zjff.txt',
0,
2,
' ',
'1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')这段sql语句,生成文件成功。把我都搞晕了,不知道怎么回事.
--确定文件路径存在!insert zd_log_sfile values('2006-02-14 09:15:21.237'
,'C:\',
'20060214090521-zjff.txt',
0,
2,
' ',
'1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')
if exists (select * from sysobjects where name='zd_log_sfile')
drop table zd_log_sfile
go
begin
create table zd_log_sfile
(
log_time datetime not null,
log_pathname varchar(200) not null,--处理的文件路径
log_filename varchar(200) not null,--处理的文件名
log_filetype int not null,--0发送,1接受
log_status int not null,--传输状态,0失败,1成功,2待处理
re varchar(100) null,--备注
log_content varchar(1000) null,
constraint PK_zd_log_sfile primary key clustered(log_time,log_pathname,log_filename)
)
end
go--建立触发器
create trigger tr_logsfile on dbo.zd_log_sfile for insert as
begin
declare
@cmd varchar(1000),
@vfilename varchar(2000),
@vfile varchar(4000),
@vtype int,
@vsta int,
@o int,
@f int,
@t int,
@ret intSELECT
@vfilename=log_pathname+log_filename,
@vfile=log_content,
@vtype=log_filetype,
@vsta=log_status
FROM inserted i
if @vtype=0 and @vsta=2
begin
-- SET @cmd = 'echo ' + @vfile + ' > '+@vfilename
--EXEC master..xp_cmdshell @cmd
exec sp_oacreate 'scripting.filesystemobject',@o out
exec sp_oamethod @o,'openTextFile',@f out,@vfilename,8,1
exec @ret=sp_oamethod @f,'writeline',NULL,@vfile
exec sp_OADestroy @o
exec sp_oastop
end
end--插入数据
insert zd_log_sfile values('2006-02-14 09:15:21.237'
,'C:\',
'20060214090521-zjff.txt',
0,
2,
' ',
'1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')--查看C:\下,是否有文本文件生成'20060214090521-zjff.txt',我这边执行成功,你仔细检查下!
insert zd_log_sfile values(getdate(),@vfilepath1,@vfilename1,0,2,'',@vcontent1)
如果把zd_log_sfile中的触发器tr_logsfile删除后,则a中的数据也可以插入,而且上面的insert zd_log_sfile也可以成功.
但是如果我把zd_log_sfile中的触发器tr_logsfile建立后,对a表的插入操作都失败。
另外.如果不在a表中运行insert zd_log_sfile,而是单独运行--插入数据
insert zd_log_sfile values('2006-02-14 09:15:21.237'
,'d:\CFTP\13\sfile\',
'20060214090521-zjff.txt',
0,
2,
' ',
'1390000001,1390000022,2006-02-14 09:05:00,22,00,13,国家,河北省,aaaaa,asfad,正常,证件发放')这段sql语句,生成文件成功。楼上的,我说了,单独做都会成功。
但是从a表中插入时就不行。
我在查询分析器里用的是sa登录,但在程序里是其他用户,对xp_cmdshell操作的权限不够。谢谢萧萧的耐心,马上结贴给分。