Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/oracle/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
oracle10g中的动态SQL查询_Oracle_Oracle10g_Oracle Sqldeveloper - Fatal编程技术网

oracle10g中的动态SQL查询

oracle10g中的动态SQL查询,oracle,oracle10g,oracle-sqldeveloper,Oracle,Oracle10g,Oracle Sqldeveloper,下午好, 我不熟悉Oracle和SQL,但我有一个查询,其中包含我创建的以下代码。我目前正在使用Oracle 10g。我只是想知道是否有人可以帮助我使这个代码动态,而不是硬编码 我的代码只是查看一个记录用户活动的表。然后,我基本上计算每个用户/PC的记录数,并将其显示在透视样式表中 这并不是一个非常困难的查询,但我可能需要输入大约30台PC机,而这种硬编码方法显然不是完成这项任务的最佳方式 我一直在互联网上搜索一个基于主机名或用户ID可以使用的动态语句,但我没有找到任何简单地通过我的数据循环然后

下午好,

我不熟悉Oracle和SQL,但我有一个查询,其中包含我创建的以下代码。我目前正在使用Oracle 10g。我只是想知道是否有人可以帮助我使这个代码动态,而不是硬编码

我的代码只是查看一个记录用户活动的表。然后,我基本上计算每个用户/PC的记录数,并将其显示在透视样式表中

这并不是一个非常困难的查询,但我可能需要输入大约30台PC机,而这种硬编码方法显然不是完成这项任务的最佳方式

我一直在互联网上搜索一个基于主机名或用户ID可以使用的动态语句,但我没有找到任何简单地通过我的数据循环然后生成这个piviot样式视图的东西

我一直在看《光标》,但我想我离目标还很远

蚂蚁的帮助是预先通知的

问候 贝蒂


当您询问oracle问题时,请务必注意该版本。在您的情况下-如果您有11g,您可以查看
pivot
功能

在10G(和11g)中,您可以尝试类似的功能

create or replace function get_pivot() 
return sys_refcursor
as
    stmt varchar2(32000);
    c sys_Refcursor;
    cursor c_values as 
        select distinct host from table_name;
begin
    stmt := 'select user_id , ';

    for x in c_values loop
        stmt := stmt || ' sum(case when host = '''||x.host||''' then 1 else 0 end) as ' ||host|| ',';
    end loop;

    stmt := stmt || ' count(host) as grand_total from table_name group by user_id';

    open c for stmt;
    return(c);
end get_pivot;
无论您使用的是pivot还是DynamicSQL,都必须查询不同的值

我还没有测试过它——我现在没有甲骨文

create or replace function get_pivot() 
return sys_refcursor
as
    stmt varchar2(32000);
    c sys_Refcursor;
    cursor c_values as 
        select distinct host from table_name;
begin
    stmt := 'select user_id , ';

    for x in c_values loop
        stmt := stmt || ' sum(case when host = '''||x.host||''' then 1 else 0 end) as ' ||host|| ',';
    end loop;

    stmt := stmt || ' count(host) as grand_total from table_name group by user_id';

    open c for stmt;
    return(c);
end get_pivot;