April 6, 2009 at 12:06 pm
I built a simple procedure below to learn more tsql. The output of the print statement produces the correct output. I can take the output of the print statemenet and it executes in the same query window and it works properly. I even tried running this as a job that executes as "sa" and received the same error. This is written in SQL Server 2008.
create proc [dbo].[sp_delete_old_files] @backupDir varchar(50),
@exttype varchar(3)
as
declare @olddate varchar(19)
declare @deletefiles varchar(100)
begin
set @olddate = CONVERT(VARCHAR, GetDate() - 1,126)
set @deletefiles = 'master.dbo.xp_delete_file 0,N''' + @backupDir +
''',N''' + @exttype + ''',N''' + @olddate + ''',1'
print @deletefiles
exec @deletefiles
end
run this command
exec sp_delete_old_files 'c:\testfolder', 'txt'
get this output and error
master.dbo.xp_delete_file 0,N'c:\testfolder',N'txt',N'2009-04-05T12:53:54',1
Msg 2812, Level 16, State 62, Procedure sp_delete_old_files, Line 14
Could not find stored procedure 'master.dbo.xp_delete_file 0,N'c:\testfolder',N'txt',N'2009-04-05T12:53:54',1'
April 6, 2009 at 1:24 pm
cchart3 (4/6/2009)
I built a simple procedure below to learn more tsql. The output of the print statement produces the correct output. I can take the output of the print statemenet and it executes in the same query window and it works properly. I even tried running this as a job that executes as "sa" and received the same error. This is written in SQL Server 2008.create proc [dbo].[sp_delete_old_files] @backupDir varchar(50),
@exttype varchar(3)
as
declare @olddate varchar(19)
declare @deletefiles varchar(100)
begin
set @olddate = CONVERT(VARCHAR, GetDate() - 1,126)
set @deletefiles = 'master.dbo.xp_delete_file 0,N''' + @backupDir +
''',N''' + @exttype + ''',N''' + @olddate + ''',1'
print @deletefiles
exec @deletefiles
end
run this command
exec sp_delete_old_files 'c:\testfolder', 'txt'
get this output and error
master.dbo.xp_delete_file 0,N'c:\testfolder',N'txt',N'2009-04-05T12:53:54',1
Msg 2812, Level 16, State 62, Procedure sp_delete_old_files, Line 14
Could not find stored procedure 'master.dbo.xp_delete_file 0,N'c:\testfolder',N'txt',N'2009-04-05T12:53:54',1'
Try this:
exec (@deletefiles)
April 7, 2009 at 9:52 am
A blog on xp_delete_file
http://sqlblog.com/blogs/andy_leonard/archive/2009/03/11/xp-delete-file.aspx
declare @DeleteDate nvarchar(50)
declare @DeleteDateTime datetime
set @DeleteDateTime = DateAdd(hh, -24, GetDate())
set @DeleteDate = (Select Replace(Convert(nvarchar, @DeleteDateTime, 111), '/', '-') + 'T' + Convert(nvarchar, @DeleteDateTime, 108))
EXECUTE master.dbo.xp_delete_file 0,N'E:\WSSLogs',N'log',@DeleteDate,1
April 7, 2009 at 5:38 pm
Jerry,
I do not understand why I would want to do something in three lines that I can do in one. My time statement seems much more efficient. Also you can pass in variables to my sql. You can use it in all systems and you could use it for bak, log files, and etc. I am new to TSQl and are more familiar with oracle, but usually less code is better and variables are always better than hard coding. Is there something I am missing?
create procedure both_the_same
as
declare @DeleteDate nvarchar(50)
declare @olddate varchar(19)
declare @DeleteDateTime datetime
set @DeleteDateTime = DateAdd(hh, -24, GetDate())
set @DeleteDate = (Select Replace(Convert(nvarchar, @DeleteDateTime, 111), '/', '-') + 'T' + Convert(nvarchar, @DeleteDateTime, 108))
set @olddate = CONVERT(VARCHAR, GetDate() - 1,126)
print @DeleteDate
print @olddate
exec both_the_same
2009-04-06T18:28:17
2009-04-06T18:28:17
April 7, 2009 at 7:50 pm
Curious, you respond to Jerry, but not a word about wether or not my suggested change helped you or not? Sorry, but something is missing there, don't you think?
April 7, 2009 at 9:28 pm
Lynn,
Sorry I did not reply to reply to your response. Your answer worked great. I responded to the other answer, because it did not make since to me why you would do more work than you needed. I am new to TSQL but his code looked like it was less efficient than my code. I was hoping I was missing something from his what he posted and that I was looking at something wrong. I am always trying to learn. Again think for your answer, it was exactly what I needed.
April 7, 2009 at 9:35 pm
Glad to hear it worked. Just so you know, it is just common curtesy to let people know if your problem has been solved and how. This is especially true if you find your own resolution. Others may have similar issues/problems and seeing your may help.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply