当前位置:首页 » 编程语言 » sql怎么保留1万个记录
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql怎么保留1万个记录

发布时间: 2022-09-14 10:33:54

sql数据有几百万条记录,如何应用查询语句查询出1-10000、10000-20000、以此类推。

create table t5 ( item varchar(20))

insert t5 select '1-10000'
union all select '10000-20000'
union all select '20000-30000'
union all select '40000-50000'

create table t6 (id int identity(1,1),item varchar(20))
insert into t6
select * from t5

create table #t6 (id int,item varchar(20))

declare @i int
declare @c int

select @i= min(id) from t6
select @c=max(id) from t6

while (@i<@c)
begin
insert into #t6
select t1.id, t1.item from t6 t1,t6 t2 where substring(t2.item, 1, Charindex('-',t2.item)-1)=substring(t1.item, Charindex('-',t1.item)+1,len(t1.item)) and t2.id=@i

insert into #t6
select t1.id, t1.item from t6 t1,t6 t2 where substring(t1.item, 1, Charindex('-',t1.item)-1)=substring(t2.item, Charindex('-',t2.item)+1,len(t2.item)) and t1.id=@i

set @i=@i+1

end

select distinct * from #t6

Ⅱ 如何用一条Sql语句向表中插入10000条记录

CREATE TABLE dbo.Nums(n INT NOT NULL PRIMARY KEY);
DECLARE @max AS INT, @rc AS INT;
SET @max = 1000000;
SET @rc = 1;

INSERT INTO Nums VALUES(1);
WHILE @rc * 2 <= @max
BEGIN
INSERT INTO dbo.Nums SELECT n + @rc FROM dbo.Nums;
SET @rc = @rc * 2;
END

INSERT INTO dbo.Nums
SELECT n + @rc FROM dbo.Nums WHERE n + @rc <= @max;
GO

Ⅲ SQL2000想在表里插入一万条记录 每1000条为一组并创建新的组名,这样的语句怎么写

insert 表(id,部门) select id,
case
when id>=1 and id<=1000 then '部门1'
when id>1000 and id<=1000 then '部门2'
...
else then '部门N'
end from 表 where

Ⅳ 怎样一句sql搞定保留最新的10条记录

1、新增:insert
into
表名
values
(值1,值2,值3……);如果要为每列插入值的话,就这样写,如果只给特定的列插入值,则应该指定列名,如下
insert
into
表名 (列名1,列名2,列名3……) values
(值1,值2,值3……);
2、修改:update
学生表
set
地址='湖北宜昌'
where
年龄<20;
3、删除:delete
from
学生表
where
年龄>=20;
4、select
学生表.学生姓名,课程表.课程,成绩表.分数
from
学生表,课程表,成绩表
where
学生表.学生编号=成绩表.学生编号
and
课程表.课程编号=成绩表.课程编号
5、计算某一门总分:select
sum(成绩表.成绩)
from
成绩表,课程表
where
成绩表.课程编号=课程表.课程编号
and
课程表.课程名称='英语'——这样就查出了英语的总分

Ⅳ 您好,请问在有100万条数据的数据表中,随机取出1万条数据(不能重复)并存入一个新表中的SQL语句怎么写

insertinto新表select*from旧表whererownum<=10000orderbydbms_random.random;

不过,可能会有点慢。

Ⅵ sql语句查询上万条数据。

用NOT IN 是很慢的一种查询方式。
试试用Join。

select a.*, 'N' as chk from pay a
where a.txn_dt = '20090110'
and txn_sta_cd = '6'
union
select * from (
select b.*, isnull(c.txn_user_id,'N') as chk
from pay b left outer join pay c
on b.txn_user_id = c.txn_user_id
and b.txn_dt = c.txn_dt
and b.txn_sta_cd = '7'
and c.txn_sta_cd = '6'
where b.txn_dt = '20090110' ) d
where d.chk = 'N'
=========================================
如果不行, 试试下面的

select a.*, 'N' as chk
from pay a
where a.txn_dt = '20090110'
and a.txn_sta_cd = '6'
union
select * from (
select b.*, isnull(c.txn_user_id,'N') as chk
from
(select * from pay
where txn_dt='20090110'
and txn_sta_cd = '7') b left outer join
(select * from pay
where txn_dt='20090110'
and txn_sta_cd = '7') c
on b.txn_user_id = c.txn_user_id) d
where d.chk = 'N'

强烈要求补分~~~!!!!!!!

Ⅶ 用sql server 2000保存上千万条数据,包含年月字段,有什么办法以年月方法对表进行查分以提高速度

1、只需要做一个时间字段的索引即可。
2、SQL Server 2000 不会对表进行实际拆分,它没有这个功能。
3、MS SQL Server 2000 及其后的各版本,都没有对表进行实际拆分成多个表,没有将一个数据库拆分成多个数据库的功能。现在的最新版本的MS SQL 为2012版。
4、加快SQL Server搜索速度的方法很简单,加入必要的限制条件即可,在限制条件为确定的,条件为单方向增长的,搜索结果集(结果数据条不多)时,SQL Server会自动变快,能使用等于条件时,不要使用大于、小于条件,尽可能规避模糊通配条件。对常用可排序的条件进行索引可以有效加快系统搜索速度。
5、每日30万条以上数据,对SQL Server来说,压力很大!存储过程、客户端如果在设计过程中不经过特殊优化,对于普通企业的硬件设备来说,是比较难以承受的。
6、相关的知识请网络中按“sql server 千万条记录”去搜别人的说法。

此外,个人建议这样的数据库在搜索时,以分年方式建库,分月方式建表,在搜索时如跨年、跨月,需要复合搜索,设计起来很难。

Ⅷ sql如何最快地返回上千万的总记录数

第一种方法:select * from 表
select @@rowcount as '记录数' -----@@rowcount返回select * from 表 中数据行的数目.

第二种方法:1.在表中加一identity属性列,每增加一条记录,该列值自动加1

最后一条记录的identity属性列值就是总条数

2.最后查询表中最后一条记录的identity属性列的列值就可以知道总条数了.
语句为:
select top 1 identity属性列 from 表 order by identity属性列 desc

Ⅸ 怎样用一条sql语句向数据库中插入10000条记录

只能用循环了,以下是php语法
for($i=0;$i<10001;$i++){
INSERT INTO TABLE();//这里式sql语句
}