怎么把表中的某些字段的数据添加到临时表??

怎么把表中的某些字段的数据添加到临时表??,第1张

实体表说明

测试环境只有一张学生成绩表:student_scores,表中没有数据。创建表的sql如下:

create table student_scores

(

id          varchar(20),

name        nvarchar(50),

chinese     decimal(4,1),

math        decimal(4,1),

english     decimal(4,1),

PRIMARY KEY (id)

)

go

创建临时表

执行如下Sql,创建一张和student_scores结构相同的临时表#temp。

select * into #temp from student_scores

go

select * from #temp

go

更新临时表

执行如下的Sql,插入5笔数据到临时表。

insert into #temp VALUES

('70601', N'沙龙逸', 123, 148, 137),

('70602', N'刘帅', 116, 143, 140),

('70603', N'王雪', 131, 135, 144),

('70604', N'韩雨萌', 129, 133, 138),

('70605', N'杨璐', 131, 143, 144)

go

将临时表的数据插入到正式表

将一张表的数据批量插入到另外一张表,需要用到insert into select语法,可以百度关键字:sql insert into select,了解更多关于insert into select语法的帮助信息。。执行如下的sql,将临时表的数据插入到正式表。

insert into student_scores select * from #temp

go

select * from student_scores

go

更新临时表

执行如下的sql,将临时表中的语文成绩小于125分的同学加2分。

update #temp set chinese=chinese+2 where chinese<125

go

select * from #temp

go

将临时表的数据更新到正式表

将一张表的数据批理更新另外一张表,需要用到update from语法,可以百度关键字:sql update from,了解更多关于update from语法的帮助信息。执行如下的sql,将临时表的数据插入到正式表。

update student_scores

set chinese=b.chinese

from #temp b

where student_scores.id=b.id

and student_scores.chinese<>b.chinese

go

select * from student_scores

go

比如临时表叫temp,你要查询的语句为select * from 表名 where id=1。 如果temp表存在: insert into temp select * from 表名 where id=1commit如果temp表不存在 create table temp as insert into temp select * from 表名 where id=1注意...


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11812057.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存