views:

224

answers:

3

The title kind of says it, but I’ll elaborate a bit.

I use SQL Server 2005, and I'm writing an export query that’ll return a list of users. Some users have profile images, some don’t. The profile image is not represented in the database, at all. There’s just a file on the filesystem, named [username].jpg. So what I need to do is to check if such a file exists for the user.

+1  A: 

Well if you must you could always do the following

DECLARE  @fso int,
         @hr int
EXECUTE @hr = sp_OACreate 'Scripting.FileSystemObject', @fso OUT

From that you may (if hr=0) get a good old FileSystemObject.

From that you should be able to do the following

EXECUTE sp_OAMethod @fso, 'FileExists', @hr OUT, @FileName

But really, for wanting to do this, you're going to hell **grin**

Of course this depends on a lot of things, your SQL server configuration, the user it runs under, etc etc. Your mileage may vary.

blowdart
Going to hell, you say. I sense this solution is not what one might call best practice. Am I reading you right? :) Should I look into some other solution?
Marcus L
Heh. Well ActiveX inside SQL has all sorts of implications, not least security problems.If you could rewrite the upload so it sets a flag in the database, or the filename that would be better, then update the table in a one time hit
blowdart
Yes, I absolutely see your point. Might have to do that, as it feels like the most sensible solution in the end. It's just an extra bit in the DB. Either that, or I'll make some kind of SSIS package that solves it for me.
Marcus L
A: 

Is there a good reason not to store the images in the database? You don't really want to access the file system from your database back-end, because giving a database user filesystem access has pretty big security implications. That leaves two options: store the images in the database, or access the files only from your client application.

The "good" reason for not storing images in the database is that databases are built for data, and filesystems based for files. When the web application was built, this export was not required, so all handling of files was left to the filesystem - which I still think was the best solution, and practice for storing files.
Marcus L
How big are the images? If they're under 1MB, the DB can handle them fairly well.
GilaMonster
They're generally less than 1MB, but in the end, changing how the whole app handles user images is not really an option in this case.
Marcus L
+1  A: 

If you must do this from within sql you could use xp_cmdshell and 'if exist'. This isn't enabled by default for security reasons, so be careful.

enable xp_cmdshell:

sp_configure 'show advanced options', 1
reconfigure
sp_configure 'xp_cmdshell', 1
reconfigure

use xp_cmdshell:

xp_cmdshell 'if exist c:\blargh.txt echo 1'
rorr
Does it have to be enabled permanently, or is it possible to simply enable it while the query runs, then disable it again?But since everyone seems to think it's bad practice, I'll probably solve it some other way.
Marcus L
you can enable and then disable as needed.
rorr