请看一下我的SQL语句,为什么会出现这样的错误呢?

vc6delphi6 2004-04-22 10:25:49
declare @nd varchar(2),@oldnd varchar(2)
set @nd='03'
set @oldnd=case when len(convert(int,@nd)-1)=1 then '0'+convert(char(1),convert(int,@nd)-1) else convert(char(2),convert(int,@nd)-1) end

select *,isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)=jb.月度 and 进出口=0),0) as 出口当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)=jb.月度 and 进出口=1),0) as 进口当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)=jb.月度),0) as 进出口同期当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)=jb.月度 and 进出口=0),0) as 出口同期当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)=jb.月度 and 进出口=1),0) as 进口同期当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=jb.月度),0)as 进出口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=jb.月度 and 进出口=0),0) as 出口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=jb.月度 and 进出口=1),0) as 进口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=jb.月度),0) as 进出口当月同期累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=jb.月度 and 进出口=0),0) as 出口当月同期累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=jb.月度 and 进出口=1),0) as 进口当月同期累计

from
(
select sum(美元) as 进出口当月,substring(日期,3,2) as 月度 from hgsj where substring(日期,1,2)=@nd group by substring(日期,3,2)

)jb



提示错误:
服务器: 消息 4408,级别 16,状态 1,行 5
查询和其中的视图或函数超过了 256 个表的限制。


其中hgsj为视图
将子查询去掉几个就没有错误了,
但是去掉了功能就实现不了了,怎么让他没有错误呢
...全文
28 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
8992026 2004-04-22
  • 打赏
  • 举报
回复
试试这样,减少差不多一半的连接表数:

select a.*,b.进出口同期当月,b.出口同期当月,b.进口同期当月,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=a.月度),0)as 进出口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=a.月度 and 进出口=0),0) as 出口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@nd and substring(日期,3,2)<=a.月度 and 进出口=1),0) as 进口当月累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=a.月度),0) as 进出口当月同期累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=a.月度 and 进出口=0),0) as 出口当月同期累计,
isnull((select sum(美元) from hgsj where substring(日期,1,2)=@oldnd and substring(日期,3,2)<=a.月度 and 进出口=1),0) as 进口当月同期累计

from (
select sum(美元) as 进出口当月,substring(日期,3,2) as 月度,
isnull(sum(case when 进出口=0 then 美元 else 0 end),0) as 出口当月,
isnull(sum(case when 进出口=1 then 美元 else 0 end),0) as 进口当月,
from hgsj
where substring(日期,1,2)=@nd
group by substring(日期,3,2)
) as a left join (
select sum(美元) as 进出口同期当月,substring(日期,3,2) as 月度,
isnull(sum(case when 进出口=0 then 美元 else 0 end),0) as 出口同期当月,
isnull(sum(case when 进出口=1 then 美元 else 0 end),0) as 进口同期当月,
from hgsj
where substring(日期,1,2)=@oldnd
group by substring(日期,3,2)
) as b
on a.月度=b.月度
vc6delphi6 2004-04-22
  • 打赏
  • 举报
回复
没有超过256个啊
就十多个啊
caiyunxia 2004-04-22
  • 打赏
  • 举报
回复
hgsj有多少个表连接
超过256?
vc6delphi6 2004-04-22
  • 打赏
  • 举报
回复
没人知道吗

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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