sql中一次性插入多行数据的方法汇总
2009-02-14 13:45
393 查看
下面均以数据库testdb和表testtb为例
use master
if exists (select * from sysdatabases where name='testdb')
drop database testdb
go
create database testdb
go
use testdb
if exists (select * from sysobjects where name='testtb')
drop table testtb
go
create table testtb(tid int,tvalue int)
go
I.下面是用sql编程一次性插入多条语句的完整实例:
declare @a int, @b int
set @a=1
set @b=20
while @a<=20
begin
insert into testtb(tid,tvalue)values(@a,@b)
set @a=@a+1
set @b=@b-1
end
select * from testtb
go
II.使用insert into 表名(字段列表) select 值列表 union all select 值列表的语句格式:
insert into testtb(tid,tvalue)
select 94,100 Union all
select 72,12
go
III.
1》.从存在的表复制数据到新表(与旧表表结构相同的表):
create table newtb(nid int,nvalue int ) go
INSERT INTO newtb (nid, nvalue) SELECT tid,tvalue FROM testtb
2》.从存在的表复制数据到新表(新表不存在):
use master
if exists (select * from sysdatabases where name='testdb')
drop database testdb
go
create database testdb
go
use testdb
if exists (select * from sysobjects where name='testtb')
drop table testtb
go
create table testtb(tid int,tvalue int)
go
I.下面是用sql编程一次性插入多条语句的完整实例:
declare @a int, @b int
set @a=1
set @b=20
while @a<=20
begin
insert into testtb(tid,tvalue)values(@a,@b)
set @a=@a+1
set @b=@b-1
end
select * from testtb
go
II.使用insert into 表名(字段列表) select 值列表 union all select 值列表的语句格式:
insert into testtb(tid,tvalue)
select 94,100 Union all
select 72,12
go
III.
1》.从存在的表复制数据到新表(与旧表表结构相同的表):
create table newtb(nid int,nvalue int ) go
INSERT INTO newtb (nid, nvalue) SELECT tid,tvalue FROM testtb
2》.从存在的表复制数据到新表(新表不存在):
相关文章推荐
- sql中一次性插入多行数据的方法汇总收藏
- sql选择插入多行数据到同一表中的几种方法汇总
- mysql插入多行数据的方法
- T-SQL 语句插入中文数据时数据库显示全是问号“???”的解决方法(数据库编码规则)
- SQL一次性插入多条数据
- sql将一个表中的数据插入到另一个表中的方法
- SQL语句:orac 3ff0 le中如何插入Date类型的数据和根据Date数据进行查询的方法
- sql插入数据的方法以及效率
- SQL Server 2008 学习笔记【一】 一次性插入多行数据的问题
- sql下三种批量插入数据的方法
- SQL 2005批量插入数据的二种方法
- JDBC中sql插入数据得到主键的方法
- sql 2005 T_sql 动态行转列(数据汇总)方法
- sql insert into 一次性插入多条数据 从一张表中查询到的数据插入到另一张表
- 插入多行数据的方法
- 利用SqlBulkCopy一次性把Table数据插入到数据库
- SQL 向表中插入数据(单行/多行)
- 插入多行数据和类似 select union 方法
- SQL中插入带单引号的数据的方法