Hi,
I want to select number of record from my product table which have the image file exist in server side through sql server. Is it possible? Please give some idea. THanks
(1)Do you have SQL2000? If so, I am thinking of a solution that uses a UDF (User Defined Function)
(2) Does the path&filename for the image file exist as column in the Products table?
For example, the UDF could return 1 if the file exists, 0 if it doesn't. It would work approx. like this:
Select SUM(ImgExists(ColumnWithPath)) as ExCount
From Products
Other variations of the Select statement would be possible if, for instance, you wanted to list the records that did (or did not) have the Image on disk.
But for this approach to work, you need SQL2000. Is that what you have?
hi bperry,
i hv sql 2000, but how to include the UDF? iIf I dont't have the full path of image, how my select statement will be? Thanks for your idea, yet I think I need more information.
Okay,
Many times in SQL Server, folks won't actually store images directly in the database (although that is quite possible.) Instead, what they will do is store the path&file name of the image in an ordinary varchar column. Something like 'c:\products\pictures\widget.gif'. Then it becomes the job of the application (ASP, whatever...) to read that column to get the path&file name, and then retrieve the image from the disk.
I was assuming (maybe incorrectly) that this is what you were doing. So when I said this:
Select SUM(ImgExists(ColumnWithPath)) as ExCount
From Products
I thought you had a column in your table like I have described.
If this is not what you were looking for, then my apologies. (It's too bad, because I actually have it working!) Would you like to describe further what it is you are interested in?
Hi,
I trying to pass the physical path of all my images located in server, from there i would like to do a select statement to select the item where imgExits( path name & itemfilename) = true. Or my other idea is can function pass back the whole recordset so that i can use it in my asp. I trying to filter the item which those tat hv image to be displayed on the web page. Thanks.
Hi bperry,
I have a file name in my product table, and i will do a select statement as this 'select 'c:\temp\products\' + Productfilename as pathName from tblproduct WHERE ImgExists(Pathname)=1', yet when i try to create function for ImgExits, using the scripting.FileSystemObject, it prompted error. May I know how you detect the existing of file in ImgExists? Thanks.
The solution uses a UDF to determine whether the file exists on disk. It returns 1 if exists, 0 if not.
Here is the function:
CREATE FUNCTION FileExists (
@File varchar(50)
)
Returns int AS
BEGIN
--------------------
-- Method 1
--------------------
declare @result int
exec master..xp_fileexist @File, @result out
Return @result
--------------------
-- Method 2
--------------------
--declare @result int
--exec @result =master..xp_getfiledetails @File
--return ABS(@result - 1)
END
----------------- And here are some example of how you might use it.
-------------------------
-- List all that exist
-------------------------
Select * from Products
Where dbo.FileExists('c:\temp\products\' + ImageName) = 1
-------------------------
-- List all do not exist
-------------------------
Select * from Products
Where dbo.FileExists('c:\temp\products\' + ImageName) = 0
-------------------------
-- Count do not exist
-------------------------
Select Count(*) as DoesNotExist
From
(
Select * from Products
Where dbo.FileExists('c:\temp\products\' + ImageName) = 0
) dt
-------------------------
-- Count do exist
-------------------------
Select Count(*) as DoesExist
From
(
Select * from Products
Where dbo.FileExists('c:\temp\products\' + ImageName) = 1
) dt
or
Select
SUM(dbo.FileExists('c:\temp\products\' + ImageName))
as DoesExist
From Products
----------------------
Oh yes, I almost forgot.
This will work fine if you plan to run it as sa or other member of sys admin.
If you need an 'ordinary' user to call this function, then you will need to make a security change in the master database. I (or someone else here) can help you with that if you need it.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.