Solved

Combine INSERT and SELECT

Posted on 2003-11-18
5
686 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 6

Accepted Solution

by:
lausz earned 100 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

751 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