CREATE TABLE 工资表 (
员工号 char(10),
工资项目 char(10),
中文说明 char(20),
数据 decimal(16, 2),
是否计算项目 char(3));
INSERT INTO 工资表 VALUES (
'A0001',
'GZ1',
'底薪',
5700.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ2',
'平常加班小时',
10.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ3',
'假日加班小时',
16.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ4',
'加班费',
0,
'YES');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ5',
'所得税',
200.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ6',
'应发工资',
0,
'YES');
员工号 char(10),
工资项目 char(10),
中文说明 char(20),
数据 decimal(16, 2),
是否计算项目 char(3));
INSERT INTO 工资表 VALUES (
'A0001',
'GZ1',
'底薪',
5700.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ2',
'平常加班小时',
10.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ3',
'假日加班小时',
16.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ4',
'加班费',
0,
'YES');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ5',
'所得税',
200.00,
'NO');
INSERT INTO 工资表 VALUES (
'A0001',
'GZ6',
'应发工资',
0,
'YES');
解决方案 »
- 条件语句
- 关于对 SQL 2000 OLE 自动化解释详细的书
- SQL server新手求助
- sql数据删除恢复
- 大家帮忙看看这个存储过程在调用的时候条件应该怎么样给
- 十万火急啊!!!!![DBNETLIB][ConnectionOpen(connect()).]SQL Server 不存在或拒绝访问
- 如何在一个网段内搜索有哪些计算机上安装MySQL 的Server??
- 如何加速数据库访问,大家赶快帮忙啊
- 一个ORALC的问题!
- 发短信的realdreaming进来
- 急问:SQL Server 2000中用insert向一个表中插入记录,为何select出后发现没按插入的顺序排列?
- 求助:关于有条件的存储过程的分页,解决后立即解贴,谢谢
select GZ1,GZ2,GZ3,GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2,GZ5,GZ1+GZ4-GZ5 from [工资表]如果公式是不断变化的,那就麻烦了,考虑使用动态sql
工资项目 char(10),
公式 char(255),
说明 char(200));
INSERT INTO 公式表 VALUES (
'GZ4',
'GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2',
'底薪/22天/8小时*平时加班小时*1.5倍 + 底薪/22天/8小时*假日加班小时*2倍');
INSERT INTO 公式表 VALUES (
'GZ6',
'GZ1+GZ4-GZ5',
'底薪 + 加班费 - 所得税');
员工号 char(10),
工资项目 char(10),
中文说明 char(20),
数据 decimal(16, 2),
是否计算项目 char(3));
select 员工号, case... as 工资项目... from 工资表 where
[email protected]: zxyjyzxyjy(星星)
是啊,我的公式是动态的.
--------------------------------------------------------------------------
declare @s varchar(8000)
declare @v varchar(10)
declare @t varchar(100)select 员工号,工资项目,中文说明,数据 into temp from 工资表set @s = ''
select @s = @s+',['+rtrim(工资项目)+'] decimal(16, 2)'
from 工资表 group by 工资项目set @s = 'create table ttt(EmployeeID char(10)'+@s+')'
exec(@s)set @s = ''select @s = @s + ',case when '''+rtrim(是否计算项目)+''' = ''NO''
then sum(case when '''+rtrim(工资项目)+'''=工资项目 then 数据 else 0 end)
else 0 end'
from 工资表 group by 工资项目,是否计算项目
set @s = 'insert into ttt select 员工号' + @s + ' from 工资表 group by 员工号'
exec(@s)DECLARE t_cursor CURSOR FOR SELECT 工资项目, 公式 FROM 公式表 order by 工资项目
OPEN t_cursor
FETCH NEXT FROM t_cursor INTO @v, @t
WHILE @@FETCH_STATUS = 0
BEGIN
set @s = 'update ttt set '+@v+' = round('+@t+',0)'
exec(@s)
set @s = 'update a set a.数据 = b.'+@v+' from temp a,ttt b
where a.员工号 = b.EmployeeID and a.工资项目 = '''+@v+''''
exec(@s)
FETCH NEXT FROM t_cursor INTO @v, @t
END
CLOSE t_cursor
DEALLOCATE t_cursorselect * from temp--删除中间表
drop table ttt
drop table temp
update gz set bb=fs from gz inner join sf on gz.sno=sf.sno
while
(select count(*) from (select * from gz where bb is not null) a inner join gz on (charindex(gz.sno,a.bb)>0) )>0
begin
update gz set bb=replace(a2,b1,b2) from (select a1=sno,a2=bb from gz where gz.bb is not null) a inner join (select b1=sno,b2=qty from Gz) b on (charindex(b1,a2)>0) where bb is not null
end
select * from Gz
alter table Gz drop column bb
insert into Gz
select 'A0001','GZ1','底薪', 5700.00,'NO' union all
select 'A0001','GZ2','常加班小时' ,10.00,'NO' union all
select 'A0001','GZ3','假日加班小时' ,16.00,'NO' union all
select 'A0001','GZ4','加班费' ,.00,'YES' union all
select 'A0001','GZ5','所得税' , 200.00,'NO' union all
select 'A0001','GZ6','应发工资' ,.00,'YES'
------------------------另一表---------------------------------------------------
Create Table Sf(Sno varchar(4),fs varchar(50),me varchar(100))
insert into sf
select 'GZ4','GZ1/22/8*GZ2*1.5 + GZ1/22/8*GZ3*2','底薪/22天/8小时*平时加班小时*1.5倍 + 底薪/22天/8小时*假日加班小时*2倍' union all
select 'GZ6','GZ1+GZ4-GZ5','底薪 + 加班费 - 所得税'
------------------------------------------------------------------------------------
---------------------過程實現---------------------------------------------------
alter table Gz add bb varchar(50) null --------------結Gz 表加一個字段
Create Proc kkk
as
begin
update gz set bb=fs from gz inner join sf on gz.sno=sf.sno
while
(select count(*) from (select * from gz where bb is not null) a inner join gz on (charindex(gz.sno,a.bb)>0) )>0
begin
update gz set bb=replace(a2,b1,b2) from (select a1=sno,a2=bb from gz where gz.bb is not null) a inner join (select b1=sno,b2=qty from Gz) b on (charindex(b1,a2)>0) where bb is not null
end
select * from Gz where bb is not null
end
---------------------------結束-----------------------------------
exec kkk -------------執行過程
----------------結果--------------------------
A0001 GZ4 加班? .00 YES 5700.00/22/8*10.00*1.5 + 5700.00/22/8*16.00*2
A0001 GZ6 ??工? .00 YES 5700.00/22/8*10.00*1.5 + 5700.00/22/8*16.00*2----------------------隻算到這一步把 結果表達示放在一個字段裡,隻要對這字段進行處理就可
這個處理也可放在過程裡 ,不過時間問題 暫時沒有做完
给你发完了,你看看吧,能不能用上