How to use table variables and temporary tables in function?

890    Asked by SamShukla in SQL Server , Asked on Nov 1, 2019
Answered by Rachit Gupta

Use of Temporary is not allowed in the function. We can use only the permanent table/or the table variables in the function. As the create table/ any DML/DDL operation is not allowed in the function. Also table variables are allowed in the multi-line function table valued only.


CREATE FUNCTION dbo.udf_get_employs_increment(

 @p_lows_lab1 DEC(10,2) ,@p_lows_lab2 DEC(10,2), @p_high_slab DEC(10,2)

)

RETURNS @employ table (

EMPLOYID int,EMPLOYNAME nvarchar(50),Salary FLOAT,Salary_after_increment FLOAT

)

AS


begin

insert into @employ

select EMPLOYID,EMPLOYNAME,EMPLOYSALARY,EMPLOYSALARY*1.10 from employ where EMPLOYSALARY< @p_lows_lab1

insert into @employ

select EMPLOYID,EMPLOYNAME,EMPLOYSALARY,EMPLOYSALARY*1.09 from employ where EMPLOYSALARY between @p_lows_lab1 and @p_lows_lab2


insert into @employ

select EMPLOYID,EMPLOYNAME,EMPLOYSALARY,EMPLOYSALARY*1.08 from employ where EMPLOYSALARY > @p_high_slab


insert into @employ

select EMPLOYID,EMPLOYNAME,EMPLOYSALARY,EMPLOYSALARY*1.07 from employ where EMPLOYSALARY between @p_lows_lab2 and @p_high_slab

return

end


select * from dbo.udf_get_employs_increment(2150,2250,2350)



Your Answer

Interviews

Parent Categories