【问题标题】:Must Declare the Scalar Value in SQL必须在 SQL 中声明标量值
【发布时间】: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;

存储过程是关于获取特定日期的员工日志。它由三个用于员工信息的数据库组成,同时通过根据手动选择的时间范围选择员工的 ma​​x()min() 时间来显示时间。 Select

之后的Insert方法中仍然存在错误

我做错了什么?谢谢指教。

【问题讨论】:

  • RETURNS @LogsTABLE - 应该是 RETURNS @Logs TABLE ,缺少空格。
  • @Abhishek 那里有一个空格。印刷错误。

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


【解决方案1】:

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

在这种情况下,您不需要额外的变量(@UserID, @Fullname 等)。

【讨论】:

  • 所以我做错了。我在其中添加了其他变量。谢谢您的帮助。完美运行
【解决方案2】:

我已经评论了下面的代码,添加了有关您的错误的解释

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;

【讨论】:

  • 感谢您的解释。它也可以完美运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-26
相关资源
最近更新 更多