SQL Server - Passing a table to a function... how? and is it a good idea?

1.9k Views Asked by At

I have a stored procedure that query one of my tables. To that table I want it to add another column with a values that is calculated by a function.

The function is another query with sub queries that is based on user data in another table.

Now since there are a lot of rows to do calculations on, I figure that the same data is being queried over and over for each row by the function.

I thought I might query the needed data for the function in the stored procedure and keep it in a temp table, and pass that table to the function so the function actually queries the in-memory small table instead of making a query to the real table - that way making it faster, and not having too much IO on my real table.

My question is actually 2 parts:

  1. is what I'm saying even makes sense? is it a good solution that will make it faster and more efficient? or is passing table data to a function comes with a high cost?
  2. how do I pass a table to a function?

Thanks

EDIT:

BTW - it's a scalar function

1

There are 1 best solutions below

0
On

You can use Table-Valued Parameters in a stored procedure or function on MS SQL Server 2008 and up:

http://technet.microsoft.com/en-us/library/bb510489.aspx