I’m really a beginner in SQL Server programming. I’m writing a table-valued user defined function but when i execute it I get this errors:
*Msg 178, Level 15, State 1, Procedure Select_info_FN, Line 10
A RETURN statement with a return value cannot be used in this context.
Msg 102, Level 15, State 31, Procedure Select_info_FN, Line 12
Incorrect syntax near ‘BEGIN’.*
Here’s my code :
create function Select_info_FN() returns table
as
begin
declare @count int
SELECT @count = COUNT(*) FROM dbo.info
if @count = 0
begin
INSERT INTO dbo.info VALUES (NULL, NULL, NULL, NULL, NULL)
end
return (SELECT * FROM dbo.info)
end
Try something like the following. I don’t know what the column names and types are, but you can replace
Column*with your column schema.However, if your goal is to modify an existing table rather than build a table of data for use in queries, then Aaron Bertrand is correct that you can’t do an insert in a function, and sgeddes is correct that you should use a stored procedure. In that case, try something like the following.