Search code examples
sqlsql-servercaseinsert-into

Insert into with case statement


I am messing around with some things in a test database and came across something I would like to do. In the future I will have to update some information in one table with new dates and budget info. Here is my code...

USE REPORTING
DECLARE
@JAN MONEY = 100.00

INSERT INTO FACTBUDGET (BUDGETAMOUNT)

SELECT
CASE WHEN BUDGETDATEID > 20141231 AND BUDGETDATEID < 20150201 AND MISCID = 0 
THEN @JAN
ELSE 'BUDGET NEEDED'
END AS BUDGETDATEID

FROM FACTBUDGET

When I run this the variable goes into that column but to almost all records. Am i doing this the easiest way? I was thinking of creating a temp table but thought that would be too much data to create.


Solution

  • Do you want to do an update?

    UPDATE FACTBUDGET
        SET BUDGETAMOUNT = @JAN
        WHERE BUDGETDATEID > 20141231 AND BUDGETDATEID < 20150201 AND MISCID = 0 ;