Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hello,

Some Location path is saved in one of the SQL table in some string format.
I want to know if any location path saved in database table is accessible or not using SQL Query.
please suggest.

Thanks in advance.
Posted
Updated 25-Feb-14 20:28pm
v2
Comments
Maciej Los 26-Feb-14 2:26am    
What?
phil.o 26-Feb-14 2:43am    
I think OP means that he has some column in a table holding filesystem paths, and wants to check from SQL the validity of each of these paths.
ali khanna 26-Feb-14 2:51am    
Exactly. The location is saved in some of the table, and I want to verify if that location is accessible or not from SQL Query only

It is 'xp_cmdshell': http://technet.microsoft.com/en-us/library/ms175046.aspx[^]

but you may have to configure it:
SQL
EXEC sp_configure 'show advanced options', 1
go
reconfigure
go
EXEC sp_configure'xp_cmdshell', 1 
go
exec xp_cmdshell 'dir c:\'


of course, you must adapt this procedure usage to your case.

[update]
tested for sql-server 2008 R2 express:

SQL
if not exists(select * from tempdb.dbo.sysobjects
    where name like '#tmptable_%')
begin
    create table #tmptable (dirs varchar(255));
    insert into #tmptable(dirs)
    values('d:\tmp\'), ('d:\source');
    create table #restable (dir_list varchar(255));
end


declare @mydir sysname;
declare @cmd sysname;
declare dircursor cursor for select * from #tmptable;
open dircursor;
fetch next from dircursor into @mydir;
while @@FETCH_STATUS=0
begin
    set @cmd='dir '+@mydir+' /b';
    insert into #restable(dir_list) exec xp_cmdshell @cmd;
    if exists (select * from #restable where dir_list='File Not Found')
        print 'not a dir: '+@mydir;
    else
        print 'exists: '+@mydir;
    delete from #restable;
    fetch next from dircursor into @mydir;
end
close dircursor;
deallocate dircursor
go
 
Share this answer
 
v3
Comments
coolRahul_12 26-Feb-14 3:52am    
Looks good 5+ :)
No, you cannot do so. The SQL Server may run on a different computer than the application requesting the information. The user running the application may be different from the "user" running the query (the SQL Server normally runs under a service account) and thus have different mapped network drives, different access rights to local files etc.
You have to provide the data saved in the table to the client which in turn has to check the availability of the files.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900