Quick background for those interested,
I have a master detail table(options date), about 20 details for each master record. Our oltp system that is saving the data was doing 21 inserts for each new piece of information we saved. This was killing the server, so I'm trying to get this working by substituting comma separated values for the details table. Everything seems to be working, except I can figure out how to get the details table back. I'm trying to use table valued functions, and it's not quite working.
I'd like to call something like
Select Top 100 * FROM dbo.fn_MarketDataDetails (MarketDataMasterID) mdd INNER JOIN MarketDataMaster mdm on mdm.MarketDataMasterID = mdd.MarketDataMasterID
Clearly, that just doesn't compile.
I can run
Select Top 100 * FROM dbo.fn_MarketDataDetails (15425) // Assuming 15425 is a valid MarketDataMasterID
And I get back a table that looks like my old details table.
Is this even possible? Am I making any sense?
In this article Because the return type of a table-valued function is Table , you can use a table-valued function anywhere in SQL that you can use a table.
SQL Server supports table valued functions, what are functions that return data in the form of tables. JOIN operations in SQL Server are used to join two or more tables. However, JOIN operations cannot be used to join a table with the output of a table valued function. APPLY operators are used for this purpose.
Yes if those are same columns.
The APPLY operator should do the trick:
SELECT * from MarketDataMaster cross apply dbo.fn_MarketDataDetails (MarketDataMasterID)
This essentially calls the function once per row returned from MarketDataMaster. "cross apply" works like an inner join, in that only rows for which data is returned by the function will be returned; use "outer apply" for functionality similar to left outer joins.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With