一次性插入很多数据

好帅的一条鱼 2008-09-15 11:40:29
insert into 表(数值,日期) values()
我要插入一次性一个月30天的数据,这个日期字段怎么写呢?
(例如今天9月15号,需要插入1-15号,15条数据)
...全文
162 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
ojuju10 2008-09-15
  • 打赏
  • 举报
回复
用循环插入
letisgoto 2008-09-15
  • 打赏
  • 举报
回复
顶下。楼上的都很清楚了。。。
-晴天 2008-09-15
  • 打赏
  • 举报
回复
--插入指定日期及以前的该月的所有日期
declare @d datetime
set @d='2008-05-22'
create table 表(数值 int,日期 datetime)
select top 31 id=identity(int,1,1) into # from sysobjects
insert into 表(数值,日期) select 999,convert(datetime,convert(varchar(8),@d,120)+right('0'+convert(varchar(2),id),2)) from # where id<=datepart(d,@d)
select * from 表
go
drop table 表,#
/*
数值 日期
----------- -----------------------
999 2008-05-01 00:00:00.000
999 2008-05-02 00:00:00.000
999 2008-05-03 00:00:00.000
999 2008-05-04 00:00:00.000
999 2008-05-05 00:00:00.000
999 2008-05-06 00:00:00.000
999 2008-05-07 00:00:00.000
999 2008-05-08 00:00:00.000
999 2008-05-09 00:00:00.000
999 2008-05-10 00:00:00.000
999 2008-05-11 00:00:00.000
999 2008-05-12 00:00:00.000
999 2008-05-13 00:00:00.000
999 2008-05-14 00:00:00.000
999 2008-05-15 00:00:00.000
999 2008-05-16 00:00:00.000
999 2008-05-17 00:00:00.000
999 2008-05-18 00:00:00.000
999 2008-05-19 00:00:00.000
999 2008-05-20 00:00:00.000
999 2008-05-21 00:00:00.000
999 2008-05-22 00:00:00.000

(22 行受影响)
*/
-晴天 2008-09-15
  • 打赏
  • 举报
回复
--插入本月今天及以前的所有日期
create table 表(数值 int,日期 datetime)
select top 31 id=identity(int,1,1) into # from sysobjects
insert into 表(数值,日期) select 999,convert(datetime,convert(varchar(8),getdate(),120)+right('0'+convert(varchar(2),id),2)) from # where id<=datepart(d,getdate())
select * from 表
go
drop table 表,#
/*
数值 日期
----------- -----------------------
999 2008-09-01 00:00:00.000
999 2008-09-02 00:00:00.000
999 2008-09-03 00:00:00.000
999 2008-09-04 00:00:00.000
999 2008-09-05 00:00:00.000
999 2008-09-06 00:00:00.000
999 2008-09-07 00:00:00.000
999 2008-09-08 00:00:00.000
999 2008-09-09 00:00:00.000
999 2008-09-10 00:00:00.000
999 2008-09-11 00:00:00.000
999 2008-09-12 00:00:00.000
999 2008-09-13 00:00:00.000
999 2008-09-14 00:00:00.000
999 2008-09-15 00:00:00.000

(15 行受影响)
*/
hyqwan11112 2008-09-15
  • 打赏
  • 举报
回复

/*------------------------
create table 表(数值 int,日期 datetime)
select * from 表
declare @starttime datetime
declare @endtime datetime
set @starttime = '2008-09-01'
set @endtime = '2008-09-15'
while @starttime<=@endtime
begin
insert into 表
select 1,@starttime
set @starttime = dateadd(day,1,@starttime)
end
select * from 表
drop table 表
------------------------*/
数值 日期
----------- -----------------------

(0 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

(1 行受影响)

数值 日期
----------- -----------------------
1 2008-09-01 00:00:00.000
1 2008-09-02 00:00:00.000
1 2008-09-03 00:00:00.000
1 2008-09-04 00:00:00.000
1 2008-09-05 00:00:00.000
1 2008-09-06 00:00:00.000
1 2008-09-07 00:00:00.000
1 2008-09-08 00:00:00.000
1 2008-09-09 00:00:00.000
1 2008-09-10 00:00:00.000
1 2008-09-11 00:00:00.000
1 2008-09-12 00:00:00.000
1 2008-09-13 00:00:00.000
1 2008-09-14 00:00:00.000
1 2008-09-15 00:00:00.000

(15 行受影响)


想飞的狼 2008-09-15
  • 打赏
  • 举报
回复
楼上思路和我想的一样
hyqwan11112 2008-09-15
  • 打赏
  • 举报
回复

--不知道你的数值列怎么来,我都插入的1
create table 表(数值 int,日期 datetime)
select * from 表
declare @starttime datetime
declare @endtime datetime
set @starttime = '2008-09-01'
set @endtime = '2008-09-15'
while @starttime<=@endtime
begin
insert into 表
select 1,@starttime
set @starttime = dateadd(day,1,@starttime)
end
select * from 表
drop table 表
昵称被占用了 2008-09-15
  • 打赏
  • 举报
回复
例如今天9月15号,需要插入1-15号,15条数据:

DECLARE @SDate DATETIME
SET @SDate='2008-9-1'

INSERT INTO 表(数值,日期)
SELECT 0 as 数值,dateadd(day,a+10*b,@SDate) as 日期
FROM (
select 0 as a union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9
) as t1,(
select 0 as b union all
select 1 union all
select 2 union all
select 3 union all
select 4 union all
select 5 union all
select 6 union all
select 7 union all
select 8 union all
select 9
) as t2
WHERE a+10*b<15




这语句可以自己按日期范围需要扩展

-晴天 2008-09-15
  • 打赏
  • 举报
回复
日期好办:
select top 31 id=identity(int,1,1) into # from sysobjects
insert into 表(日期) select convert(varchar(8),getdate(),120)+right('0'+convert(varchar(2),id),2) from # where id<=15

但不知道你这些记录里的数值要填什么.
fengxieric 2008-09-15
  • 打赏
  • 举报
回复
受教了 受教了 不错
ChinaJiaBing 2008-09-15
  • 打赏
  • 举报
回复

insert into 表 values() from 表 where 日期 between... and...
好帅的一条鱼 2008-09-15
  • 打赏
  • 举报
回复
对问题在补充补充。。比如今天9月15号。
要求插入表 15条记录。。字段1一样,字段2分别是 2008年9月1日,2008年9月2日,。。。。。到2008年9月15日
这样15条记录。
fzcheng 2008-09-15
  • 打赏
  • 举报
回复

DECLARE @SetDate1 DATETIME,@SetDate2 DATETIME
SET @SetDate1='2008-9-1'
SET @setDATE2='2008-9-15'

INSERT INTO 表(数值,日期)
SELECT 数值,日期 FROM 表 WHERE 日期 BETWEEN @SetDate1 AND @SetDate2

问题很模糊哦.
chenping1984 2008-09-15
  • 打赏
  • 举报
回复

insert into 表(数值,日期) values(
select 数值字段,日期字段 from 数据表 where datediff(day,日期字段,getdate())=15
)

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧