How do I use an IF type structure in an UPDATE statement?

Rod

In T-SQL you can use a CASE statement when doing a SELECT to determine what sort of data you want to return. I'd like to do something like that, but not in a SELECT statement, but instead in an UPDATE statement. I don't know if this is the correct format or not, but hopefully you'll get the idea from this.

Here's what I'm trying to do:

CREATE PROCEDURE spSomePS
    @Number1 int,
    @Number2 int,
    @Amt money
AS
BEGIN
    UPATE SomeTable
    CASE @Number1 +1 = @Number2
         MoneyColumn = MoneyColumn - @Amt
    ELSE
         MoneyColumn = MoneyColumn + @Amt
    END -- end the CASE statement

Will that so what I'm trying to do or is there another syntax?

Hamlet Hakobyan

You can use CASE expression in UPDATE statement as in SELECT statement.

CREATE PROCEDURE spSomePS
@Number1 int,
@Number2 int,
@Amt money
AS
BEGIN
UPATE SomeTable
SET MoneyColumn = 
  CASE WHEN @Number1 +1 = @Number2
       THEN MoneyColumn - @Amt
       ELSE MoneyColumn + @Amt
  END -- end the CASE statement

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I use INNER JOIN in an UPDATE statement?

How do I use the If statement?

How do I set a variable in an UPDATE statement, then use that variable in a xml.modify() statement?

How do I deserialize this type of data structure

How do I convert the following SQL Statement to an Update Statement

How do I unitize a variable in structure that is of type structure

when defining a private structure in a class how do i use it as a function parameter or return type?

How do i update multiple records using MERGE statement and use max(column_value) based on previously updated records in the same statement?

How do i use an if statement in react?

How do I use a trim statement correctly?

How do I use a switch statement in a binding?

How do I use an AND statement in XPATH?

How do I use a transaction with a SCHEDULE statement?

How do I use a SQL If statement here?

How do i use with statement in Elixir?

How do I use AND and OR in a single statement

How do I use an IN statement in SSRS Formula

How do I update remote database structure with local changes?

How I can use ArrayList field in Jooq with update statement

How would I use excel to generate a large update sql statement?

How can I use multiple substring functions in an update statement?

How do I estimate the execution time of a SQL Server Update Statement?

How do I update a value of a variable in an if statement in Go?

How can I do an UPDATE statement with JOIN in SQL Server?

How do I run an SQL update query using a like statement

How do I update a single element of an ARRAY with a prepared statement?

How do I write a PostGIS update statement that ignores invalid geometries?

How do I access a variable, created outside of an IF/ELSE scope, update it in the IF statement, and access that update in the ELSE statement?

How can I use "type" as a structure attribute in Go?