Home > SQL > Table-Valued User-Defined Functions with Temp table

Table-Valued User-Defined Functions with Temp table

Create FUNCTION GetEmployee()
RETURNS  @rtnTable TABLE
(
    — columns returned by the function
    ID UNIQUEIDENTIFIER NOT NULL,
    Name nvarchar(255) NOT NULL
)
AS
BEGIN
DECLARE @myTable table (id uniqueidentifier, name nvarchar(255))

insert into @myTable
select from your stuff

–This select returns data
insert into @rtnTable
SELECT ID, name FROM @myTable
return
END

you can’t access Temporary Tables from within a SQL Function. You will need to use table variables so essentially. After select the value to return it as table formate you have to insert the selected records  to ‘@rtnTable’ which returns in table structure.

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: