I am kinda new to stored procedure and scalar values. Somehow I came up with a thread wherein I need to convert my stored procedure to scalar value in order to produce the output I wanted. I tried converting my stored procedure into a function and now I am getting the error Must declare the scalar value @---
The code is
Create Function fn_logs( @Month varchar(50) ,@Year varchar(50) ,@date_from datetime ,@date_to datetime) RETURNS @Logs TABLE ( -- Columns returned by the function UserID int PRIMARY KEY NOT NULL, Fullname nvarchar(max) NULL, Description nvarchar(250) NULL, Department nvarchar(250) NULL, DepartmentHead nvarchar(250) NULL, Position nvarchar(250) NULL, Date nvarchar(250) NULL, Month1 nvarchar(250) NULL, Year1 nvarchar(250) NULL, AMIN nvarchar(250) NULL, AMOUT nvarchar(250) NULL, PMIN nvarchar(250) NULL, PMOUT nvarchar(250) NULL ) begin DECLARE @AM DATETIME , @AM_MID DATETIME ,@AM_OUT DATETIME, @PM DATETIME,@PM_MID DATETIME,@PM_OUT DATETIME, @ABSENT NVARCHAR, @Four INt, @IN_AM DATETIME, @OUT_AM DATETIME,@IN_PM DATETIME,@OUT_PM DATETIME; SET @AM= '12:01:00 AM' SET @AM_MID = '10:00:00 AM'; SET @AM_OUT = '12:59:59 PM'; SET @PM = '12:00:00 PM'; SET @PM_MID = '3:00:00 PM'; SET @PM_OUT = '11:59:59 PM'; SET @IN_AM = '8:00:00 AM'; SET @OUT_AM = '12:00:00 PM'; SET @IN_PM = '1:00:00 PM'; SET @OUT_PM = '5:00:00 PM'; SET @ABSENT = 'ABSENT'; SET @Four = 4; INSERT into @Logs Select @UserID= usrinfo.ID, @Fullname=usrinfo.Name, @Description=usrinfo.Description, @Department=grop.Description, @DepartmentHead=grop.DepartmentHead, @Position=grop.HeadPosition, @Date=FORMAT(CONVERT(DATETIME, auth.TransactionTime), 'MM/dd/yyyy'), @Month1=DATENAME(month, auth.TransactionTime), @Year1=DATEPART(year, auth.TransactionTime), @AMIN=max(case when auth.FunctionKey = '1' and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') <= @AM_MID and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') >= @AM then FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') end) , @AMOUT=min(case when auth.FunctionKey = '2' and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') >= @AM_MID and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') <= @AM_OUT then FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') end) , @PMIN=max(case when auth.FunctionKey = '1' and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') >= @PM and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') <= @PM_MID then FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') end), @PMOUT=min(case when auth.FunctionKey = '2' and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') >= @PM_MID and FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') <= @PM_OUT then FORMAT(CONVERT(DATETIME, auth.TransactionTime, 8),'hh:mm:ss tt') end) from NGAC_AUTHLOG as auth INNER JOIN NGAC_USERINFO as usrinfo ON usrinfo.ID = auth.UserID INNER JOIN NGAC_GROUP as grop ON grop.ID = usrinfo.GroupID where auth.AuthResult ='0' AND usrinfo.GroupID = '1' AND DATENAME(month, auth.TransactionTime)=@Month AND DATEPART(year, auth.TransactionTime)=@Year AND FORMAT(CONVERT(DATETIME, auth.TransactionTime), 'MM/dd/yyyy') between @date_from and @date_to group by FORMAT(CONVERT(DATETIME, auth.TransactionTime), 'MM/dd/yyyy'),usrinfo.ID, usrinfo.Name,grop.DepartmentHead, grop.HeadPosition, grop.Description, usrinfo.ID, usrinfo.Description, DATEPART(year, auth.TransactionTime), DATENAME(month, auth.TransactionTime); RETURN; end; The stored procedure is about getting the Employee Logs in a specific day. It consists of three databases for the Employee information while the time is being displayed by selecting the max() and min() time of Employee depending on the time range manually selected. The error persists in the Insert method after Select
What am I doing wrong? Thanks for the enlightenment.
RETURNS @LogsTABLE- should beRETURNS @Logs TABLE, space is missing.