SQL数据插入

T-SQL中提供了多个将数据插入到表中的语句:insert values、insert select、insert exec、select into和Bulk insert;

1、  insert values

这是一种比较常用的插入数据方式,可以进行单条或者多条数据插入

首先创建一个表,然后进行数据插入操作

create table test(

id int not null identity(1,1) primary key,

name nvarchar(100)

)

单条数据插入

insert into test(name) values(‘test1‘)

多行数据插入

insert into test(name) values(‘test2‘),(‘test3‘),(‘test4‘)

以上两种方式都可以省略表名后面的字段(不推荐这种方式),如下:

insert into test values(‘test1‘)

insert into test values(‘test2‘),(‘test3‘),(‘test4‘)

多行数据插入被作为一个原子性操作处理,即输入到表中的数据如果任意一行失败,则该语句不会插入到表中。

对于增强values子句,还可以将其作为表值构造函数以标准方式构建一个派生表,如下:

select * from (values

(‘001‘,‘test1‘),

(‘002‘,‘test2‘),

(‘003‘,‘test3‘)

)as test(id,name)

2、  insert select

insert select 语句和insert values语句类似,不同的是values子句不同,在这可以指定一个select查询,例如将刚才的数据复制到另外一个表test1中,首先创建test1表,结构与test相同

create table test1(

id int not null primary key,

name nvarchar(100)

)

执行插入语句

insert into test1

select ID,name from test

插入test1表结果如下,完全将test表中数据复制到test1中

删除test1中数据

truncate table test1

该数据插入方式中还可以使用union all进行多行数据插入

insert into test1

select ‘1‘,‘test1‘ union all

select ‘2‘,‘test2‘ union all

select ‘‘,‘test3‘

查询test1表结果如下:

3、  insert exec

使用insert exec语句可以将存储过程或者动态SQL批处理的结果集插入到目标表中。

创建测试存储过程

create proc proc_Test @name as nvarchar(100)

as

select * from test where name like @name+‘%‘

go

测试执行结果

exec proc_Test ‘test‘

删除test1数据

truncate table test1

执行插入语句

insert into test1(id,name)

exec proc_test ‘test‘

查询test1表中数据

4、  select into

select into语句是一个非标准的T-SQL语句,它使用查询的结果创建并填充目标表。

使用下列语句可以将test中数据复制到temp的临时表中,temp表的结构和数据是基于test表的,该语句会将(列名、类型、是否为空、identity)属性和数据复制到目标表中,但是索引、约束、触发器和权限不会复制,如果目标表中需要这些需要重新创建。

select ID,name

into #temp

from test

查询#temp表,数据如下:

5、bulk insert

使用bulk insert语句将来自文件的数据插入到现有的一个表中。在语句中指定表、源文件和选项。可以指定多个选项,包括数据类型、字段终止符、行终止符和其他的文件选项

修改test表结构:

alter table test add address nvarchar(100) ,age int

清空test表

truncate table test

编辑本地文件,将下列蓝色字体数据保存在txt文件中,在SQL语句中指定该文件地址即可。

1,tom,北京市海淀区,20

2,sam,北京市朝阳区,23

3,amy,上海市浦东区,27

4,张三,河北省唐山市,28

5,李四,北京市海淀区,31

执行语句

bulk insert dbo.test from ‘C:\sqltest.txt‘

with(

datafiletype=‘char‘,

fieldterminator=‘,‘,

rowterminator =‘\n‘

)

查询结果如下

时间: 2024-12-15 12:17:15

SQL数据插入的相关文章

SQL 数据插入、删除 大数据

--测试表 CREATE TABLE [dbo].[Employee] ( [EmployeeNo] INT PRIMARY KEY, [EmployeeName] [nvarchar](50) NULL, [CreateUser] [nvarchar](50) NULL, [CreateDatetime] [datetime] NULL );--1.循环插入 SET STATISTICS TIME ON; DECLARE @Index INT = 1; DECLARE @Timer DATET

SQL数据插入字符串时转义函数

函数一: 1 std::string CheckString(std::string& strSource) 2 { 3 std::string strOldValue = "'"; 4 std::string strNewValue = "''"; 5 for(std::string::size_type pos(0); pos != std::string::npos; pos += strNewValue.length()) 6 { 7 if ((po

ASP.NET MVC与Sql Server交互,把字典数据插入数据库

在"ASP.NET MVC与Sql Server交互, 插入数据"中,在Controller中拼接sql语句.比如: _db.InsertData("insert into Product(Name,quantity,Price) values('"+productVm.Name+"','"+productVm.Quantity+"','"+productVm.Price+"')"); 在某些场景中需要把数

SQL Server插入中文数据后出现乱码

原文:SQL Server插入中文数据后出现乱码 今天在做项目的过程中遇到如标题的问题,情况如下图: 数据库使用的是SQL Server2012版本,创建表的脚本如下: CREATE TABLE [dbo].[Type](  [TypeId] INT NOT NULL PRIMARY KEY,     [TypeName] NVARCHAR(50)NOT NULL,     [Description] NVARCHAR(255) NULL,     [CategoryId] INT NOT NU

SQL Server插入数据和删除数据基础语句使用

首先在我的Student表中插入几条数据,由于我的表已经创建完成了,所以就没有创建表的 sql 语句了,不过可以看我的上一篇文章: http://www.cnblogs.com/Brambling/p/6649350.html 插入数据sql语句: 1 insert into Student(S_StuNo,S_Name,S_Sex,S_Height) 2 select '001','项羽','男','190' union 3 select '002','刘邦','男','170' union

sql - 批量插入数据

直接构造 insert into t (c1, c2, c3) values (1, 1, 1), (2, 2, 2) 使用UNION insert into t (c1, c2, c3) (select a1, a2, a3) union all (select b1, b2, b3) 利用查询结果 insert into t (c1, c2, c3) (select a1, a2, a3 from t2) sql - 批量插入数据,布布扣,bubuko.com

SQL从入门到基础–03 SQLServer基础1(主键选择、数据插入、数据更新)

一.SQL语句入门 1. SQL语句是和DBMS"交谈"专用的语句,不同DBMS都认SQL语法. 2. SQL语句中字符串用单引号. 3. SQL语句中,对于SQL关键字大小写不敏感,对于字符串值大小写敏感. 4. 创建表.删除表不仅可以手工完成,还可以执行SQL语句完成,在自动化部署.数据导入中用的很多,Create Table T_Person(Id int not NULL,Name nvarchar(50),Age int NULL).Drop Table T_Person1

sql将一个表中的数据插入到另一个表中

列名不一定要相同,只要你在HH中列出要插入列的列表跟select   from   mm表中的选择的列的列表一一对应就可以了,当然两边的数据类型应该是兼容的. 比如: insert   into   hh   (fielda,fieldb,fieldc)   select   fieldx,fieldy,fieldz   from   mm ---更新计量点中不存在的数据,将台帐中的信息转移到计量点中 insert into MetricPoints (MeterID,MetricPointNa

SQL一次性插入大量数据

在SQL Server 中插入一条数据使用Insert语句,但是如果想要批量插入一堆数据的话,循环使用Insert不仅效率低,而且会导致SQL一系统性能问题.下面介绍SQL Server支持的两种批量数据插入方法:Bulk和表值参数(Table-Valued Parameters). 运行下面的脚本,建立测试数据库和表值参数. [c-sharp] view plain copy --Create DataBase create database BulkTestDB; go use BulkTe