473,657 Members | 2,422 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SQL statement - INSERT INTO and SELECT

Hi,

I have a very simple issue: for simplicity lets say I have 2 tables, A and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'

In order to post data to table A I thus (from a known text value that should
match 1 value in B.text) have to get the value of B.text before performing
the UPDATE/INSERT statement.

How is this possible?

I would have thought something like

INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] = B.text,
'x4')

however this is not possible, so I'm lost - not experienced in the arts of
SQL:-)
Hope someone can help.

Best Regards,
Daniel
Feb 22 '07 #1
6 18247
On Feb 22, 8:57 am, "dhek" <d...@REMOVEvip .cybercity.dkwr ote:
Hi,

I have a very simple issue: for simplicity lets say I have 2 tables, A and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'

In order to post data to table A I thus (from a known text value that should
match 1 value in B.text) have to get the value of B.text before performing
the UPDATE/INSERT statement.

How is this possible?

I would have thought something like

INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] = B.text,
'x4')

however this is not possible, so I'm lost - not experienced in the arts of
SQL:-)

Hope someone can help.

Best Regards,
Daniel
Try something more like this:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']

Feb 22 '07 #2
>I have a very simple issue: for simplicity lets say I have 2 tables, A
>and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field
which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'

In order to post data to table A I thus (from a known text value that
should
match 1 value in B.text) have to get the value of B.text before
performing
the UPDATE/INSERT statement.

How is this possible?

I would have thought something like

INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] =
B.text,
'x4')

however this is not possible, so I'm lost - not experienced in the arts
of
SQL:-)

Hope someone can help.

Best Regards,
Daniel

Try something more like this:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']
But this is not possible since table B only contains 2 fields (id, and text)
or am I misunderstandig u?
Feb 22 '07 #3
dhek wrote:
>>I have a very simple issue: for simplicity lets say I have 2 tables, A
and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field
which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'

In order to post data to table A I thus (from a known text value that
should
match 1 value in B.text) have to get the value of B.text before
performing
the UPDATE/INSERT statement.

How is this possible?

I would have thought something like

INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] =
B.text,
'x4')

however this is not possible, so I'm lost - not experienced in the arts
of
SQL:-)

Hope someone can help.

Best Regards,
Daniel
Try something more like this:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']

But this is not possible since table B only contains 2 fields (id, and text)
or am I misunderstandig u?
The SELECT portion only gets one of its five values (b.id) from
table B; it gets the other four from the values provided directly
on the SELECT line (which, in practice, might instead be input
parameters to a stored procedure).

Consider this hypothetical alternative:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT c.x1, c.x2, c.x3, b.id, c.x4
FROM BTable b
JOIN Ctable c on b.id = c.id
WHERE b.Text = ['Your Text Here']

Obviously x1 through x4 aren't taken from table B in this case. In
Utahduck's example, x1 through x4 aren't taken from /any/ table.
Feb 22 '07 #4
"Ed Murphy" <em*******@soca l.rr.comwrote in message
news:45******** *************** @roadrunner.com ...
dhek wrote:
>>>I have a very simple issue: for simplicity lets say I have 2 tables, A
and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field
which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'

In order to post data to table A I thus (from a known text value that
should
match 1 value in B.text) have to get the value of B.text before
performing
the UPDATE/INSERT statement.

How is this possible?

I would have thought something like

INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] =
B.text,
'x4')

however this is not possible, so I'm lost - not experienced in the arts
of
SQL:-)

Hope someone can help.

Best Regards,
Daniel
Try something more like this:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']

But this is not possible since table B only contains 2 fields (id, and
text) or am I misunderstandig u?

The SELECT portion only gets one of its five values (b.id) from
table B; it gets the other four from the values provided directly
on the SELECT line (which, in practice, might instead be input
parameters to a stored procedure).

Consider this hypothetical alternative:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT c.x1, c.x2, c.x3, b.id, c.x4
FROM BTable b
JOIN Ctable c on b.id = c.id
WHERE b.Text = ['Your Text Here']

Obviously x1 through x4 aren't taken from table B in this case. In
Utahduck's example, x1 through x4 aren't taken from /any/ table.
He maaaaan, I totally get it now and it works like a bloody charm. If I
could, I would award u guyz 1000000 points each - I really appreciate it -
thanks a lot.

Best Regards,
Daniel
Feb 22 '07 #5
On Feb 22, 10:06 am, Ed Murphy <emurph...@soca l.rr.comwrote:
dhek wrote:
>I have a very simple issue: for simplicity lets say I have 2 tables, A
and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field
which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'
>In order to post data to table A I thus (from a known text value that
should
match 1 value in B.text) have to get the value of B.text before
performing
the UPDATE/INSERT statement.
>How is this possible?
>I would have thought something like
>INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] =
B.text,
'x4')
>however this is not possible, so I'm lost - not experienced in the arts
of
SQL:-)
>Hope someone can help.
>Best Regards,
Daniel
Try something more like this:
INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']
But this is not possible since table B only contains 2 fields (id, and text)
or am I misunderstandig u?

The SELECT portion only gets one of its five values (b.id) from
table B; it gets the other four from the values provided directly
on the SELECT line (which, in practice, might instead be input
parameters to a stored procedure).

Consider this hypothetical alternative:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT c.x1, c.x2, c.x3, b.id, c.x4
FROM BTable b
JOIN Ctable c on b.id = c.id
WHERE b.Text = ['Your Text Here']

Obviously x1 through x4 aren't taken from table B in this case. In
Utahduck's example, x1 through x4 aren't taken from /any/ table.
This is correct. You don't need to "select" from any table. You can
even do things like:

SELECT GetDate() -- Get the date... no tables involved at all
SELECT 'I got this from ATable', * FROM ATable -- I do this quite
often when merging several tables into one so I know the source
SELECT 2+2 -- Just in case you forget what that comes to. :D
SELECT 'Hello World!' -- I do this quite often as a form of
troubleshooting , thought it more closely resembles SELECT 'Finished
Step #7'

Hope that helps!

Feb 22 '07 #6
<Ut******@hotma il.comwrote in message
news:11******** **************@ a75g2000cwd.goo glegroups.com.. .
On Feb 22, 10:06 am, Ed Murphy <emurph...@soca l.rr.comwrote:
>dhek wrote:
>>I have a very simple issue: for simplicity lets say I have 2 tables,
A
and
B.
- Table A contains 5 fields. Amongst these there is a 'id'-field
which
is but a reference to table B.
- Table B contains 2 fields: 'id' and 'text'
>>In order to post data to table A I thus (from a known text value that
should
match 1 value in B.text) have to get the value of B.text before
performing
the UPDATE/INSERT statement.
>>How is this possible?
>>I would have thought something like
>>INSERT INTO A (val1, val2, val3, ID, val4)
VALUES ('x1','x2','x3' , SELECT id FROM B WHERE [SOME TEXT VALUE] =
B.text,
'x4')
>>however this is not possible, so I'm lost - not experienced in the
arts
of
SQL:-)
>>Hope someone can help.
>>Best Regards,
Daniel
Try something more like this:
>INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT 'x1', 'x2', 'x3', b.id, 'x4'
FROM BTable b
WHERE b.Text = ['Your Text Here']
But this is not possible since table B only contains 2 fields (id, and
text)
or am I misunderstandig u?

The SELECT portion only gets one of its five values (b.id) from
table B; it gets the other four from the values provided directly
on the SELECT line (which, in practice, might instead be input
parameters to a stored procedure).

Consider this hypothetical alternative:

INSERT INTO ATable(val1, val2, val3, ID, val4)
SELECT c.x1, c.x2, c.x3, b.id, c.x4
FROM BTable b
JOIN Ctable c on b.id = c.id
WHERE b.Text = ['Your Text Here']

Obviously x1 through x4 aren't taken from table B in this case. In
Utahduck's example, x1 through x4 aren't taken from /any/ table.

This is correct. You don't need to "select" from any table. You can
even do things like:

SELECT GetDate() -- Get the date... no tables involved at all
SELECT 'I got this from ATable', * FROM ATable -- I do this quite
often when merging several tables into one so I know the source
SELECT 2+2 -- Just in case you forget what that comes to. :D
SELECT 'Hello World!' -- I do this quite often as a form of
troubleshooting , thought it more closely resembles SELECT 'Finished
Step #7'

Hope that helps!
It all help indeed of my lacking understanding of what is possible and what
is not. This clearifies a great deal and makes my life much easier. I'm no
longer a troubled man:-)

Thanks again for all your help - I really appreciate it.

Best Regards,
Daniel
Feb 23 '07 #7

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

Similar topics

1
3422
by: Stephen Patten | last post by:
Hi All, While in the process of building my table (40 or so Insert statments) can I then query ("select * from @Table_variable") and use the results up to theat point for another insert into @Table_varible? If you look for stepID -15 I have commented that section out due to it not retuning the correct values. Thank you in advance
4
6921
by: soni29 | last post by:
hi, i have a small question regarding sql, there are two tables that i need to work with on this, one has fields like: Table1: (id, name, street, city, zip, phone, fax, etc...) about 20 more columns Table2: name what i need help with is that table2 contains about 200 distinct names that i need to insert into table1, i'm using sql server, is there a
1
10069
by: avinash | last post by:
hi my self avi i want to copy data from one table to other table,by giving certain condition and i want o use insert statement .in this i want to pass some value directly and some value from select statement , if i try i ll get error i.e all column of destination table (i.e in which i want to insert data) should match with all columns in values column some thing like this. plz give me some helpful suggetion on this
2
2517
by: serge | last post by:
/* This is a long post. You can paste the whole message in the SQL Query Analyzer. I have a scenario where there are records with values pointing to wrong records and I need to fix them using an Update statement. I have a sample code to reproduce my problem. To simplify the scenario I am trying to use Order related tables to explain a little better the tables i have to work with.
10
2473
by: SueB | last post by:
I currently have a 'mail-merge' process in my Access db project. It generates custom filled out Award Certificates based on an SQL SELECT statement in a VBA routine invoked by clicking on a command button. The "problem": I want to conditionally insert some text into the award certificate based on a field selected by the SELECT statement. Is this possible? Details: One of the fields selected is a concatenation of a value from a table
19
8362
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
7
28742
by: Cindy H | last post by:
Hi I'm having a problem getting the insert statement correct for an Access table I'm using. The Access table uses an autonumber for the primary key. I have tried this: INSERT INTO Tournaments (Tournament, MemberName, Score) VALUES (vtournament, vMemberName, vScore) SELECT ID, Tournament, MemberName, Score FROM Tournament WHERE (ID = @@IDENTITY);"
9
2268
by: rhaazy | last post by:
Using MS SQL 2000 I have a stored procedure that processes an XML file generated from an Audit program. The XML looks somewhat like this: <ComputerScan> <scanheader> <ScanDate>somedate&time</ScanDate>
8
3509
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a date that is supplied to the SP as a parameter. The SP is usually a lengthy process (it takes at least 30 mins). The problem is that SQL server 2000 Dev Edition doesn't allow me to insert new invoices that are "younger", while the SP is executing....
1
2556
flexsingh
by: flexsingh | last post by:
Hello there I have kinda got gotten myself into a sticky situation. I am trying to do something which seams too big to do in my head but I feel I kinda know how to do it. My problem is I have a website and and users come in and book a court. There are three courts, and on the first page ill have up its a table which they choose which court they want then they go to a new page which is also a table. The second page has three values to fill...
0
8411
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, 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...
0
8739
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 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...
0
8613
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
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 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...
0
5638
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
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...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.