Solved

If condition in Store Proc

Posted on 2011-03-23
7
376 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
  • 4
  • 3
7 Comments
 
LVL 142

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 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 35196922
exactly
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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 142

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
I have a large data set and a SSIS package. How can I load this file in multi threading?
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
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

919 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now