Share via

Adding a tuple from a function to table

SigmaZX 21 Reputation points
2021-08-19T20:41:54.06+00:00

Hi,
I am creating a function and I want it to add a tuple from the function to a table-as the question says above :'D-
I got the function right but I am missing something...I am missing the part of
RETURNS
What should I write after RETURNES keyword ? I want to add a tuple -let's say I want to add The Employee_id , Employee_name, Spec-
How can I return these values into the table ?
Thanks in advance,

SQL Server | Other
SQL Server | Other

Additional SQL Server features and topics not covered by specific categories

0 comments No comments

Answer accepted by question author

Erland Sommarskog 134.6K Reputation points MVP Volunteer Moderator
2021-08-19T21:12:44.503+00:00

I'm not sure that I understand your question. In your function definition you insert any data to a table, but you can do that when you use the function.

Here is an example of an inline table-valued function:

CREATE FUNCTION dbo.inline(@n int) RETURNS TABLE AS
RETURN (SELECT object_id, name FROM sys.objects WHERE object_id % @n = 0)

Here is the same function as a multi-statement function:

CREATE FUNCTION dbo.multistmt(@n int) 
RETURNS @t TABLE (object_id int NOT NULL,
                  name      sysname NOT NULL) AS
BEGIN
   INSERT @t(object_id, name)
     SELECT object_id, name FROM sys.objects WHERE object_id % @n = 0
   RETURN
END

Here are examples of calling them and inserting data into a table:

CREATE TABLE #test (object_id int NOT NULL,
                    name      sysname NOT NULL)
go
INSERT #test (object_id, name)
   SELECT object_id, name FROM dbo.inline(5)
INSERT #test (object_id, name)
   SELECT object_id, name FROM dbo.multistmt(7)

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.