日期:2014-05-18  浏览次数:20477 次

求更新数据库
我想把查询运算后的数据更新到一个新的数据表中。把以前的表删了。重新建一个表,再把数据填加进去。请高手给我看看。
SQL code

selecet a+b as [列1],c/d as[列2] from 表1



------解决方案--------------------
select a+b as [列1],c/d as[列2] into T from 表1

------解决方案--------------------
跨表联查更新即可,不用频繁的删除表,创建表。

示例:
SQL code
create table m_name  (id int,name varchar(4))
insert into m_name
select 1,'张三' union all
select 2,'李四' union all
select 3,'王五'

select * from m_name
/*
id          name
----------- ----
1           张三
2           李四
3           王五
*/

create table m_chengji  (name varchar(4),kemu int,chengji int,id sql_variant)
insert into m_chengji
select '张三',1,95,null union all
select '张三',2,92,null union all
select '张三',3,91,null union all
select '李四',1,56,null union all
select '李四',2,76,null union all
select '李四',3,99,null union all
select '王五',1,57,null union all
select '王五',2,100,null union all
select '王五',3,67,null

select * from m_chengji
/*
name kemu        chengji     id
---- ----------- ----------- -----------
张三   1           95          NULL
张三   2           92          NULL
张三   3           91          NULL
李四   1           56          NULL
李四   2           76          NULL
李四   3           99          NULL
王五   1           57          NULL
王五   2           100         NULL
王五   3           67          NULL
*/

update m_chengji 
set id = a.id from m_chengji b left join m_name a on a.[name]=b.[name]