473,396 Members | 2,147 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,396 software developers and data experts.

Insert Subqueries

I have tried this insert comand and it errors out telling me that i
cannot use subqueries this way. INSERT INTO tblPartLocation
(PartLocation, Part)VALUES (999,(SELECT PartID FROM tblParts WHERE
PartName = 'test'))

how would i insert a value from a query?

thanks for any help

Jul 6 '06 #1
2 1572
Stu
The syntax you are looking for is:

INSERT INTO tblPartLocation (PartLocation, Part)
SELECT 999, PartID
FROM tblParts
WHERE PartName = 'test'

However, you may also want to reexamine your schema. Don't use tbl as
a prefix for your tables; it's redundant, and unnecessary. Also, you
have a column named Part in one table, but you're inserting the values
of PartID from another table. If the columns represent the same thing,
why don't you name them the same?

HTH,
Stu
pl*******@gmail.com wrote:
I have tried this insert comand and it errors out telling me that i
cannot use subqueries this way. INSERT INTO tblPartLocation
(PartLocation, Part)VALUES (999,(SELECT PartID FROM tblParts WHERE
PartName = 'test'))

how would i insert a value from a query?

thanks for any help
Jul 6 '06 #2
thanks for your reply...the names are legacy. Just trying to make it
more functional.
Stu wrote:
The syntax you are looking for is:

INSERT INTO tblPartLocation (PartLocation, Part)
SELECT 999, PartID
FROM tblParts
WHERE PartName = 'test'

However, you may also want to reexamine your schema. Don't use tbl as
a prefix for your tables; it's redundant, and unnecessary. Also, you
have a column named Part in one table, but you're inserting the values
of PartID from another table. If the columns represent the same thing,
why don't you name them the same?

HTH,
Stu
pl*******@gmail.com wrote:
I have tried this insert comand and it errors out telling me that i
cannot use subqueries this way. INSERT INTO tblPartLocation
(PartLocation, Part)VALUES (999,(SELECT PartID FROM tblParts WHERE
PartName = 'test'))

how would i insert a value from a query?

thanks for any help
Jul 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Agoston Bejo | last post by:
Hi, when a SELECT in an INSERT INTO statement returns no rows, NO_DATA_FOUND exception is raised. How do I write a "nice" SELECT INTO when I expect that 0 or 1 row gets returned, and the variable...
6
by: pete | last post by:
Been banging my head against the wall with subqueries. Even simple stuff like this fails: SELECT CompanyName FROM tblcompanies WHERE CompanyName IN (SELECT HostName FROM tblhosts) Am I...
6
by: Daniel Elliott | last post by:
Hello, I was wondering if anyone would be able to help me with a problem I'm having. I'm trying to use the following query: SELECT Distinct c.site_id FROM campsite c WHERE c.site_id NOT IN...
5
by: Nick | last post by:
Im moving a development app (MySQL 5.0) to a different server which runs MySQL 4.0.20-standard. I am getting errors on queries that have subqueries such as... SELECT id FROM table1 WHERE id IN...
3
by: audj | last post by:
Hello, I am trying to use a subquery to avoid duplicate entries when someone submits a form to subscribe to a mailing list. So I want to check if the email exists before adding the record. ...
4
by: muzu1232004 | last post by:
Can anyone explain me when we use correlated subqueries rather than nested subqueries. Do all the correlated subqueries can be written in nested subqueries form as well ? What are the major...
0
by: acesfull | last post by:
Hi, I am trying to do something in SQL that I have done in PHP, but I am trying to have the operation performed as a stored procedure because of the sheer number of inserts I have to perform in PHP. ...
0
debasisdas
by: debasisdas | last post by:
Using Subqueries ================== The sub query is often referred to as a nested SELECT, Sub - SELECT, or inner SELECT statement. The sub query executes once before the main query. The...
1
by: lizandra | last post by:
Greetings, I am a newbie, I have been working to extract data from a basic sales db and trying to decide when I should use joins and when I should use subqueries. Much of what I read online says...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
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,...
0
Oralloy
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,...
0
jinu1996
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.