当前位置:首页 » 编程语言 » sql把结果保存到新表
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sql把结果保存到新表

发布时间: 2023-02-05 05:50:00

‘壹’ 怎么把下面的的sql语句查询出来的结果插入到一张新表中去 求大神帮忙

@SQL这个是你生成的sql语句,你在你的sql中增加一个into table,这样能不能满足你的需求呢?

关于select into语句我就不多说了,给你参考链接,你看看是不是你要的

http://www.w3school.com.cn/sql/sql_select_into.asp

还有就是,你也可以将insert 加到你的@SQL语句的前面直接执行,是不是也行呢?

比如:

现有的@SQL = 'SELECT NAME FROM TABLE UNION ALL SELECT ''ZHANGSNA'' '

你修改成@SQL2 = 'INSERT INTO TABLE2(NAME) ' + @SQL,这样是不是也行?

我们既然写存储过程了,而且目的也只是唯一的,那么我们就可以考虑直接将所有步骤放在存储过程中来处理,没必要再拿出来单独考虑怎么用

另外给你一个方式,你看看用得上用不上

--表

create table test

(

name varchar(50)

)

go


--动态sql添加数据

insert into test

exec('select 1')

go


--存储过程

create proc protest

as

declare @sql nvarchar(100) = ''

declare @s int = 1

while(@s < 5)

begin

select @sql += 'select ' + cast(@s as varchar(20)) + ' union all '

set @s += 1

end

select @sql += 'select 999'

exec(@sql)

go


--存储过程添加数据

insert into test

exec protest

‘贰’ sql 如何把查询得到的结果如何放入一个新表中

表已经存在;
insert into 表名 (列名1.。。 列名n) select 列名1.。。。列名n from 表 where 条件
表不存在.
oracle
create table 新表明 as select 列名1.。。。列名n from 表 where 条件
sqlserver
select 列名1.。。。列名n
into 新表名
from 表 where 条件

‘叁’ sql将查询结果保存为新表

这段语句里怎么还有个 反斜线? jyje/atmjybs.jybs as
而且没发现 where ...

创建表的时候应该判断这个表是否已经 存在,即使你创建的 是临时表,

‘肆’ SQL中 已经在2张表中查询出了想要的结果 如何将这个结果保存在一个新的表中 如图 请提供SQL语句 谢谢!

如果“新的表”已经存在了:
INSERT INTO [新的表](name,cname, score)
SELECT name,cname,score FROM....INNER JOIN .... INNER JOIN ....

如果新的表不存在:
SELECT name, cname, score INTO [新的表] FROM....INNER JOIN .... INNER JOIN ....

‘伍’ SQL Server数据库中如何将查询的结果保存到另一个表中

将查询结果放在另一张表中(table2表必须存在)insert into table2 select * from table1
查询结果直接创建一个新表存放select * into table2 FROM table1

‘陆’ sql 计算查询的结果怎么保存到新表里

update 表2 set d=x.new
where exists(select * from
(select 表一.a,表一.b,表2.c,表2.d,表一.a+表2.c as new from 表一 ,表2) x
where 表2.c=x.c)

如果有其他条件追加在where 表2.c=x.c后面,如:
where 表2.c=x.c and x.a>3 and x.b<10

‘柒’ sql server将查询结果放入新表

如果只是套公式
select *into D from (
select * from A
union all
select * from B
union all
select * from C
order by ID,TM
)

‘捌’ SQL如何将查询的结果用一张新起表中存储起来

语句:如果新表未创建,用:
select * into 新表 from 表

如果新表已经存在
insert into 新表
(列a,列b,列c,...)
select 列a,列b,列c,... from 表

‘玖’ SQL Server数据库中如何将查询的结果保存到另一个表中

如果数据保存的对象表已经存在,
insert
into
结果表(字段1,
字段2)
select
字段1,
字段2
from
检索表
where
……
如果如果数据保存的对象表尚未建立,可以偷懒,直接
select
字段1,
字段2
into
结果表
from
检索表
where
……
系统会自动按字段1,字段2建立结果表,字段属性从检索表继承