INSERT INTO使用SELECT和值,并插入多行 [英] INSERT INTO using SELECT and values, and inserting multiple rows

查看:1170
本文介绍了INSERT INTO使用SELECT和值,并插入多行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,大家好!


基本上,我需要根据一些标准从表B中插入*多行*到表A中,我需要插入一些静态值和表A中的每一行。


例如:


插入tableA(col1,col2,col3,col4,col5)



(''奶酪'',

''蓝'',

''John Wayne'',

从tableB中选择favorite_movie

其中movietype =''科幻''

和(moviedate = 1965或1966)

和B& W!= 1,

''Paris'')


我已经尝试了几乎所有:声明变量,使用不同的单词(MSSQL不喜欢插入到,只是插入),选择值列表的内部和外部,不使用单词VALUES等。


有什么想法吗?目的是根据一组标准将多行从tableB复制到A.

谢谢!

解决方案


嘿,大家好!


基本上,我需要根据一些标准从表B中插入*多行*到表A中,我需要插入一些静态值以及表A中的每一行。


例如:


插入tableA(col1,col2,col3, col4,col5)



(''奶酪'',

''蓝'',

''John Wayne'',

从tableB中选择favorite_movie

其中movietype =''SciFi''

和(moviedate = 1965或1966) )

和B& W!= 1,

''Paris'')


我试过了一切:声明变量,使用不同的单词(MSSQL不喜欢插入到,只是插入),内部和外部的SELECT值列表中的e,不使用值VALUES等。


有什么想法吗?目的是根据一组标准将多行从tableB复制到A.


谢谢!



你必须使用两个插入Qry

1.对于多个表行

2.用户定义值


嘿,大家!


基本上,我需要插入*多行*基于某些标准,从表B到表A,我需要插入一些静态值以及表A中的每一行。


例如:


插入tableA(col1,col2,col3,col4,col5)



(''奶酪'',

''Blue'',

''John Wayne'',

从tableB中选择favorite_movie

其中movietype =''SciFi''

和(moviedate = 1965或1966)

和B& W!= 1,

''Paris'')


我已经尝试了几乎所有:声明变量,使用不同的单词(MSSQL不喜欢插入到,只是插入),里面的SELECT在值列表之外,不使用VALUES等字。


有什么想法吗?目的是根据一组标准将多行从tableB复制到A.


谢谢!




您可以先使用相同的select查询插入col4的值,该查询可能会在tableA中插入多个多行。稍后您可以使用查询中提到的静态值更新其他列。


查询将如下所示:

插入表格A

(col4)

选择

favorite_movie
来自tableB的


其中movietype =''SciFi''

和(moviedate = 1965或1966)

和B& W!= 1


插入行后你可以更新这样的其他列。


更新表A

set col1 =''奶酪'',

col2 =''蓝色'',''

col3 =''John Wayne'',

col5 =''巴黎''
来自AA表的
,表BB

其中

A.favorite_movie = B.favorite_movie

和B.movi​​etype =''SciFi''

和B.movi​​edate in(1965,1966)

和B.B& W!= 1


谢谢!


我可以从另一个临时表中提取用户定义的值吗?那么它们会被视为用户定义的值,然后我可以使用一个插入吗?


Hey, everyone!

Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A.

For example:

insert into tableA (col1,col2,col3,col4,col5)
values
(''Cheese'',
''Blue'',
''John Wayne'',
select favorite_movie from tableB
where movietype = ''SciFi''
and (moviedate=1965 or 1966)
and B&W !=1,
''Paris'')

I''ve tried just about everything: declared a variable, used different words (MSSQL doesn''t like "insert into", just "insert"), SELECTs inside and outside of the values list, not using the word VALUES, etc.

Any ideas? The intent is to copy multiple rows from tableB to A based a set of criteria.

Thanks!

解决方案

Hey, everyone!

Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A.

For example:

insert into tableA (col1,col2,col3,col4,col5)
values
(''Cheese'',
''Blue'',
''John Wayne'',
select favorite_movie from tableB
where movietype = ''SciFi''
and (moviedate=1965 or 1966)
and B&W !=1,
''Paris'')

I''ve tried just about everything: declared a variable, used different words (MSSQL doesn''t like "insert into", just "insert"), SELECTs inside and outside of the values list, not using the word VALUES, etc.

Any ideas? The intent is to copy multiple rows from tableB to A based a set of criteria.

Thanks!


You have to Use Two Insert Qry
1. For Multiple Table Rows
2. for User Define Values


Hey, everyone!

Basically, I need to insert *multiple rows* into table A from table B based upon some criteria, and I need to insert some static values along with each row from table A.

For example:

insert into tableA (col1,col2,col3,col4,col5)
values
(''Cheese'',
''Blue'',
''John Wayne'',
select favorite_movie from tableB
where movietype = ''SciFi''
and (moviedate=1965 or 1966)
and B&W !=1,
''Paris'')

I''ve tried just about everything: declared a variable, used different words (MSSQL doesn''t like "insert into", just "insert"), SELECTs inside and outside of the values list, not using the word VALUES, etc.

Any ideas? The intent is to copy multiple rows from tableB to A based a set of criteria.

Thanks!



You can first insert values for the col4 using the same select query which may insert multiple multiple rows into tableA. Later you can update other columns with the static values mentioned in your query.

The query will be like this:
insert into tableA
(col4)
select
favorite_movie
from tableB
where movietype = ''SciFi''
and (moviedate=1965 or 1966)
and B&W !=1

Once the rows are inserted you can update other columns like this.

update tableA
set col1=''Cheese'',
col2=''Blue'',''
col3=''John Wayne'',
col5=''Paris''
from table A A,table B B
where
A.favorite_movie=B.favorite_movie
and B.movietype = ''SciFi''
and B.moviedate in (1965,1966)
and B.B&W !=1

Thanks!


Could I pull the user defined values from another temp table? Would they then be considered user defined values, and then could I use one insert?


这篇关于INSERT INTO使用SELECT和值,并插入多行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆