备份表数据:
select * into 新表名 from 源表名
-> 可以加where条件
1 select 2 * 3 into 4 newStudent 5 from 6 dbo.Student 7 where 8 stuSex = 'f' 9 and 10 YEAR(stuBirthdate) = 1990;
-> 备份表结构
select * into 新表 from 源表 where 1>2
-> 备份表时只会备份表结构,对于约束不会备份
插入多条数据:
从源表中某特定数据加到一新表中
insert into 目标表名 select 字段列表 from 源表名
如:insert into backupStudent select * from students
插入一个结果集;
1 insert into Test(id, name, age) 2 select 6, '张三', 18 3 union 4 select 7, '李四', 20;
插入多条数据;
1-- SQL Server 2008中插入数据可以使用
2 insert into Tbl(id, name, age) 3 values 4 (1, '张三' , 12), 5 (2, '李四' , 18), 6 (3, '王五' , 13), 7 (4, '张三' , 12), 8 (5, '王五' , 13)
批量插入记录时,对有标识列的字段要设置 set IDENTITY_INSERT 表名 on,然后再执行插入记录操作;插入完毕后恢复为 off 设置
1 SET IDENTITY_INSERT newStudent ON; 2 insert into dbo.newStudent 3 (stuId, stuName, stuSex, stuBirthdate, stuStudydate, stuAddress, stuEmail, stuPhone, stuIsDel, stuInputtime, classId) 4 select * from Student 5 where stuId <50; 6 go 7 set IDENTITY_INSERT newStudent off
--把现有表的数据备份,以供瞎搞
(表不能存在)
--select * into newStudent from student(newStudent表在select查询的同时自动建立。)
•--把现有表的数据复制到另一个一个已存在的表
•通过这种方式复制,只能复制表中的数据,以及列的名字和数据类型,对于约束,不会复制过来。
--insert into backupStudent select * from students(backupStudent表必须提前建好)
增删改查:
-> 增加数据
set nocount on; 不显示几条数据受影响
insert into 表名(列名1, 列名2, ...) values(值1, 值2, ...);
insert into tblPerson(id, Name, Sex, Age)
values(1, N'王老五', 'true', 30);
-- 插入数据时bit类型比较特殊,如果使用true或false需要加单引号
-- 或者使用0和1,0表示false,1表示true
--N'王老五', n不区分大小写 , nvarchar 前面+n
->删除数据
delete from tblPerson where stuName='王老五';
->查询数据
select 字段列表 from 表名;
-> 更改数据
update 表名 set 字段名=值, 字段名=值, ... where 条件;
浙公网安备 33010602011771号