日期:2014-05-17 浏览次数:20451 次
WITH a1 (id,sal,HOURS,DAYS,operator) AS
(
select 1, 200, 2, 3,'+,*' UNION ALL
select 2, 250, 3, 4,'*,-' UNION ALL
select 3, 350, 2, 5,'-,/'
)
,a2 AS
(
select
CASE
WHEN LEFT(operator,1)='+' THEN (sal+HOURS)
WHEN LEFT(operator,1)='-' THEN (sal-HOURS)
WHEN LEFT(operator,1)='*' THEN (sal*HOURS)
WHEN LEFT(operator,1)='/' THEN (sal/HOURS)
END result,DAYS,operator
FROM a1
)
select
CASE
WHEN right(operator,1)='+' THEN (result+DAYS)
WHEN right(operator,1)='-' THEN (result-DAYS)
WHEN right(operator,1)='*' THEN (result*DAYS)
WHEN right(operator,1)='/' THEN (result/DAYS)
END result
FROM a2
if OBJECT_ID('tempdb..#temp', 'u') is not null drop table #temp;
go
create table #temp( [id] varchar(100), [sal] int, [hours] int, [days] int, [operator] varchar(100));
insert #temp
select '1','200','2','3','+,*' union all
select '2','250','3','4','*,-' union all
select '3','350','2','5','-,/'
--SQL:
SELECT
id, sal, [hours], [days], operator,
result = CAST(
CASE RIGHT(operator, 1)