Solved

SQL INSERT SELECT Query

Posted on 2014-04-16
7
272 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
[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
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This video teaches viewers about errors in exception handling.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

707 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