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!
20 188285
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_movi e=B.favorite_mo vie
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?
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!
All: I ended up using two queries to perform this operation. It increased the number of steps required, but worked and was consistent.
iburyak 1,017
Recognized Expert Top Contributor
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]
where are you using your sql for oracle or mssql/postgre/mysql?
if mssql
--
insert into urtable
values
('val1','val2') ,
('val1','val2') ;
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
@Mohamad
Can you explain a little more?? the query you use dont work Thanks
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 procedure that essentially inserts in the orders
table the mail order, and then insert multiple orderdetails within the
same transaction. I also need to do this via SQL 2000. Right now i
have "x" amount of variables for all columns in my orders tables,...
|
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
application breaks that data into a string array. Each line is an item of
that array.
The user clicks Submit.
|
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 out .
Thanks
|
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 of a loop, but where is the fun in that :-)
If you'r familliar with MySQL, this MySQL query would do what I am trying to do with MSSQL / SQL Server 2005.
INSERT INTO
UserInfo(InfoName, InfoValue)
VALUES
|
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
The code below works:
myDataAdapter.InsertCommand = New OleDbCommand("INSERT INTO Inventory ( PartNum ) SELECT 'blah' AS PartNum FROM
myDataAdapter.Update(gbldataset, "Inventory")
| |
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 each flower is inserted into the table.
username - flowerid - qty - total
user1 ------------ 2 ------- 1----- 1.99
user1 ------------ 3 ------- 5----- 2.55
user1 ------------ 6 ------- 1----- 0.50
i havent set up the session yet to display...
|
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 user (eg txtRDateStart & txtRDateEnd). The script was envisaged to be able to also gather a time and text description that would be repeated within each row.
For example:
The user would enter...
Start Date: 13/03/2010
End Date: 17/03/2010...
|
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, Breakdown
1)Application, John Doe, Application
2)Hardware, Joe Smith , Hardware
3)Multiple , John Doe , ...
|
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 test1 values(6,'san6',1),( 7,'san7',1);
Error:
Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near ','.
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |