Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Insert into with Multiple select statements


Insert into with Multiple select statements

Author
Message
Suresh Babu Palla
Suresh Babu Palla
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 453
Hi All,

I have a table with 10 number of columns, out of 10 five need to get the data from different tables.Say TB1 has 10 columns, the 5 columns need to be populated from a single source table the other five has to be populated from its associated columns from 5 different tables. I have written some thing like this but it's not working


Insert into Daily.QRM_OffBalancesheet(
TCMTradeID,BusinessDate,SDL,TCMProductID,TCMOrganisationID,ExposureAmount,ExposureCurrencyID,
TradeDate,MaturityDate,TCMLegalEntityID)

select TransactionID,BusinessDate,SourceSystemName,
(select reference.ProductDescription
from ODS.Daily.TCMStdTradeClaimJHB as Undrawn
Left join ODS.reference.TCMRefProduct as Reference on
Undrawn.TCMProductID = Reference.TCMProductID
where Undrawn.TCMProductID in (102,196,202)),BusinessUnit,Balance,
(select Reference.currencyISOCode
from ODS.Daily.TCMStdTradeClaimJHB as Undrawn
Left Join ODS.Reference.TCMRefCurrency as Reference On
Undrawn.ExposureCurrencyID = Reference.TCMCurrencyID
WHERE Undrawn.TCMProductID IN (102,196,202)),StartDate,MaturityDate,
(select Reference.legalEntityCode
from ODS.Daily.TCMStdTradeclaimJHB as TradesClaim
Left Join ODS.Reference.TCMRefGroupLegalEntity as Reference
ON TradesClaim.TCMLegalEntityID = Reference.TCMLegalEntityID)
from Daily.TCMStdTradeClaimJHB

It's pretty urgent.. your help is much appreciated :-)
Suresh Babu Palla
Suresh Babu Palla
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 453
Does any one has inputs on this ?
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16459 Visits: 13207
I'd suggest you read up on how to join tables. You are referencing tables in your correlated subqueries who are aliased in another correlated subquery. This is not possible.
Example: in the first subquery, you cannot use the table Reference.

Correlated are also a big no-no: they are very bad for performance.

edit: if you want people to help you with actually writing a query, you'd want to post the table DDLs and some sample data.
edit part 2: I rechecked the query in SSMS with some syntax highlighting to make it more readable. I was mistaken: you are not using correlated subqueries but just regular subqueries. Each subquery has Undrawn and Reference table, something I missed in my earlier reply. However, each subquery probably returns more than one row, which is not allowed.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Suresh Babu Palla
Suresh Babu Palla
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 453
Thanks for your Inputs. I did managed to join the tables.
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16459 Visits: 13207
Suresh Babu Palla (10/23/2013)
Thanks for your Inputs. I did managed to join the tables.


Did you get rid of the subqueries?



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Suresh Babu Palla
Suresh Babu Palla
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 453
Yes, I managed to getrid of subqueries and joined all my selects in to one and got the result.
Koen Verbeeck
Koen Verbeeck
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16459 Visits: 13207
Suresh Babu Palla (10/23/2013)
Yes, I managed to getrid of subqueries and joined all my selects in to one and got the result.


Great, glad you got it worked out.



How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?

Member of LinkedIn. My blog at SQLKover.

MCSA SQL Server 2012 - MCSE Business Intelligence
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search