Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
jgoodson
Starting Member
3 Posts |
Posted - 2015-02-25 : 17:21:44
|
I have a table that holds customers CustID, CustName1, John2, Sally3, WilliamAnd a FUNCTION that accepts a single CustID and returns the productIDs purchased by that customer. For example select productID from dbo.uspGetAllProductsUserHasBought(3) SKU003SKU004SKU016--products purchased by WilliamI want to build a new table that looks like the following:CustomerID, Project3, SKU0033, SKU0043, SKU0162, SKU01322, SKU016k332, SKU013441, SKU016k991, SKU016003k1, SKU01600dd3kIn other words, for each row in the customer table pass in the customerID to the function and write out the results to a new table. In the actual production system the tables have millions of rows and the procedure will run off-hours. Thanks in advance. John Goodson |
|
gbritton
Master Smack Fu Yak Hacker
2780 Posts |
Posted - 2015-02-26 : 03:30:05
|
insert into newtableselect custid, pro.jectfrom customerscross apply func(custid) as pro(ject) |
|
|
jgoodson
Starting Member
3 Posts |
Posted - 2015-02-26 : 06:51:05
|
Gonna Answer my own question... CREATE TABLE #CustProducts ( [CustID] VARCHAR(255) NULL, [ProdID] VARCHAR(255) NULL,);GOInsert into #CustProducts( [CustID], [ProdID] )select c.custID, p.[ProdID] from Customers c outer apply (SELECT ProdID from dbo.uspGetAllProductsUserHasBought(r.CustID)) pJohn Goodson |
|
|
jgoodson
Starting Member
3 Posts |
Posted - 2015-02-26 : 06:55:56
|
quote: Originally posted by gbritton insert into newtableselect custid, pro.jectfrom customerscross apply func(custid) as pro(ject)
Thank you for this answer...John Goodson |
|
|
|
|
|
|
|