Solved

If condition in Store Proc

Posted on 2011-03-23
7
379 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 500 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

756 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