Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Combine INSERT and SELECT

Posted on 2003-11-18
5
Medium Priority
?
689 Views
Last Modified: 2006-11-17
how do I combine the following 2 statements?

INSERT INTO JL_MACHINE(ServiceTag, DateModified, PCName)
VALUES ('2G40T0J', '18/11/2003 13:52:19', 'LSPC166')

INSERT INTO JL_MACHINE(STAFF) SELECT DIWOR from lawsoft.dbo.STAFF where EARNERNAME = 'john smith'


I tried to put the select from the 2nd statement into a subquery in the VALUES section of the 1st statement, but this will not work giving an error saying "you cannot use subqueries in this context". I am using MS SQL server 2000.

Any help much appreciated.

Tariq
0
Comment
Question by:tariqf
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
lausz earned 300 total points
ID: 9771600
Try this

INSERT INTO JL_MACHINE(ServiceTag, DateModified, PCName,staff)
SELECT '2G40T0J', '18/11/2003 13:52:19', 'LSPC166' ,DIWOR  
from lawsoft.dbo.STAFF where EARNERNAME = 'john smith'

0
 
LVL 1

Author Comment

by:tariqf
ID: 9771657
nice one! does exactly what it says on the tin :)
0
 
LVL 6

Expert Comment

by:lausz
ID: 9771663
Glad I could help ...but why do you select a 'B'? It is only curiousity!
0
 
LVL 1

Author Comment

by:tariqf
ID: 9771885
what do you mean 'B'?

also I have tried to put another entry after the select like this

INSERT INTO JL_MACHINE(ServiceTag, DateModified, PCName,staff,manufacturer)
SELECT '2G40T0J',
'18/11/2003 13:52:19',
'LSPC166',
DIWOR from lawsoft.dbo.STAFF where EARNERNAME = 'john smith',
'1'

but it does not work. How do I add more values?
0
 
LVL 6

Expert Comment

by:lausz
ID: 9772435
You have to change the order...

INSERT INTO JL_MACHINE(ServiceTag, DateModified, PCName,manufacturer,staff)
SELECT '2G40T0J',
'18/11/2003 13:52:19',
'LSPC166', '1' ,
DIWOR
from lawsoft.dbo.STAFF where EARNERNAME = 'john smith'


The first 4 values are hardcoding , and the only one you are selecting from a table is staff, the from condition is for all the query but you are using only staff in your query..The query syntax is always ..

select field1, field2, field3, etc
from table
where .....

you tried to do this and it is wrong


select field1, field2, field3
DIWOR  from table ,
field4, etc
where .....


About the 'B'  I am talking when you accept the answer depend on the result you can select 'a','b','c' ..'b' is not very good and I want to understand why ..


pd: sorry for my English
0

Featured Post

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question