Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SET a variable with IF ELSE

Status
Not open for further replies.

Iamthestig

Programmer
Apr 30, 2008
38
GB
Can someone please help me with this?

DECLARE @REF int
DECLARE @PreferredEmail as varchar(50)
SET @REF = 102108

SET @PreferredEmail = IF (SELECT WorkEmailPreferred FROM tblContactPersonal WHERE CREF = @REF) =1
BEGIN
SELECT Email FROM tblContactPersonal WHERE CREF = @REF
END
ELSE
IF (SELECT PrivateEmailPreferred FROM tblContactPersonal WHERE CREF = @REF) = 1
BEGIN
SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF
END
ELSE
IF (SELECT Email FROM tblContactPersonal WHERE CREF = @REF) <> ''
BEGIN
SELECT Email FROM tblContactPersonal WHERE CREF = @REF
END
ELSE
IF (SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF) <> ''
BEGIN
SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF
END

I get an 'Incorrect syntax near the keyword 'IF' error.
 
Code:
DECLARE @REF int
DECLARE @PreferredEmail as varchar(50)
SET @REF = 102108

IF EXISTS(SELECT WorkEmailPreferred FROM tblContactPersonal WHERE CREF = @REF)
   BEGIN
       SET @PreferredEmail = ????????
       SELECT Email FROM tblContactPersonal WHERE CREF = @REF
   END
ELSE IF EXISTS(SELECT PrivateEmailPreferred FROM tblContactPersonal WHERE CREF = @REF)
     BEGIN
        SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF
     END
ELSE IF EXISTS(SELECT Email FROM tblContactPersonal WHERE CREF = @REF)
     BEGIN
        SELECT Email FROM tblContactPersonal WHERE CREF = @REF
     END
ELSE IF EXISTS(SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF)
     BEGIN
        SELECT PrivateEmail FROM tblContactPersonal WHERE CREF = @REF
     END

Not tested!

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top