Hi all. I'm trying to retrieve ID Codes of employees that have we have no ID Picture saved yet. The ID Pictures' file names are based on an Employee's ID Code. Can I put a path in a where statement in SQL in order to do this?
This is what I thought of at first but I know it's incorrect: where('C:\Portal\Images\EmployeePhotos' + EmployeeCode + '.jpg')
I just hope it gives you a clear idea of what I am trying to accomplish here.
You need to compare your path/name to something. The WHERE clause is looking for true or false (Boolean) expression. WHERE
My brain is not working this morning so my NOT EXISTS example is not coming to mind.
You can use master.sys.xp_fileexist to check if a file exists. Probably easiest / most reuseable to create a function to do it:
CREATE FUNCTION dbo.CheckFileExists (
@filename varchar(255)
)
RETURNS bit
AS
BEGIN
DECLARE @FileExists int
EXEC Master.dbo.xp_fileexist @filename, @FileExists OUTPUT
RETURN @FileExists
END /*FUNCTION*/
Then call the function from the query:
SELECT ...
FROM dbo.employees e
CROSS APPLY (
SELECT 'C:\Portal\Images\EmployeePhotos\' + EmployeeCode + '.jpg' AS emp_picture_filename
) AS assign_alias_names
WHERE dbo.CheckFileExists(emp_picture_filename) = 0