posts - 206,  comments - 26,  views - 17万
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
备份表数据:
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 条件;
 
posted on   努力--坚持  阅读(208)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)
点击右上角即可分享
微信分享提示