Solved

If condition in Store Proc

Posted on 2011-03-23
7
375 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
Comment Utility
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
Comment Utility
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]
Comment Utility
exactly
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Author Comment

by:Sha1395
Comment Utility
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]
Comment Utility
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
Comment Utility
Thank you so much AngelIII
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

In this article—a derivative of my DaytaBase.org blog post (http://daytabase.org/2011/06/18/what-week-is-it/)—I will explore a few different perspectives on which week today's date falls within using Microsoft SQL Server. First, to frame this stu…
Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 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

10 Experts available now in Live!

Get 1:1 Help Now