?
Solved

How to insert into a derived table, & then select from the final result with Joins.  Beginner level.

Posted on 2013-06-16
12
Medium Priority
?
334 Views
Last Modified: 2013-06-18
Hello, I'm trying to add data to a derived table and being new to MS SQL am confused by the correct syntax.  Also my google searches are returning 0 results, so I'm not sure if I'm allowed to do what i'm attempting, though I can't see why not.  The 2nd stage of this query is to then requery tblSupplierData for additional fields.

INSERT INTO derivedtbl_1 (fldSupplierId, fldItemCode)
SELECT (fldSupplierId, (fldItemCode)
FROM tblSupplierData
WHERE (fldSupplierId = @supplierID) AND (fldIssueSequenceNumber = @sequenceNo)

(SELECT        fldSupplierId, fldItemCode
FROM            tblSubscriptionSales
WHERE        (fldSupplierId = @supplierID) AND (fldSubscriptionId = @subscriptionId) AND (fldCustomerId = @customerID)) As derivedtbl_1
0
Comment
Question by:StuBabyAight
[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
  • 6
  • 5
12 Comments
 

Author Comment

by:StuBabyAight
ID: 39251929
Sorry, I have to go out for two hours and cannot attempt any expert suggestions until I return.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39251935
oops, a mistake - sorry
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39251946
but as I'm here this page discusses the topic:
http://www.blackwasp.co.uk/SQLSelectInsert.aspx
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 49

Accepted Solution

by:
PortletPaul earned 1000 total points
ID: 39251956
and this should work I believe- with a union all
SELECT
      fldSupplierId
    , fldItemCode
INTO derivedtbl_1
FROM tblSupplierData
WHERE (fldSupplierId = @supplierID)
    AND (fldIssueSequenceNumber = @sequenceNo)

UNION ALL

SELECT
      fldSupplierId
    , fldItemCode
FROM tblSubscriptionSales
WHERE (fldSupplierId = @supplierID)
    AND (fldSubscriptionId = @subscriptionId)
    AND (fldCustomerId = @customerID)

Open in new window

0
 

Author Comment

by:StuBabyAight
ID: 39252180
I get a "Must declare the scalar variable "@supplierID" message when I try this.  Any suggestions?

(Oh, and this is after adding ( )) as derivedtbl_1 to the end of the second SELECT.  Though the same error appears without as per your original statement.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39252231
there is the presumption that (as you proposed them) that you would still be declaring and using the variables...

declare @supplierID int
declare @sequenceNo int --<< maybe, I don't know what ata type it is
declare@customerID int
declare@subscriptionId int

set @supplierID = 100 --<< whatever values makes sense, do this for each variable
...

then run the query

>>after adding ( ))
??

you have a tendency toward overuse of parentheses :)
none of the parentheses that survived into my comment above are actually required
0
 

Author Comment

by:StuBabyAight
ID: 39252235
I'm a beginner to this, so while the answer here didn't work, it sent me down the right path which was exploring the "UNION ALL" command enabling me to solve the problem.
0
 

Author Comment

by:StuBabyAight
ID: 39252295
PortletPaul, thank you for your time on this question.  I am well aware that as a beginner I will be prone to asking and formatting questions in a manner that may come across as counter-intuitive as I get my head around these building blocks.  I really appreciate your input.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39252351
no problem, you are welcome to keep the question open to arrive at a better result (& this would would be better for me than a C grade :(

please keep in mind that
UNION ALL

has an important difference to
UNION

(without the word 'ALL' a UNION will remove duplicate rows and because of this consumes more processing effort) e.g. see:
http://blog.sqlauthority.com/2009/03/11/sql-server-difference-between-union-vs-union-all-optimal-performance-comparison/

there are times when UNION makes sense, and there are times when UNION ALL is approriate - just be aware of the differences.
0
 

Author Comment

by:StuBabyAight
ID: 39252396
Oh no, sorry about the C, how can I change it?  I'm back after three years and not familiar with the points allocation system.  The extra knowledge about UNION processing certainly warrants an upgrade.
0
 
LVL 49

Expert Comment

by:PortletPaul
ID: 39252409
cheers, you may request attention and ask to re-open the question.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

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…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Suggested Courses

765 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