Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we call function inside an function SQL Server 2005

ALTER function [dbo].[getEmployeeID](@ID int) returns table
as  
  begin
  return (
    select * from [dbo].[gtEmployeeName](2)
    select * from Employees where EmployeeID = @ID)
end

here [dbo].[gtEmployeeName] is an other function that I am trying to call.

I am getting an error, can we call or is there any syntax problem?

Msg 156, Level 15, State 1, Procedure getEmployeeID, Line 6
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Procedure getEmployeeID, Line 6
Incorrect syntax near ')'.

Thanks Prince

like image 899
happysmile Avatar asked Jul 30 '11 18:07

happysmile


People also ask

Can we call function inside function?

Calling a function from within itself is called recursion and the simple answer is, yes.

Can functions be called from within an SQL select statement?

When calling a function from a SELECT statement, that function can only perform DML (insert, update, delete) statements if it is defined as an AUTONOMOUS_TRANSACTION. 2. You should avoid using RESULT_CACHE with temporary tables. The PL/SQL documentation even presents this as a restriction.

Can procedure be called in function?

A procedure cannot be called by a function. DML statments cannot be executed within a function. DML statements can be executed within a procedure.


1 Answers

If [dbo].[gtEmployeeName] returns scalar you probably are looking for

ALTER function [dbo].[getEmployeeID](@ID int) returns table
as  
begin
return (
    select *, [dbo].[gtEmployeeName](2) as EmpName from Employees where EmployeeID=@ID)
end

If [dbo].[gtEmployeeName] returns table you probably are looking for

ALTER function [dbo].[getEmployeeID](@ID int) returns table
as  
begin
return (
    select * from [dbo].[gtEmployeeName](2) EN
    inner join Employees E on EN.EmployeeID = E.EmployeeID
    where EmployeeID=@ID)
end

Update the join to outer if that is what you need. Also update the join condition (the example assumes that the returned table from gtEmployeeName has a column EmployeeID and that can be used for joining to Employees.

like image 124
amit_g Avatar answered Sep 24 '22 14:09

amit_g