Solved

Access Denied error when running xp_cmdshell  on SQL 2005

Posted on 2013-11-20
9
1,105 Views
Last Modified: 2013-12-10
I need to update a process on a SQL 2005 package.

I need to copy, rename, and delete a file.

Here is my code:  

DECLARE
@TodayDate as varchar(40),
@TodayHour as varchar(40),
@TodayMinu  as varchar(40),
@NewFileName as varchar(100),
@cmdstr as varchar(328)
SELECT @TodayDate = CONVERT(varchar(10), GETDATE(), 112)
SELECT @TodayHour = DATEPART(hh,GETDATE())
SELECT @TodayMinu = DATEPART(mi,GETDATE())
SELECT @NewFileName = 'myfileone' + '_' + @TodayDate + '_' + @TodayHour + '_' + @TodayMinu + '.mdb'
print @NewFileName
set @cmdstr='copy \\servername\folderpath\myfileone.mdb  \\servername\folderpath\'  + @NewFileName  
print @cmdstr
EXEC master..xp_cmdshell @cmdstr
exec master..xp_cmdshell 'del \\servername\folderpath\myfileone.mdb'

The stored procedure does run without errors but I get the below message:

Access is denied.

Why am I getting this message?  The login I am running this under has full permissions,  How do I resolve this error?
0
Comment
Question by:programmher
  • 5
  • 3
9 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39662949
The mdb file is probably in use as it is a database file so you won't be able to delete it.

What are you trying to do?
0
 

Author Comment

by:programmher
ID: 39662994
I need to copy the existing .mdb, rename it, then delete the first .mdb.

I have checked and the .mdb file is not in use.
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39663021
If this is a SQL database and not an Access database, then unless the database has been detached, it will be in use.

I'm still at a loss as to what you're trying to achieve. I know that you're attempting to copy, rename and then delete, but what is the aim here?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:programmher
ID: 39663260
The size of the access database is exceeding the 2 GB limit.  Deleting the large database and replacing it with a smaller database is the manual work around.  I am trying to automate this process.

Do you know of an easier way to do this?   Is there a way to delete the records in the Access database from SQL then proceed with updating the tables?
0
 

Author Comment

by:programmher
ID: 39665113
I've requested that this question be deleted for the following reason:

I changed the solution to use all cmdexec commands instead of executing a stored procedure or executing SQL statements
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39665114
Please post your solution and accept your own answer if you found one so that it might help others.
0
 

Accepted Solution

by:
programmher earned 0 total points
ID: 39698938
First, I deleted the existing .mdb via  a SQL job's CMDEXEC command:  del: path the file existed.

Second, I copied a smaller version (with all the data elements deleted) of the mdb to the path in the same folder the final .mdb will reside:  copy:  filepath oldfilename.mdb newfilepath.

Last, I renamed the .mdb: rename "filepath\oldname.mdb" "newname.mdb"

The newname.mdb database was ready to receive new data from the schedued job.
0
 

Author Closing Comment

by:programmher
ID: 39708081
Selected own answer per admin's request instead of deleting the question.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Order by but want it in specific order 2 34
SQL Backup skipping a few tables 7 52
SQL Error - Query 6 42
Clone table from one server.database to another server.database 24 39
If you having speed problem in loading SQL Server Management Studio, try to uncheck these options in your internet browser (IE -> Internet Options / Advanced / Security):    . Check for publisher's certificate revocation    . Check for server ce…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question