Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-10-03 : 11:20:03
|
| Hi GuysI am using the following query but it is failing:INSERT INTO SESSALESQTYRESTRICTIONS( ItemRelation ,MaxQty ,CustCode )SELECT v.[Part Number] ,CASE WHEN v.[Quantity] <> 0 THEN (INSERT INTO SESSALESQTYRESTRICTIONS ( ItemRelation ,MaxQty ) SELECT v.[Part Number] ,v.[Quantity] FROM v_ItemDetails AS v ) ELSE v.[Quantity] END ,2FROM v_ItemDetails AS vThe thing I am trying to do is if the Quantity is not the same 0 to Insert the record again so there are two of them. Is this possible to do using CASE?Thanks |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 11:24:28
|
| [code]INSERT INTO SESSALESQTYRESTRICTIONS(ItemRelation,MaxQty,CustCode)SELECTv.[Part Number],v.[Quantity],2FROM v_ItemDetails AS vWHERE v.[Quantity]=0INSERT INTO SESSALESQTYRESTRICTIONS(ItemRelation,MaxQty,CustCode)SELECTv.[Part Number],v.[Quantity],2FROM v_ItemDetails AS vCROSS JOIN (SELECT 1 AS ID UNION ALL SELECT 2)tmpWHERE v.[Quantity]<>0[/code] |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-03 : 11:41:04
|
or combine themINSERT INTO SESSALESQTYRESTRICTIONS(ItemRelation,MaxQty,CustCode)SELECTv.[Part Number],v.[Quantity],2FROM v_ItemDetails AS vCROSS JOIN (SELECT 1 AS ID UNION ALL SELECT 2)tmpWHERE (v.[Quantity]<>0OR tmp.ID=1) |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-10-04 : 04:16:28
|
| HeyThat's great, thanks for that!!!Was there ever a way to do it through CASE? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-04 : 06:39:39
|
quote: Originally posted by rcr69er HeyThat's great, thanks for that!!!Was there ever a way to do it through CASE?
nope. you cant use CASE to manipulate control flow. case can only be used to conditionally return value for a field. |
 |
|
|
rcr69er
Constraint Violating Yak Guru
327 Posts |
Posted - 2008-10-05 : 19:09:05
|
| Could you use an IF statement?Thanks |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-06 : 00:40:28
|
quote: Originally posted by rcr69er Could you use an IF statement?Thanks
yup you can. but the solution i provided is much better approach. why do you want to use if? |
 |
|
|
|
|
|
|
|