Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

If condition in Store Proc

Posted on 2011-03-23
7
Medium Priority
?
385 Views
Last Modified: 2012-05-11
Hi ,

I was trying to put if condition in my store proc ,it throws error. I know am doing wrong some where,can some one guide me please

Here what am trying to achieve is ,when ever i do update,i will check the existing record and get the "ClassValue" and check the parameter am getting if both are not equal then i will update "ClassValue" ="01" if not then i will pass the parameter.

Am not sure i miss something in my If condition look like


CREATE PROC [dbo].[InsertEmployee]
      @EmployeeNo int,
      @ClassValue Decimal(18,2)
AS
IF NOT EXISTS (select employeeno from Phone where employeeno = @EmployeeNo and PhoneNumber = @PhoneNumber)
BEGIN
      DECLARE @CONTEXT_INFO varchar(100)
      SELECT @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)
      Declare @Level deceimal(18,2)

      SELECT
             @Level = Ad.ClassValue
      FROM
             Employee Ad
      WHERE Ad.Employeeno= @EmployeeNo

If(@Level !=Ad.ClassValue)
BEGIN
update [Employee] set ClassValue= '01', UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo 
END
 Else
Begin
update [Employee] set ClassValue= ClassValue, UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo 

End
Else
Begin	

     I will insert.
END

Open in new window


0
Comment
Question by:Sha1395
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 35196868
what about:
CREATE PROC [dbo].[InsertEmployee]
      @EmployeeNo int,
      @ClassValue Decimal(18,2)
AS
IF NOT EXISTS (select employeeno from Phone where employeeno = @EmployeeNo and PhoneNumber = @PhoneNumber)
BEGIN
      DECLARE @CONTEXT_INFO varchar(100)
      SELECT @CONTEXT_INFO = COALESCE(CONVERT(VARCHAR(128), CONTEXT_INFO()), CURRENT_USER)



  update [Employee] 
     set ClassValue = CASE WHEN ClassValue = @ClassValue THEN '01' ELSE @ClassValue END
       , UpdatedBy = @CONTEXT_INFO 
  where employeeno = @EmployeeNo 

  if @@rowcount = 0
  begin
     '  I will insert.
  end
END

Open in new window

0
 

Author Comment

by:Sha1395
ID: 35196898
Thanks angelIII,

Please correct me if am wrong

     set ClassValue = CASE WHEN ClassValue <> @ClassValue THEN '01' ELSE @ClassValue END


Condition am trying to implement in update query is

If(ClassValue !=@classValue)
{
ClassValue ="01"
else
ClassValue=@ClassValue
}

This is what am trying.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35196922
exactly
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Sha1395
ID: 35196925
Thanks again AngelIII for your help on yesterday and today's queries.
0
 

Author Comment

by:Sha1395
ID: 35205887
Hi AgnelIII,

My condition changed little bit. what if is suppose to use multiple if condition in my Query.

Like this

If(@Level ='V2') and (Ad.ClassValue='v1')
BEGIN
update [Employee] set ClassValue= '01', UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo
END
 Elseif (@Level ='V3') and (Ad.ClassValue='v2')
begin
update [Employee] set ClassValue= '01', UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo
End
Elseif (@Level ='V4') and (Ad.ClassValue='v3')
begin
update [Employee] set ClassValue= '01', UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo
End
Else
update [Employee] set ClassValue= '00', UpdatedBy = @CONTEXT_INFO where employeeno = @EmployeeNo
End
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35205940
you change the CASE syntax:
set ClassValue = CASE 
   WHEN ClassValue = 'V1' AND @ClassValue = '02' THEN '01' 
   WHEN ClassValue = 'V2' AND @ClassValue = '03' THEN '01' 
   WHEN ClassValue = 'V3' AND @ClassValue = '04' THEN '01' 
   ELSE '00' END

Open in new window

0
 

Author Comment

by:Sha1395
ID: 35205954
Thank you so much AngelIII
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question