Advertisement

03.06.2008 at 12:22PM PST, ID: 23220964
[x]
Attachment Details

IF/ELSE statement in stored procedure not working correctly

Asked by ev72178 in SQL Server 2005

Tags: SQL Server 2005, TSQL

I want to import a files into a temp table and select a substring of the first record as the FileID. I then want to compare this FileID to the FileID column in my Pharmacy.PDEFileLog table. If there is a match and the UplaodedReturnFile column in my Pharmacy.PDEFileLog table is NULL, I want to update the table with certain values (see code below). If the FileID does not exist, I want to return a statemnt that the file does not exist and if the FileID does exists and the UplaodedReturnFile column in my Pharmacy.PDEFileLog table is 'TRUE', I want to return a statement saying that the file has already  been uploaded. Here is my code:
SELECT SUBSTRING(strRecord, 17, 3) as FileID
FROM #tmpDDPS

IF NOT EXISTS (SELECT FileID, UploadedReturnFile
FROM Pharmacy.PDEFileLog
WHERE FileID = FileID AND UploadedReturnFile='TRUE')

BEGIN
UPDATE Pharmacy.PDEFileLog
SET UploadedReturnFile='TRUE', DateUploadedReturnFile=GETDATE(), UploadedBy=USER
WHERE FileID = FileID
END

ELSE

BEGIN
PRINT 'This file has already been uploaded to the server.'
END

The above code updates all of my records in the Pharmacy.PDEFileLog table if the FileID exists. Not sure where to go from here. Any help would be appreciated. Thx.Start Free Trial
[+][-]03.06.2008 at 12:26PM PST, ID: 21064355

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2008 at 12:27PM PST, ID: 21064361

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: SQL Server 2005
Tags: SQL Server 2005, TSQL
Sign Up Now!
Solution Provided By: chapmandew
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628