Solved

SQL INSERT SELECT Query

Posted on 2014-04-16
7
260 Views
Last Modified: 2014-04-16
Experts, I am trying to create an insert query. I need to insert some data from a user form and then need to get some data from two other tables. I cannot figure this out. Is what I am trying to do possible?

Please Help...
        strSql = "INSERT INTO ProjectProfile (ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount," & _
                 "QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) " & _
                 "VALUES (" & _
                 "'" & txtProjectName.Text & "','" & txtQuoteNumber.Text & "',#" & Today & "#),(" & _
                 "SELECT Freight,FOB,Lineitems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife FROM QuoteDefaults),(" & _
                 "SELECT Onsite,Offsite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software FROM ServiceRates)"

Open in new window

0
Comment
Question by:Basicfarmer
7 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40004173
your question is a little unclear...
 are you asking for how to write the following query in VB.NET  Syntax ?
0
 
LVL 5

Expert Comment

by:jayakrishnabh
ID: 40004181
write a single select statement where you can pass first 3 values from the form values.
 
something like this..
Insert into ProjectProfile(columns...)
select txtProjectName.Text, txtQuoteNumber.Text, other columns...
FROM table
0
 

Author Comment

by:Basicfarmer
ID: 40004198
Plusone3055, I was hoping that my attempt to create this query would show what I wanted to do. This query does not work.  How should this be written so it works? I dont need any vb specifc syntax im just creating the string.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 40004210
It should be possible (depending on the database you are using).

Try something like:
strSql = "INSERT INTO ProjectProfile 
(ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) 
SELECT '" & txtProjectName.Text & "', '" & txtQuoteNumber.Text & "', #" & Today & "#, t1.Freight,t1.FOB,t1.Lineitems,t1.GroupTotal,t1.TotalList,t1.IncludeService,t1.Discount,t1.QuoteLife,
	t2.Onsite,t2.Offsite,t2.Training,t2.Overtime,t2.Saturday,t2.SaturdayOvertime,t2.Sunday,t2.Hold,t2.Travel,t2.WeekendTravel,t2.Mileage,t2.Software
				 FROM QuoteDefaults t1, ServiceRates t2"

Open in new window

0
 

Author Comment

by:Basicfarmer
ID: 40004214
Jayakrishnabh, this gave me an error at "Select Freight".
        'strSql = "INSERT INTO ProjectProfile (ProjectName,QuoteNumber,LogDate,Freight,FOB,LineItems,GroupTotal,TotalList,IncludeService,Discount," & _
        '         "QuoteLife,Onsite,OffSite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software) " & _
        '         "SELECT " & _
        '         "'" & txtProjectName.Text & "','" & txtQuoteNumber.Text & "',#" & Today & "#," & _
        '         "SELECT Freight,FOB,Lineitems,GroupTotal,TotalList,IncludeService,Discount,QuoteLife FROM QuoteDefaults," & _
        '         "SELECT Onsite,Offsite,Training,Overtime,Saturday,SaturdayOvertime,Sunday,Hold,Travel,WeekendTravel,Mileage,Software FROM ServiceRates"

Open in new window

0
 

Author Closing Comment

by:Basicfarmer
ID: 40004225
That was exactly what I needed, Thanks Carl...
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40004226
>>I need to insert some data from a user form and then need to get some data from two other tables.

Is the data being inserted ONLY from the user form?
The get some data - is that after the INSERT have completed?

If the answer to both of the above questions is true then you need to perform the action in two (or more) distinct steps.  First run the INSERT SQL command then the SELECT command(s) to return the data.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now