如何在sql server中批量插入数据

mfeic 2009-03-30 08:48:21
譬如一张两个字段的表:第一个字段是编号,第二个是名称。现在假如希望插入1000条数据,编号为数字不能重复(例如1——1000),名称可以重复,例如从“张三,李四”选择插入。
请问应该怎样写sql语句?谢谢!
...全文
68 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
mfeic 2009-03-30
  • 打赏
  • 举报
回复
谢谢,解决了我的一个大问题。呵呵~
Teng_s2000 2009-03-30
  • 打赏
  • 举报
回复
只有insert了吧
jinjazz 2009-03-30
  • 打赏
  • 举报
回复
create table t(id int identity(1,1),name varchar(100))

declare @s varchar(1000)
set @s='张三,李四,jinjazz'


insert into t(name)
select replace(reverse((left(s,charindex(',',s)))),',','') as S from(
select r,reverse(left(@s,r))+',' as s
from(
select (select count(*) from sysobjects where name<=t.name ) as r
from sysobjects t
)a where r<=len(@s)
and left(@s+',',r+1) like '%,'
)t order by r

select * from t
drop table t

/*
(3 行受影响)
id name
----------- ----------------------------------------------------------------------------------------------------
1 张三
2 李四
3 jinjazz

(3 行受影响)
*/
htl258_Tony 2009-03-30
  • 打赏
  • 举报
回复
做一自增列字段,俗称种子字段:
if not object_id('tb1') is null
drop table tb1
Go
Create table tb1([id] int identity,[numb] int)
Insert tb1
select 20 union all
select 30 union all
select 50

select * from tb1
/*
id numb
----------- -----------
1 20
2 30
3 50

(3 行受影响)
*/
drop table tb1
rucypli 2009-03-30
  • 打赏
  • 举报
回复
create table test(a int identity(1,1),name varchar(20))

insert into test(name) values('张三')

34,590

社区成员

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

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