February 14, 2011 at 2:03 am
HI.
I want to ask if there's a way to call a function with the argument "table name" (string),
and the function will return a variable of type Table?
Thanks.
February 14, 2011 at 2:06 am
use sp_executesql it will help you.
February 14, 2011 at 2:09 am
barakos (2/14/2011)
HI.I want to ask if there's a way to call a function with the argument "table name" (string),
and the function will return a variable of type Table?
Thanks.
I think a Table Valued Function is what you are looking for. You can get more information on the same in Books Online or Google..
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
February 14, 2011 at 1:47 pm
barakos,
I am guessing you have an Oracle background and are looking for something similar to the %ROW_TYPE functionality but it does not exist in SQL Server.
There is an open item requesting that functions like this be added to the product (which I voted in favor of) but it's been open for quite a while:
https://connect.microsoft.com/SQLServer/feedback/details/124506
There are no special teachers of virtue, because virtue is taught by the whole community.
--Plato
February 14, 2011 at 2:52 pm
In answer to your question .. Yes there is ... here is an example posted the other day here on SSC at:
http://www.sqlservercentral.com/Forums/Topic1062357-338-1.aspx#bm1063021
by the user with the name of: mister.magoo
CREATE FUNCTION OrdinalDateSuffix( @InputDate DATETIME )
RETURNS TABLE
AS
RETURN ( WITH special(DAY,suffix)
AS
(
SELECT 1, 'st' UNION ALL
SELECT 2, 'nd' UNION ALL
SELECT 3, 'rd' UNION ALL
SELECT 21, 'st' UNION ALL
SELECT 22, 'nd' UNION ALL
SELECT 23, 'rd' UNION ALL
SELECT 31, 'st'
)
SELECT
CONVERT (NVARCHAR(2), DATEPART (DAY, @InputDate)) +
COALESCE (
(SELECT
suffix
FROM
special
WHERE
DAY = DATEPART(DAY,@InputDate)
),'th') AS suffix
)
February 14, 2011 at 4:33 pm
k.thanigaivel (2/14/2011)
use sp_executesql it will help you.
I'm fairly certain that you cannot use sp_executesql in a function, if that's what you meant. The only thing you can exec in a UDF is an extended (xp_*) stored procedure.
--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