必须在SQL

时间:2018-03-07 08:47:43

标签: sql sql-server user-defined-functions

我对存储过程和标量值有点新意。不知怎的,我提出了一个线程,其中我需要将我的存储过程转换为标量值,以便产生我想要的输出。我尝试将我的存储过程转换为函数,现在我收到错误必须声明标量值@ ---

代码是

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()时间来显示时间,具体取决于手动选择的时间范围。 选择

后,插入方法中的错误仍然存​​在

我做错了什么?感谢您的启发。

2 个答案:

答案 0 :(得分:1)

query getUser($id: ID!) { getUser(id: $id) { ... on UserPrivate { id, username } ... on UserPublic { id, username, email } } } 语句替换为:

INSERT

你拥有它的方式,你只是为这些变量赋值。在这种情况下,insert into @Logs select usrinfo.ID, usrinfo.Name, usrinfo.Description, grop.Description, grop.DepartmentHead, grop.HeadPosition, FORMAT(CONVERT(DATETIME, auth.TransactionTime), 'MM/dd/yyyy'), DATENAME(month, auth.TransactionTime), DATEPART(year, auth.TransactionTime), 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) , 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) , 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), 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 仅用作SELECT

SET

在这种情况下,您不需要其他变量(declare @SomeVar int select @SomeVar = 5 -- this is just setting a value to the variable set @SomeVar = 5 -- same as this 等)。

答案 1 :(得分:0)

我已对以下代码进行了评论,并添加了有关错误的说明

Create Function fn_logs(
@Month varchar(50)
,@Year varchar(50)
,@date_from datetime
,@date_to datetime)

RETURNS @Logs TABLE --error here previous was @LogsTABLE
(
    -- 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 (
    UserID, 
    Fullname, 
    Description, 
    Department, 
    DepartmentHead,
    Position, 
    Date, 
    Month1,
    Year1,
    AMIN, 
    AMOUT, 
    PMIN,
    PMOUT
)
Select --You cant use variable assignment in an insert+subquery
usrinfo.ID,
usrinfo.Name, 
usrinfo.Description,
grop.Description, 
grop.DepartmentHead, 
grop.HeadPosition, 
FORMAT(CONVERT(DATETIME, auth.TransactionTime), 'MM/dd/yyyy'), 
DATENAME(month, auth.TransactionTime),
DATEPART(year, auth.TransactionTime),
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) ,
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) ,
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),
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;