April 24, 2015 at 12:58 pm
There is a table function that returns results based on the account parameter that is referenced.
e.g. select * from dbo.mytablefunction(ACCOUNT)
I have a list of accounts in a different table that I'd like to run this table function on and return results for all of the accounts at once, rather than me having to do this one at a time for each account.
Is there a way to do this? Changing the table function isn't an option.
Thanks in advance.
April 24, 2015 at 1:03 pm
adam-639168 (4/24/2015)
There is a table function that returns results based on the account parameter that is referenced.e.g. select * from dbo.mytablefunction(ACCOUNT)
I have a list of accounts in a different table that I'd like to run this table function on and return results for all of the accounts at once, rather than me having to do this one at a time for each account.
Is there a way to do this? Changing the table function isn't an option.
Thanks in advance.
nothing built in, you'll want to go to the definition of the function, and copy it to create an adhoc query or a new function.
it sounds like internally it is doing nothing more than
SELECT SomeColumns
FROM SomeTable
WHERE SomeColumn = @account
so you'd just modify it something like this, for example
SELECT SomeColumns
FROM SomeTable
WHERE SomeColumn IN(SELECt Account From ListOfAccounts)
Lowell
April 24, 2015 at 1:06 pm
Thanks, I was hoping there would be some kind of loop (beyond my level) or something that could iterate through each account.
Ideally, I'd like to avoid creating a duplicate query (more maintenance), but your solution would certainly work if nothing else will. Thanks for the idea.
April 24, 2015 at 2:48 pm
adam-639168 (4/24/2015)
Thanks, I was hoping there would be some kind of loop (beyond my level) or something that could iterate through each account.Ideally, I'd like to avoid creating a duplicate query (more maintenance), but your solution would certainly work if nothing else will. Thanks for the idea.
You need a loop but not the kind you're thinking of. Something like the following will do the trick using the natural loop built into a SELECT along with the capabilities of CROSS APPLY calling a TableValuedFunction. Hope it's an iTFV (inline Table Valued Function) and not an mTVF (multi-statement Table Valued Function) or it's going to be slow no matter what.
SELECT ca.*
FROM dbo.DifferentTable dt
CROSS APPLY dbo.mytablefunction(dt.Account) ca
;
The cursor/natural loop behind each select runs at near machine language speeds and is called a "Pseudo-Cursor" for short. CROSS APPLY is nothing more than a correlated sub-query capable of return multiple rows for each row in the SELECT.
--Jeff Moden
Change is inevitable... Change for the better is not.
April 24, 2015 at 2:51 pm
Thanks Jeff. I will look into the cross-apply solution. I always learn something new when I'm here 🙂
April 24, 2015 at 3:38 pm
That's the whole idea here! 🙂
Thank you for the feedback and please post back if you have more questions on this problem. And, if the function has the word "BEGIN" anywhere in it, you might want to post it so we can try to convert it to something faster for you.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply