Question : SQL CASE WITH IF

i cannot seem to find the correct syntax here. I have a cursor that retrieves a value for a field call @ACCATNUM. Its values will be between 31 and 36. I want to have a CASE statement that will then calculate the values of 6 other fields depending on the value of this field. What is the correct syntax here?
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
FETCH NEXT FROM GetJobLinkerCurs INTO @JOBNUMBER,@TRANSNMBR,@MASTERTYPE,@MASTERID,@MASTERNAME,@DEBITAMT,@CRDTAMNT,@VCHRNMBR,@DOCTYPE,@DOCDATE,@POSTDATE,@DOCNUMBR,@DOCAMNT,@TRXDSCRN,@DOCSOURCE,@ACTNUMST,@ACCATNUM
	WHILE ( @@FETCH_STATUS = 0 )
	BEGIN
	
		/* Break amounts into categories */
		SELECT CASE @ACCATNUM
			WHEN 31 THEN IF @DOCTYPE IN (1,2,3) THEN @LABORAMT = @LABORAMT + @DEBITAMT ELSE @LABORAMT = @LABORAMT - @CRDTAMNT END
			WHEN 32 THEN IF @DOCTYPE IN (1,2,3) THEN @MATLAMNT = @MATLAMNT + @DEBITAMT ELSE @MATLAMNT = @MATLAMNT - @CRDTAMNT END
			WHEN 33 THEN IF @DOCTYPE IN (1,2,3) THEN @DELIVAMT = @DELIVAMT + @DEBITAMT ELSE @DELIVAMT = @DELIVAMT - @CRDTAMNT END
			WHEN 34 THEN IF @DOCTYPE IN (1,2,3) THEN @OUTSVCAMT = @OUTSVCAMT + @DEBITAMT ELSE @OUTSVCAMT = @OUTSVCAMT - @CRDTAMNT END
			WHEN 35 THEN IF @DOCTYPE IN (1,2,3) THEN @MISCAMNT = @MISCAMNT + @DEBITAMT ELSE @MISCAMNT = @MISCAMNT - @CRDTAMNT END
			WHEN 36 THEN IF @DOCTYPE IN (1,2,3) THEN @CIASAMNT = @CIASAMNT + @DEBITAMT ELSE @CIASAMNT = @CIASAMNT - @CRDTAMNT END
		END

Answer : SQL CASE WITH IF

You cannot use a case here, case is not a statement block redirector like in some languages.
It is purely used as a expression conditional.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
FETCH NEXT FROM GetJobLinkerCurs INTO @JOBNUMBER,@TRANSNMBR,@MASTERTYPE,@MASTERID,@MASTERNAME,@DEBITAMT,
@CRDTAMNT,@VCHRNMBR,@DOCTYPE,@DOCDATE,@POSTDATE,@DOCNUMBR,@DOCAMNT,@TRXDSCRN,@DOCSOURCE,@ACTNUMST,@ACCATNUM
	WHILE ( @@FETCH_STATUS = 0 )
	BEGIN
	
		/* Break amounts into categories */
IF @ACCATNUM=31
	SET @LABORAMT = CASE WHEN @DOCTYPE IN (1,2,3) THEN @LABORAMT + @DEBITAMT ELSE @LABORAMT - @CRDTAMNT END
IF @ACCATNUM=32
	SET @MATLAMNT = CASE WHEN @DOCTYPE IN (1,2,3) THEN @MATLAMNT + @DEBITAMT ELSE @MATLAMNT - @CRDTAMNT END
..etc
Random Solutions  
 
programming4us programming4us