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

x
?
Solved

if and else in stored procedure

Posted on 2016-09-02
19
Medium Priority
?
62 Views
Last Modified: 2016-09-03
Hello ,
I have a stored procedure :


      
  SET @sql = 'UPDATE table1  SET   [DeleteRecord] = 0
	              WHERE  [Name] = '''+ @Name  + ''''
				 
	  EXEC(@sql)
	
	    --Return @@Rowcount
		return 2

Open in new window


How do I return 1 when deleterecord field is updated to 0  and  0 when the deleteRecord field was already ''

Cheers
0
Comment
Question by:RIAS
[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
  • 9
  • 5
  • 4
  • +1
19 Comments
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41781440
I can see that you tried with @@RowCount before. Why you commented that part of code?
0
 

Author Comment

by:RIAS
ID: 41781445
Because it returns 1 in any case
0
 

Author Comment

by:RIAS
ID: 41781446
want to know whether it actually updated the field
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 35

Expert Comment

by:ste5an
ID: 41781449
Where is the sense in dynamic SQL in this scenario? Enabling SQL injection?
0
 
LVL 52

Accepted Solution

by:
Vitor Montalvão earned 1000 total points
ID: 41781452
It's returning 1 because it will always update even the DeleteRecord=0.
You can add that filter to your WHERE statement to it will only update when the value is different than zero:
SET @sql = 'UPDATE table1  
             SET   [DeleteRecord] = 0
             WHERE  [DeleteRecord] > 0 AND [Name] = '''+ @Name  + ''''
				 
EXEC(@sql)
Return @@Rowcount

Open in new window

0
 
LVL 35

Assisted Solution

by:ste5an
ste5an earned 1000 total points
ID: 41781453
E.g.

UPDATE table1  
SET   [DeleteRecord] = 0
WHERE  [Name] = @Name AND [DeleteRecord] <> 0;

Open in new window

0
 

Author Closing Comment

by:RIAS
ID: 41781457
Cheers!
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 41781458
the dynamic sql issue apart:

 SET @sql = 'UPDATE table1  SET   [DeleteRecord] = 0
	              WHERE  [Name] = '''+ @Name  + '''  
AND ( [DeleteRecord] <> 0 OR [DeleteRecord] IS NULL) '

Open in new window

that will make the @@recordcount variable return 1 or 0 accordingly.
however, it will not tell you if the 0 would come from Name = @Name not matching or the value already being 0

and not, there is no other way other then checking the actual value, eventually using the OUTPUT clause: https://msdn.microsoft.com/en-us/library/ms177564.aspx
1
 

Author Comment

by:RIAS
ID: 41781495
Any suggestion on how to go about this this one:

      UPDATE [dbo].[table1]
      SET  
              [Number]=CAST(@Number AS  Nvarchar(50)),            
                  [DatesCovered]= CAST(@DatesCovered AS  varchar(50)),             
                  [Date]=CAST(@Date AS  nvarchar(20)),
                  [Details]=  CAST(@Details  AS Nvarchar(100)),                  
                  [ReqBy]=CAST(@ReqBy  AS Nvarchar(50)),            
                 [DeleteRecord] = 0
            WHERE ([Number] =@Number AND [Date] = @Date AND [Details] =@Details )
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41781496
Suggestion for what?
0
 

Author Comment

by:RIAS
ID: 41781501
where clause where it returns rowcount correctly
update only when number<> @number and date<> @date and details <>@details
0
 
LVL 52

Expert Comment

by:Vitor Montalvão
ID: 41781507
If you want to follow the same line of thought then you'll need to add all fields that are being updated:
UPDATE [dbo].[table1]
SET  
               [Number]=CAST(@Number AS  Nvarchar(50)),            
               [DatesCovered]= CAST(@DatesCovered AS  varchar(50)),             
               [Date]=CAST(@Date AS  nvarchar(20)),
               [Details]=  CAST(@Details  AS Nvarchar(100)),                  
               [ReqBy]=CAST(@ReqBy  AS Nvarchar(50)),            
               [DeleteRecord] = 0
WHERE 
               [Number]<>CAST(@Number AS  Nvarchar(50)) AND            
               [DatesCovered]<>CAST(@DatesCovered AS  varchar(50)) AND
               [Date]<>CAST(@Date AS  nvarchar(20)) AND
               [Details]<>CAST(@Details  AS Nvarchar(100)) AND
               [ReqBy]<>CAST(@ReqBy  AS Nvarchar(50)) AND
               [DeleteRecord]<>0

Open in new window

NOTE: This will only update the record if ALL 6 columns have different values from the variables. If you want the update to happen when AT LEAST ONE if different then change the ANDs to ORs.
1
 

Author Comment

by:RIAS
ID: 41781509
Thanks a ton Vitor!!! Really appreciate your help!
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41781642
where clause where it returns rowcount correctly
 update only when number<> @number and date<> @date and details <>@details

Seems you still have a misconception here: @@ROWCOUNT returns the rows touched by your statement. Thus the values in the columns are set to the new value. Regarding less which value they had before, as long as you don't use an appropriate predicate to exclude these rows.

SQL Server does not distinguish between the difference that changing a value to the same value is in our human real-world logic no change. SQL Server updates such a value. It's basically the same as using identity functions in Mathematics. No real use for humans, but very important piece of theory.

btw: USE THE CODE BUTTON!!

E.g.
UPDATE  [dbo].[table1]
SET     [DatesCovered] = @DatesCovered ,
        [ReqBy] = @ReqBy ,
        [DeleteRecord] = 0
WHERE   [Number] = @Number
        AND [Date] = @Date
        AND [Details] = @Details
        AND ( [DatesCovered] <> @DatesCovered
              OR [ReqBy] <> @ReqBy
              OR [DeleteRecord] <> 0
            ); 

Open in new window


p.s. Where is the sense in using NVARCHAR for every data type?
1
 

Author Comment

by:RIAS
ID: 41781683
ste5an,
p.s. Where is the sense in using NVARCHAR for every data type?

The input to the stored procedure is from vbb.net  app so Nvarchar as thay are srings

Cheers
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41781731
?? Why. VB.NET has more data types than only strings.
0
 

Author Comment

by:RIAS
ID: 41783021
There were lotz of error when I tried to pass the variables as date and integer.
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41783101
You don't do you a favor, when you parse the data at the lowest level, cause it's harder to bubble up the correct error messages.

Seems like your front-end does not handle the values correctly.

This should be fixed.

Just as an example: I've seen an application with a database without those kind of checks. After two years of work it was shut down. Cause it was no longer possible to retrieve meaningful information from it.
0
 

Author Comment

by:RIAS
ID: 41783168
Thanks Ste5an, but I dont understand why it so wrong ? The stored procedure does convert the datatypes to correct datatype and it works well.
0

Featured Post

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

610 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