想知道在存储过程中如何使用动态表达式.

bcboy 2003-07-28 04:48:31
比如,有个表达式:
if 工种 = '电工' then 工资 = 1000
if 工种 = '机修工' then 工资 = 1500

想用在存储过程中,不知道如何设置.
...全文
27 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
gmlxf 2003-08-01
  • 打赏
  • 举报
回复
学习。
bcboy 2003-08-01
  • 打赏
  • 举报
回复
up
bcboy 2003-08-01
  • 打赏
  • 举报
回复
继续up
bcboy 2003-07-31
  • 打赏
  • 举报
回复
to zjcxc(邹建):
还有一个问题,exec sp_executesql @sql,N'@r int output',@re out这里参数是静态指定的,
有没有办法动态指定?比如@sql中用到三个参数,那么在参数列表中能动态的指定这三个参数?
txlicenhe 2003-07-31
  • 打赏
  • 举报
回复
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
bcboy 2003-07-31
  • 打赏
  • 举报
回复
zjcxc(邹建)接近我的思路,我马上试试.
bcboy 2003-07-30
  • 打赏
  • 举报
回复
没人用过么?
czb2000 2003-07-30
  • 打赏
  • 举报
回复
是写一个存贮过程吗
zjcxc 元老 2003-07-30
  • 打赏
  • 举报
回复
当然,如果你只是计算某个表达的值,那就更简单,像这样:

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 元老 2003-07-30
  • 打赏
  • 举报
回复
你的工种,计算方式都是表的字段?

可以用类似下面的方法来执行

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
PrimalFancy 2003-07-30
  • 打赏
  • 举报
回复
你上面的语句:
select @expr = 'case 工种 when 'xx' then 1000 end'

select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end'
在SQL的存储过程中好象也是可以执行的
bcboy 2003-07-29
  • 打赏
  • 举报
回复
up
bcboy 2003-07-29
  • 打赏
  • 举报
回复
楼上这些我都知道,问题是表达式不是固定的,是由一个字符串变量确定的,比如:

select @expr = 'case 工种 when 'xx' then 1000 end'

select @expr = 'case 计算方式 when 1 then 200 when 2 then 基本工资 * 2 end'

在pb中可以用datastore计算列来做,在存储过程中怎么实现?
CrazyFor 2003-07-28
  • 打赏
  • 举报
回复
select case when 工种='电工' then 1000
when 工种='机修工' then 1500 end [工资]
from tableName
sdhdy 2003-07-28
  • 打赏
  • 举报
回复
用case when then
如:
select case 工种 when '电工' then 1000 when '机修工' then 1500 end as 工资
from tableName

or
update tablename set 工资=case 工种 when '电工' then 1000
when '机修工' then 1500 else 0 end
friendliu 2003-07-28
  • 打赏
  • 举报
回复
update table set 工资=case 工种 when '电工' then 1000
when '机修工' then 1500 end
nboys 2003-07-28
  • 打赏
  • 举报
回复
select case 工种 when '电工' then 1000
case 工种 when '机修工' then 1500 end [工资]
from tableName
nboys 2003-07-28
  • 打赏
  • 举报
回复
select case 工种 when '电工' then 1000
case 工种 when '机修工' then 1500 end [工资]
from tableName

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧