Solved

Access Denied error when running xp_cmdshell  on SQL 2005

Posted on 2013-11-20
9
1,097 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with SQL joins 9 48
How to disable/enable multiple sql jobs in efficient way 11 108
Delete from table 6 47
How to simplify my SQL statement? 14 53
This article will describe one method to parse a delimited string into a table of data.   Why would I do that you ask?  Let's say that you need to pass multiple parameters into a stored procedure to search for.  For our sake, we'll say that we wa…
Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

810 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