Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Insert into with Multiple select statements Expand / Collapse
Author
Message
Posted Wednesday, October 23, 2013 1:46 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 7:23 AM
Points: 19, Visits: 321
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
Post #1507483
Posted Wednesday, October 23, 2013 3:01 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 7:23 AM
Points: 19, Visits: 321
Does any one has inputs on this ?
Post #1507507
Posted Wednesday, October 23, 2013 3:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:20 PM
Points: 13,252, Visits: 10,133
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 LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1507515
Posted Wednesday, October 23, 2013 4:24 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 7:23 AM
Points: 19, Visits: 321
Thanks for your Inputs. I did managed to join the tables.
Post #1507534
Posted Wednesday, October 23, 2013 4:44 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:20 PM
Points: 13,252, Visits: 10,133
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 LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1507541
Posted Wednesday, October 23, 2013 10:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: 2 days ago @ 7:23 AM
Points: 19, Visits: 321
Yes, I managed to getrid of subqueries and joined all my selects in to one and got the result.
Post #1507724
Posted Wednesday, October 23, 2013 10:58 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Sunday, July 20, 2014 11:20 PM
Points: 13,252, Visits: 10,133
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 LessThanDot.

MCSA SQL Server 2012 - MCSE Business Intelligence
Post #1507754
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse