Solved

if and else in stored procedure

Posted on 2016-09-02
19
35 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
  • 9
  • 5
  • 4
  • +1
19 Comments
 
LVL 45

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
 
LVL 32

Expert Comment

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

Accepted Solution

by:
Vitor Montalvão earned 250 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 32

Assisted Solution

by:ste5an
ste5an earned 250 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 142

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 45

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 45

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 32

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 32

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 32

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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Problem with SqlConnection 5 124
sql query 7 37
Recurring Excel Timelime for Veeam 2 37
Query to Add Late Tolerance 10 31
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

743 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

15 Experts available now in Live!

Get 1:1 Help Now