如何根据列名创建自动增量表?

发布于 2025-01-21 11:35:43 字数 857 浏览 0 评论 0原文

标题可能有点令人困惑,但我会尝试在这里更好地解释。

我要实现的是:

  1. 创建一个带有3列的表(到目前为止很好):

     创建表statisticcounter
    ((
        始终生成的统计数字始终为身份(以1000的增量启动1), 
        Statistikname varchar(255),
        计数器整数 
    );
     
  2. 创建一种调用方法(函数,过程,触发器或其他内容。我现在将其称为函数),将其称为Counter 1基于statistiknameStatisticId

这里的限制是:所述统计量是一个SQL脚本,该脚本通过座舱文件传播,其语法与常规SQL不同(in:out:of:select,with with with variables)。我想在此座舱文件中放置一个函数或某些内容,每次运行脚本时都会运行。它将自动增加使用统计量的次数。

我不知道我需要什么(功能,过程,触发器或其他任何内容),这就是为什么我写一些含糊的原因。

编辑:我尝试使用合并,但我总是得到不匹配的结果执行。没有施放它。

merge into StatisticCounter stc using 
CAST((select 1000 id from dual)AS INTEGER) val on (stc.statisticid=val.id) 
when matched then 
UPDATE StatisticCounter SET counter = counter + 1; 
when not matched then 
select * from dual;

The title might be a little confusing but I will try explaining here better.

What I want to achieve is:

  1. Create a table with 3 columns (so far so good):

    CREATE TABLE StatisticCounter
    (
        statisticID NUMBER GENERATED ALWAYS as IDENTITY (START with 1000 INCREMENT by 1), 
        statistikname varchar(255),
        counter integer 
    );
    
  2. Create a way to call (function, procedure, trigger or something. I will call it function for now) that will increment counter with 1 based on statistikname or statisticid.

The restriction here being: said statistic is an SQL script ran through a cockpit file with a little bit of a different syntax than regular sql (in: out: select, WHERE with variables). I want to put a function or something in this cockpit file, that will run each time the script is run. Which will auto increment the number of times the statistic has been used.

I have no idea what I need (function, procedure, trigger or anything else) and this is why I am writing it a bit vague.

EDIT: I tried with merge but I always get the WHEN NOT MATCHED result executed. Without CAST its the same.

merge into StatisticCounter stc using 
CAST((select 1000 id from dual)AS INTEGER) val on (stc.statisticid=val.id) 
when matched then 
UPDATE StatisticCounter SET counter = counter + 1; 
when not matched then 
select * from dual;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明月夜 2025-01-28 11:35:43

经过许多功能和过程后,我自己找到了答案。

  1. 创建表;
  2. 创建功能;
  3. 在文件中调用功能。

这里重要的是,执行SQL的外部_cockpit文件会贯穿每行的所有OUT参数,结果统计返回。这使柜台变得疯狂 - 无法抓住统计范围或开始。因此,我做了第二个_cockpit,只有1个结果,然后将其附加到我要计算的统计数据上。

创建表
statistikname varchar2(256 char),计数器整数);

创建或替换函数udx_func_statistiscounter(datainput in
varchar2)返回varchar2是pragma automous_transaction;
开始
更新udx_table_statistiscounter set counter counter = counter +1其中statistisid = datainput;
犯罪;
返回“完成”;结束udx_func_statisticcounter;

I found an answer on my own after going through a lot of functions and procedures.

  1. Create table;
  2. Create function;
  3. Call function in file.

The important thing here is that the outside _cockpit file that executes the SQL goes through all OUT parameters for each line the statistic returns as result. This makes the counter go wild - no way to catch END of statistic or beginning. So I made a second _cockpit with only 1 result and attach it to the statistic I want to count.

CREATE TABLE UDX_Table_StatisticCounter(statisticID INTEGER,
StatistikName varchar2(256 char), counter integer);

CREATE OR REPLACE FUNCTION UDX_FUNC_StatisticCounter(datainput IN
VARCHAR2) RETURN VARCHAR2 IS PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
UPDATE UDX_Table_StatisticCounter SET counter = counter +1 WHERE statisticID=datainput;
COMMIT;
RETURN 'done'; END UDX_FUNC_StatisticCounter;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文