这是为什么?!!!

huangkw007 2003-04-25 02:33:20
alter proc hkw
@com_id char(10) =null,
@com_name char(100)=null
AS
if @com_name is null
select total=sum(salary) from works where company_id=@com_id
else
select total=sum(a.salary) from works a,company b where b.company_name=@com_name and a.company_id=b.company_id
~~~~~~~~~~把这里改成like '%@com_name%',本来正确的结果,
现在结果变为NULL,有谁知道为什么吗?
order by total desc
go
exec hkw '','banka'
go
...全文
39 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
huangkw007 2003-04-25
  • 打赏
  • 举报
回复
一样的~~(:
SophiaWang 2003-04-25
  • 打赏
  • 举报
回复
其实,

alter proc hkw
@com_id char(10) =null,
@com_name char(100)=null
AS
if @com_name is null
select total=sum(salary) from works where company_id=@com_id
else
exec('select total=sum(a.salary) from works a,company b where b.company_name like ''%'+rtrim(@com_name)+'%''and a.company_id=b.company_id order by total desc')
go

exec hkw '','banka'
go

也可以
joygxd 2003-04-25
  • 打赏
  • 举报
回复
那是个变量
SophiaWang 2003-04-25
  • 打赏
  • 举报
回复
那我就来接分了,哈~~~
SophiaWang 2003-04-25
  • 打赏
  • 举报
回复
好象你已经搞定了??
huangkw007 2003-04-25
  • 打赏
  • 举报
回复
明白了,刚才查了一下帮助要这样写
alter proc hkw
@com_id char(10) =null,
@com_name char(100)=null
AS
if @com_name is null
select total=sum(salary) from works where company_id=@com_id
else
begin
SELECT @com_name = '%'+RTRIM(@com_name) + '%'
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`要这样写!
select total=sum(a.salary) from works a,company b where b.company_name like @com_name and a.company_id=b.company_id
order by total desc
end
go
exec hkw '','banka'
go

34,872

社区成员

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

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