當前位置:首頁 » 編程語言 » 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語句
}