开发者

Is it possible to join a table valued function and another table with parameters

开发者 https://www.devze.com 2022-12-31 23:35 出处:网络
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 p

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号