Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

I have a published server.

Where i'm write a function which returns a table using Exec query.

But Unfortunately i'm getting error

Msg 443, Level 16, State 14, Procedure Fn_GetDailyRentalFullDataForView, Line 15
Invalid use of a side-effecting operator 'INSERT EXEC' within a function.

Msg 444, Level 16, State 2, Procedure Fn_GetDailyRentalFullDataForView, Line 18
Select statements included within a function cannot return data to a client.

Msg 455, Level 16, State 2, Procedure Fn_GetDailyRentalFullDataForView, Line 18
The last statement included within a function must be a return statement.


What I have tried:

Create Function Fn_GetDailyRentalFullDataForView()
Returns  @t table(
					[VehicleType] [int] NULL,
					[vt] [nvarchar](50) NULL,
					[TruckTotal] [nvarchar](50) NULL,
					[Description] [nvarchar](max) NULL,
					[Driver] [nvarchar](50) NULL,
					[Location] [nvarchar](250) NULL,
					[Department] [nvarchar](50) NULL,
					[JobDate] [date] NULL
				)

as
begin
insert into @t				
exec Sp_GetDailyRentalFullDataForView

select * from @t
end


This is my Function.

Please let me know, where i'm getting wrong.

Thanks
Posted
Updated 19-Apr-16 4:44am

1 solution

This code is illegal as you cannot execute stored procedure from a function (read the documentation[^]):
SQL
insert into @t				
exec Sp_GetDailyRentalFullDataForView

You must also return results via return not select:
SQL
Create Function Fn_GetDailyRentalFullDataForView()
Returns  @t table(...)
as
begin
-- populate @t 
return
end
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900