【发布时间】:2018-03-07 16:57:20
【问题描述】:
我对存储过程和标量值有点陌生。不知何故,我想出了一个线程,我需要将我的存储过程转换为标量值以产生我想要的输出。我尝试将存储过程转换为函数,现在我收到错误 必须声明标量值@---
代码是
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;
存储过程是关于获取特定日期的员工日志。它由三个用于员工信息的数据库组成,同时通过根据手动选择的时间范围选择员工的 max() 和 min() 时间来显示时间。 Select
之后的Insert方法中仍然存在错误我做错了什么?谢谢指教。
【问题讨论】:
-
RETURNS @LogsTABLE- 应该是RETURNS @Logs TABLE,缺少空格。 -
@Abhishek 那里有一个空格。印刷错误。
标签: sql sql-server user-defined-functions