select case 工种 when '电工' then 1000 case 工种 when '机修工' then 1500 end [工资] from tableName
update table set 工资=case 工种 when '电工' then 1000 when '机修工' then 1500 end
用case when then 如: select case 工种 when '电工' then 1000 when '机修工' then 1500 end as 工资 from tableNameor update tablename set 工资=case 工种 when '电工' then 1000 when '机修工' then 1500 else 0 end
select case when 工种='电工' then 1000 when 工种='机修工' then 1500 end [工资] from tableName
楼上这些我都知道,问题是表达式不是固定的,是由一个字符串变量确定的,比如:select @expr = 'case 工种 when 'xx' then 1000 end' 或 select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end'在pb中可以用datastore计算列来做,在存储过程中怎么实现?
你上面的语句: select @expr = 'case 工种 when 'xx' then 1000 end' 或 select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end' 在SQL的存储过程中好象也是可以执行的
你的工种,计算方式都是表的字段?可以用类似下面的方法来执行declare @expr varchar(1000) declare @sql nvarchar(4000),@re nt set @expr='case 工种 when ''xx'' then 1000 end' --这是你传递过来的计算变量 set @sql='select @r=('+@expr+') from 取数的表' exec sp_executesql @sql,N'@r int output',@re out print '结果:' print @re
当然,如果你只是计算某个表达的值,那就更简单,像这样:declare @expr varchar(1000) declare @sql nvarchar(4000),@re nt set @expr='1312+4398+(33/12-98)*(34+3)' --要计算的表达式 set @sql='select @r=('+@expr+')' exec sp_executesql @sql,N'@r int output',@re out print '结果:' print @re
zjcxc(邹建)接近我的思路,我马上试试.
zjcxc(邹建)的贴子: 如何实现同一字段的下一值减上一值。 下面是数据测试--创建数据测试环境 declare @tb table(t int,i int) insert into @tb(t) select top 10 colid from syscolumns--更新数据 declare @i int,@ti int set @i=0 update @tb set @i=case @i when 0 then 1 else 0 end ,@ti=case @i when 1 then t else t-@ti end ,i=case @i when 1 then i else @ti end--显示更新后的结果 select * from @tb
to zjcxc(邹建): 还有一个问题,exec sp_executesql @sql,N'@r int output',@re out这里参数是静态指定的, 有没有办法动态指定?比如@sql中用到三个参数,那么在参数列表中能动态的指定这三个参数?
case 工种 when '机修工' then 1500 end [工资]
from tableName
when '机修工' then 1500 end
如:
select case 工种 when '电工' then 1000 when '机修工' then 1500 end as 工资
from tableNameor
update tablename set 工资=case 工种 when '电工' then 1000
when '机修工' then 1500 else 0 end
when 工种='机修工' then 1500 end [工资]
from tableName
或
select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end'在pb中可以用datastore计算列来做,在存储过程中怎么实现?
select @expr = 'case 工种 when 'xx' then 1000 end'
或
select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end'
在SQL的存储过程中好象也是可以执行的
declare @sql nvarchar(4000),@re nt
set @expr='case 工种 when ''xx'' then 1000 end' --这是你传递过来的计算变量
set @sql='select @r=('+@expr+') from 取数的表'
exec sp_executesql @sql,N'@r int output',@re out
print '结果:'
print @re
declare @sql nvarchar(4000),@re nt
set @expr='1312+4398+(33/12-98)*(34+3)' --要计算的表达式
set @sql='select @r=('+@expr+')'
exec sp_executesql @sql,N'@r int output',@re out
print '结果:'
print @re
下面是数据测试--创建数据测试环境
declare @tb table(t int,i int)
insert into @tb(t)
select top 10 colid from syscolumns--更新数据
declare @i int,@ti int
set @i=0
update @tb set @i=case @i when 0 then 1 else 0 end
,@ti=case @i when 1 then t else t-@ti end
,i=case @i when 1 then i else @ti end--显示更新后的结果
select * from @tb
还有一个问题,exec sp_executesql @sql,N'@r int output',@re out这里参数是静态指定的,
有没有办法动态指定?比如@sql中用到三个参数,那么在参数列表中能动态的指定这三个参数?