18

I am creating a simple function which inserts a row to a table. After the insertion I don't need the function to return anything. I just checked the function creation syntax in MSDN and it seems like a function must return a value. I also tried to create a function but got syntax error.

I wonder if there is a way to create a function which doesn't return any value.

Can anyone please let me know if there is a way to create such a function? Thanks in advance:)

1
  • Functions is meant to return some sort of value. If you just want a 'function' to execute an expression without any return value, then maybe what you want is a Stored Procedure. Commented Aug 21, 2018 at 9:16

4 Answers 4

18

Then use Stored Procedure to achieve your requirement. Because function is meant for returning values.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but can I call this new stored procedure inside other stored procedures?
Ok. I will create a proc then instead of function.
I have flagged this answer as 'This is not an answer'. It should had been a comment instead.
12

create stored procedure instead of function

Create proc my_proc()
as
BEGIN
<your insert statement >

END

See Examples here

2 Comments

Thanks, but can I call this new stored procedure inside other stored procedures?
Ok. I will create a proc then instead of function.
5

In addition to the other answers, I'll add an obvious one:

I am creating a simple function which inserts a row to a table.

A function can't insert a row into a table (by definition, a function in SQL Server is not supposed to have any side effects, which is different from functions in other languages), so whether or not you cared about returning a value, it can't be done anyway. You will need to use a stored procedure instead.

Comments

1

According to SQL Server, you cant have a function without a return value. It has to return a value. In your case i would advise is to go for Stored procedure.

2 Comments

Do you have any source?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.