(Sqlserver)将在查询结果插入新表中的时候添加一个自动增长列么 SQL语句怎么写
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了(Sqlserver)将在查询结果插入新表中的时候添加一个自动增长列么 SQL语句怎么写相关的知识,希望对你有一定的参考价值。
如题
假设表有主键ID列,以下生成比原表多一个序号列并按ID号大小填充的新表。select 序号=(select count(1) from tablename a where a.id>=tablename.id),* from tablename 参考技术A select identity(int,1,1) as x ,* insert into newtbfrom oldtb 参考技术B 用ROW_NUMBER() over(order by 排序列 asc) 不就可以了?
Oracle 定时查询数据插入新表中(job+存储过程)
create table EGMAS_COUNT_DATA
(
TIMES date not null,
COUNT NUMBER(30) not null,
SYSTEM_NAME VARCHAR2(30) not null,
Operation_index VARCHAR2(30) not null
);
-- Add comments to the columns
comment on column EGMAS_COUNT_DATA.TIMES
is ‘日期‘;
comment on column EGMAS_COUNT_DATA.COUNT
is ‘统计量‘;
comment on column EGMAS_COUNT_DATA.SYSTEM_NAME
is ‘系统编码‘;
comment on column EGMAS_COUNT_DATA.Operation_index
is ‘业务运营指标名称‘;
存储过程:
create or replace procedure orderCount is
begin
insert into EGMAS_COUNT_DATA(DAY, COUNT,SYSTEM_NAME,Operation_index)
SELECT to_date(to_char(sysdate-1,‘yyyy-MM-dd‘),‘yyyy-MM-dd‘), (select count(1) from T_ORDERS WHERE ORDER_RESOURCE = ‘13‘
AND to_char(CREATED_TM,‘yyyy-MM-dd‘) = to_char(sysdate-1,‘yyyy-MM-dd‘)), ‘EGMAS‘,‘Android手机订单量(港澳台)‘
FROM dual;
insert into EGMAS_COUNT_DATA(DAY, COUNT,SYSTEM_NAME,Operation_index)
SELECT to_date(to_char(sysdate-1,‘yyyy-MM-dd‘),‘yyyy-MM-dd‘), (select count(1) from T_ORDERS WHERE ORDER_RESOURCE = ‘14‘
AND to_char(CREATED_TM,‘yyyy-MM-dd‘) = to_char(sysdate-1,‘yyyy-MM-dd‘)), ‘EGMAS‘,‘IPHONE手机订单量(港澳台)‘
FROM dual;
insert into EGMAS_COUNT_DATA(DAY, COUNT,SYSTEM_NAME,Operation_index)
SELECT to_date(to_char(sysdate-1,‘yyyy-MM-dd‘),‘yyyy-MM-dd‘), (select count(1) from T_ORDERS WHERE
to_char(CREATED_TM,‘yyyy-MM-dd‘) = to_char(sysdate-1,‘yyyy-MM-dd‘)), ‘EGMAS‘,‘速运通订单量(港澳台)‘
FROM dual;
commit;
end orderCount;
/
设置job执行时间:
variable jobno number;
begin
dbms_job.submit(:jobno,‘orderCount;‘, sysdate, ‘TRUNC(sysdate+1)+1/24‘);
end;
/
启动job:
begin
dbms_job.run(44);
commit;
end;
/
查看job是否存在:
select job, next_date, next_sec, failures, broken from user_jobs;
以上是关于(Sqlserver)将在查询结果插入新表中的时候添加一个自动增长列么 SQL语句怎么写的主要内容,如果未能解决你的问题,请参考以下文章