1

I have the following query:

Declare @qty int
SET @qty = (SELECT Qty FROM StockTB WHERE ProductID='6' AND Qty=0)
if(@qty =0)
Update StockTB SET Qty=Qty+5, BatchNo='1234', ExpDate='03/11/2018' WHERE 
ProductID='6' AND Qty=0
Else
INSERT INTO StockTB Values('6', '5', '1234', '03/11/2018')

The above query is working fine. But when I add ELSE IF in the query, it fails. Here is after having ELSE IF implementation.

Declare @expdate date
SET @expdate =(SELECT ExpDate FROM StockTB WHERE ProductID='6' AND 
ExpDate='03/11/2018')
Declare @qty int
SET @qty = (SELECT Qty FROM StockTB WHERE ProductID='6' AND Qty=0)
if(@qty =0)
Update StockTB SET Qty=Qty+5, BatchNo='1234', ExpDate='03/11/2018' WHERE 
ProductID='6' AND Qty=0
Else if(@expdate='03/11/2018')
Update StockTB SET Qty=Qty+5 WHERE ProductID='6' AND ExpDate='03/11/2018'
Else
INSERT INTO StockTB Values('6', '5', '1234', '03/11/2018')
6
  • 2
    SET @qty = (SELECT Qty FROM StockTB WHERE ProductID='6' AND Qty=0) - when do you expect that to be non-zero? Commented Oct 18, 2017 at 12:44
  • 1
    @Damien_The_Unbeliever There's one case: if no result (NULL) is returned by the query. Commented Oct 18, 2017 at 12:54
  • 2
    @MatSnow - agreed - this would be a very bizarre way of writing what appears to just need to be a single MERGE. Commented Oct 18, 2017 at 12:55
  • 1
    What exactly do you mean " Failed to work"? What do you expect and what do you get? Commented Oct 18, 2017 at 12:56
  • 1
    You should read about MERGE. learn.microsoft.com/en-us/sql/t-sql/statements/… It is a lot easier way to handle this kind of thing. Commented Oct 18, 2017 at 13:09

2 Answers 2

2

I can't read your schema, but it seems that "ExpDate" is a date. You are comparing it to a varchar '03/11/2018'

I don't know if you mean November 3 or March 11. Neither SQL do know. Try to CONVERT(VARCHAR, ExpDate, 103) instead:

At: Else if(@expdate='03/11/2018')

Change it to: Else if(CONVERT(VARCHAR, @expdate, 103) = '03/11/2018')

You can also achieve the same result using that on the query to set the @expdate value (And make it VARCHAR, not date)

*Ps: The magic number 103 as argument means dd/mm/yyyy. You can get a full list here.

Sign up to request clarification or add additional context in comments.

1 Comment

Or use the ANSI standard YYYYMMDD.
0

Please try this

Declare @expdate date
SET @expdate =(SELECT ExpDate FROM StockTB WHERE ProductID='6' AND 
ExpDate='2018/11/03')
Declare @qty int
SET @qty = (SELECT Qty FROM StockTB WHERE ProductID='6' AND Qty=0)
if(@qty =0)
Update StockTB SET Qty=Qty+5, BatchNo='1234', ExpDate='2018/11/03' WHERE 
ProductID='6' AND Qty=0
Else 
if(@expdate='2018/11/03')
Update StockTB SET Qty=Qty+5 WHERE ProductID='6' AND ExpDate='2018/11/03'
Else
INSERT INTO StockTB Values('6', '5', '1234', '2018/11/03')

1 Comment

This isn't any different than what the OP posted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.