Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a function in SqlServer which does not return anything

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:)

like image 902
Narendra Avatar asked Sep 25 '12 11:09

Narendra


People also ask

Can a function return nothing in SQL?

Functions can return VOID if there is no useful data to return, as in a SQL DELETE , INSERT , or UPDATE statement.

Why is my function not returning a value in SQL?

You are never inserting anything into the table variable you're returning. What is supposed to happen? You are also calling the procedure with int and varchar, rather than the other way around.

Does a SQL function have to return a value?

The RETURN statement is used to unconditionally and immediately end an SQL procedure by returning the flow of control to the caller of the stored procedure. When the RETURN statement runs, it must return an integer value. If the return value is not provided, the default is 0.

Can we create function without parameter in SQL Server?

How to Create a Function Without a Parameter in SQL. Line one creates a function then named the function “YTDSALES()”. You can give your function any name. Remember to add parenthesis to the name of the function when without a parameter.


Video Answer


2 Answers

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

like image 113
AnandPhadke Avatar answered Oct 12 '22 23:10

AnandPhadke


create stored procedure instead of function

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

END

See Examples here

like image 27
Joe G Joseph Avatar answered Oct 13 '22 01:10

Joe G Joseph