0

i am working with SQL server 2012.

i have created a function in sql like this:

CREATE FUNCTION [dbo].[Fn_IsCompanyExistInUserLocation](@CompanyId int)

RETURNS @Result table
(
UserId int null
)
AS
BEGIN
    insert into @Result(UserId) select UL.UserId FROM [RRT_Reporting].[dbo].[UserLocation] as UL where UL.CompanyId=@CompanyId and UL.LocationType='COMPANY'


    RETURN 
END

i am not sure its right way or not.

now i am trying to execute this function like :

DECLARE @ret as table (UserId int null) 

EXEC @ret = [dbo].[Fn_IsCompanyExistInUserLocation] @CompanyId= 10; 

select UserId from @ret

and i am getting error:

Msg 137, Level 16, State 1, Line 6
Must declare the scalar variable "@ret".
1
  • 2
    You don't need to tell us e.g. that you'll mark the answer that works as accepted. That's how SO works. Commented Mar 27, 2014 at 7:57

2 Answers 2

2

Try this:

DECLARE @ret table (UserId int null) 
insert into @ret select * from [dbo].[Fn_IsCompanyExistInUserLocation](10)
select UserID from @ret
Sign up to request clarification or add additional context in comments.

1 Comment

it gives an error: Msg 156, Level 15, State 1, Line 2 Incorrect syntax near the keyword 'into'.
0

I got it:

SELECT *
FROM [dbo].[Fn_IsCompanyExistInUserLocation](10)

4 Comments

So are you using @ret variable at all now?
dude i tried to test Fn_IsCompanyExistInUserLocation function.
Yes. Always look at the official docs first when starting out!

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.