【问题标题】:SQL query works in PL/SQL but not in Visual StudioSQL 查询在 PL/SQL 中有效,但在 Visual Studio 中无效
【发布时间】:2015-08-22 00:34:20
【问题描述】:

我在网上搜索,发现很多人有同样的问题,但没有一个解决方案适合我。 我真的希望你能帮助我: 我有这个在 PL/SQL 中运行良好的 ORACLE SQL 查询:

select a.bzq_terminate_provider, a.callsnum, a.at_call_dur_sec, sum_charge 
From (select * from usage_cycle_sum 
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207'
and feature_code_rank='1') a, (select bzq_terminate_provider,sum(charge_amount) as sum_charge from usage_cycle_sum 
where ban='80072922' and ben='1'
and subscriber_no='036585305'
and start_cycle_code ='20150207' group by bzq_terminate_provider)  b
where  a.bzq_terminate_provider=b.bzq_terminate_provider

我也尝试了这个也可以正常工作的其他版本:

    select PROVIDER,sum(CALLS),sum(CHARGE),sum(DUR)
from (
select bzq_terminate_provider PROVIDER,callsnum CALLS,charge_amount CHARGE,at_call_dur_sec DUR
from   usage_cycle_sum 
where  ban='80072922' and ben='1'
  and  subscriber_no='036585305'
  and  start_cycle_code ='20150207'
  and  feature_code_rank='1'
union  
select bzq_terminate_provider PROVIDER,0 CALLS,charge_amount CHARGE,0 DUR
from   usage_cycle_sum 
where  ban='80072922' and ben='1'
  and  subscriber_no='036585305'
  and  start_cycle_code ='20150207'
  and  feature_code_rank='2'
  )
group by PROVIDER 

我的问题是,当我在 Visual Studio Web 应用程序中创建数据网格时,出现错误:语法错误:需要标识符或引用标识符

连接正常,我检查了简单的选择查询以及我附加的第二个查询中的整个联合部分,它们有效! 但是当我使用这两个版本时,我得到了这个错误。

可能是什么问题?还有其他方法可以解决这个问题吗? 谢谢。

编辑 21/06/2015 似乎 Visual Studio 不能很好地处理复杂的查询,我仍在寻找解决方案,因为我的下一个查询更复杂......

【问题讨论】:

  • 您可以尝试在子查询之后放置一个表别名。也许连接过程中的某些东西需要这个,因为许多数据库都这样做(并且可能 ODBC 有这个要求)。

标签: sql asp.net oracle visual-studio syntax-error


【解决方案1】:

您的第二个查询写起来要好得多:

select bzq_terminate_provider as PROVIDER, sum(callsnum) as CALLS,
       sum(charge_amount) as CHARGE, sum(at_call_dur_sec) as DUR
from usage_cycle_sum 
where ban = '80072922' and ben = '1' and
      subscriber_no = '036585305' and
      start_cycle_code ='20150207' and
      feature_code_rank in ('1', '2')
group by bzq_terminate_provider ;

或者,select 可能需要:

select bzq_terminate_provider as PROVIDER,
       sum(case when feature = '1' then callsnum else 0 end) as CALLS,
       sum(charge_amount) as CHARGE,
       sum(case when feature = '1' then at_call_dur_sec else 0 end) as DUR

(第一个版本假设字段在第二个子查询中被清零,因为它们在数据中是 NULL,但这可能不是真的。)

但是,应用程序软件还不够聪明,无法识别这种笨拙编写的查询,因此这不是您面临的实际问题。如果查询在数据库中有效,但在应用程序中无效,则典型问题是:

  • 应用程序未连接到正确的数据库。
  • 应用程序没有数据库或表的权限。
  • 应用程序查询与数据库中运行的查询不同,通常是由于某些替换问题。
  • 未正确解释在应用程序中运行查询的结果。

【讨论】:

  • 谢谢你的作品!但不是每个查询都可以像这样简化,连接不是问题。是否有另一种方法可以使复杂查询在 VS 中工作?再次感谢!
  • 不幸的是,这个解决方案对我不起作用,因为我没有从查询中得到想要的结果,它必须是我写它的方式,因为它给了我 feature_code_rank=1 但是1 + 2的总和,它必须是这样的。很抱歉回复晚了,我不在。我仍在为如何在 ASP.NET gridview 中执行复杂查询而苦苦挣扎。还有其他方法吗?
  • 如果您只想要一个总和,则将总和更改为条件聚合,类似于其他列。您的示例查询从两个子查询中获取总和,这就是它写成这样的原因。
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 2013-01-24
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-03
  • 1970-01-01
相关资源
最近更新 更多