Solved

Combine INSERT and SELECT

Posted on 2003-11-18
5
684 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 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

809 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