473,441 Members | 1,399 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,441 software developers and data experts.

INSERT INTO using SELECT and values, and inserting multiple rows

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!
Jan 9 '07 #1
20 188203
hariharanmca
1,977 1GB
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
Jan 9 '07 #2
Sangs
5
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!
Jan 9 '07 #3
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?
Jan 9 '07 #4
Sangs
5
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?

No need to use temp table. Since these are static values, u can assign them directly to the columns satisfying the conditions.

Regards!
Jan 10 '07 #5
All: I ended up using two queries to perform this operation. It increased the number of steps required, but worked and was consistent.
Jan 22 '07 #6
iburyak
1,017 Expert 512MB
Try this:

[PHP]
insert into tableA (col1,col2,col3,col4,col5)
select 'Cheese',
'Blue',
'John Wayne',
favorite_movie,
'Paris'
from tableB
where movietype = 'SciFi'
and (moviedate=1965 or 1966)
and B&W !=1[/PHP]
Jan 22 '07 #7
where are you using your sql for oracle or mssql/postgre/mysql?
if mssql

--
insert into urtable
values
('val1','val2'),
('val1','val2');
Mar 25 '07 #8
you can use a virtual table "DAUL"

your sqlstatmnet will be something like :

insert into tableA (col1,col2,col3,col4,col5)

(select 'Cheese',
'Blue',
'John Wayne',
favorite_movie from tableB,DUAL
where movietype = 'SciFi'
and (moviedate=1965 or 1966)
and B&W !=1,
'Paris')


i had the problem you are facing :D
it is solved now

Mohammad Athamneh
Mar 26 '07 #9
@Mohamad

Can you explain a little more?? the query you use dont work Thanks
Aug 24 '10 #10
NeoPa
32,556 Expert Mod 16PB
Andres, That was the one and only post from that member and it was posted over three years ago. Also, this is not a thread for your question (thus hijacking). I suggest you post your (fully explained) question (that can potentially include a link to this thread) in a new thread which will be dedicated to answering your problem.
Aug 25 '10 #11
Nobody explain that you have to put the same name of the column

Example:

table car2(
idcar
model
window
color
power
)

table car1(
idcar
model
)

now yo want to copy some data from car1 to car2

you only have to do this:

Expand|Select|Wrap|Line Numbers
  1. insert into car2(idcar,model,color,power)
  2. select idcar,model,'Black' as color, '100hp' as power from car1 where id='001'
and done, the trick is in tag the new column with the same name that is in the table

hope this help someone
Aug 26 '10 #12
NeoPa
32,556 Expert Mod 16PB
andresM:
Nobody explain that you have to put the same name of the column
That's because it's not true I'm afraid.

It's certainly good practice, but not necessary technically.

Your example SQL :
Expand|Select|Wrap|Line Numbers
  1. insert into car2(idcar,model,color,power)
  2. select idcar,model,'Black' as color, '100hp' as power from car1 where id='001'
has exactly the same effect as :
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO [Car2]
  2.            ([IDCar]
  3.           , [Model]
  4.           , [Color]
  5.           , [Power])
  6. SELECT      [IDCar]
  7.           , [Model]
  8.           , 'Black'
  9.           , '100hp'
  10. FROM        [Car1]
  11. WHERE      ([ID]='001')
Aug 27 '10 #13
I was looking for an answer to the same question when I stumbled across this thread. Unfortunately, no-one has really answered the question here but I did find a very simple answer elsewhere so will post it here.
Expand|Select|Wrap|Line Numbers
  1. INSERT [INTO] tablename1 (field1, field2, field3, field4)
  2. SELECT field1, field2, field3, A_VALUE dummy_field
  3. FROM tablename2
Here we are inserting into tablename1 some values from the corresponding fields field1, field2, field3 in tablename2. That much is obvious.

However, we either don't have the data in tablename2 for field4 or we want to use a constant value for that field. So in the SELECT statement we refer to a field that does not exist in tablename2 (dummy_field) with a substitute value (A_VALUE) before the non-existent field. This value will be used to insert into tablename1 for all the rows.
Feb 22 '12 #14
NeoPa
32,556 Expert Mod 16PB
You seem to be exhibiting the same confusion that AndresM was suffering from in post #12 Watty. It is simply not true that a field needs to be named (or ALIASed) in the SELECT clause for the SQL to work correctly.

As for there being no extant answer in the thread already, I would suggest that post #13 illustrates everything that your suggestion has, except the confusion. It illustrates the basic requirements clearly and unambiguously, without introducing any optional items as being necessary. Certainly, you can ALIAS the fields if you want to, but most people would tend to ALIAS them to match the destination field, rather than some other, random, name that can only lead to confusion for those who struggle to appreciate the method anyway.

Suggestions are always welcome of course, but it's also important to clarify when someone suggests something misleading or incorrect.
Feb 22 '12 #15
I had the same problem as the opening poster. I kept getting run time error 3134 Syntax error in INSERT INTO statement, with the same cause. The problem is caused by the VALUES keyword, if you omit this the query works. I found the syntax for the query on a MySQL forum, and I guess MySQL needs the VALUES keyword while SQL Server does not.

I hope this saves someone the headache I am currently suffering from trying to solve this problem.
Apr 20 '12 #16
NeoPa
32,556 Expert Mod 16PB
DaveBrads:
I hope this saves someone the headache I am currently suffering from trying to solve this problem.
Unlikely I'm afraid. Like many that have posted here recently you seem to be thoroughly confused about what the question is. Unlike many, at least you are suggesting a solution (rather than trying to throw your own problem into the mix).

Using the VALUES keyword is one of the possible formats supported. For that format it is not optional. For the other format it is not required. The format that uses VALUES requires the keyword VALUES.

What you post is far from clear so it's impossible for me to identify for you where you are confused, but certainly what you contend is not accurate in detail, even if it may seem so from your perspective. If you look through the thread you'll see examples of both of these formats. Better yet, MSDN has the full T-SQL syntax explained in detail.

NB. This is specifically an MS SQL Server forum, so MySQL and other flavours are not valid answers here.
Apr 20 '12 #17
I'm sorry, I don't understand your explanation. Can you explain which format requires VALUES keyword and which doesn't?

For example, this is my original statement:

Expand|Select|Wrap|Line Numbers
  1. INSERT INTO MyTable(ForeignKey1, ForeignKey2, Qty)
  2. VALUES
  3. SELECT <an integer>, ForeignKey2, Qty FROM MyTable
  4. WHERE ForeignKey1 = <another integer>
I removed the VALUES keyword and it worked.
Apr 23 '12 #18
NeoPa
32,556 Expert Mod 16PB
DaveBrads:
Can you explain which format requires VALUES keyword and which doesn't?
I think I can do this, hopefully clearly.

The APPEND type query has an INSERT INTO clause which specifies where the data should go. That's straightforward enough. However, there are two different ways of specifying where the data should come FROM :
  1. SELECT and FROM clauses with other optional clauses just like a standard SELECT query.
  2. A VALUES() list. This includes literal data for each field, and certainly in T-SQL, can handle values for multiple records.
Your example SQL has the VALUES keyword in it, but why is unclear. This clearly seems to be using the SELECT option. I suspect someone added the VALUES keyword as they'd seen it somewhere without a clear understanding of how it can be used.

The full syntax can be found at INSERT (Transact-SQL), but is not easy reading unless you're used to interpreting such information.

Please note that any further communication of a non-technical nature should be handled either by PM (where you can be as critical as you like) or by reporting a post. We have rules about what can be said in a thread and this type of conversation doesn't fall within them.
Apr 23 '12 #19
Thank you so much Watty! Exactly what I was looking for in mysql...

@watty1964
May 20 '12 #20
mit09
1
Thank @watty1964 the solution is 100% accurate..
Feb 28 '13 #21

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: JB | last post by:
To anyone that is able to help.... What I am trying to do is this. I have two tables (Orders, and OrderDetails), and my question is on the order details. I would like to set up a stored...
5
by: Arsen V. | last post by:
Hello, What is the optimal way to insert multiple rows (around 1000) from a web application into a table? The user enters multiple lines into a text box (up to 10,000). The ASP.NET...
0
by: craftit | last post by:
hi everyone, i need to insert multiple rows in a single table using Oledbdataadapter for access database all in one trip in my winform(VB.Net). i've tried the best i can.Can anyone please help me...
3
Atli
by: Atli | last post by:
Hi. I've been trying to insert multiple rows into a table using a single INSERT statement in MSSQL / SQL Server 2005. I could of course cheat and have my C# code insert each row using some sort...
1
by: AlexW | last post by:
Hi I have been scouring the web for some information regarding how to insert multiple rows at once using an SQL string. I am using an OLEDB dataadapter to communicate with an MS Access DB ...
16
by: jasone | last post by:
Hi all, The system im working on currently allows the user to select a number of flowers.. click submit and whatever they clicked is passed onto the next page, i now want them to click order and...
3
by: Vinda | last post by:
Hi Bytes, Using a previous question as a base Access 2000 Inserting multiple rows based on a date range. I also wanted to insert multiple rows into a table according to a date range supplied by a...
2
by: emceemic | last post by:
Is there a way to show multiple rows per row if required? For example, say the table I currently have show the following data: Impact to Business, Business Owner Name, ...
13
by: santhanalakshmi | last post by:
Hi, I am working on SQL 2008 database. In Micosoft SQL server Management studio, i am trying to insert multiple records at a time in a table, using this query insert into...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.