SQL函数错误[在函数中无效使用副作用运算符'select']

时间:2012-11-28 10:39:46

标签: sql-server

任何人都可以帮我在sql server 2012中创建函数,我在sql中新增了它显示错误[在函数中无效使用副作用运算符'select']

CREATE FUNCTION Get_RearEnd_Outflow( @I_TXN_ID INT)
RETURNS int
AS BEGIN
  DECLARE @V_COUNT INT
  SELECT COUNT(*) INTO @V_COUNT FROM IRR_CHARGES_M
  RETURN @V_COUNT
END

3 个答案:

答案 0 :(得分:5)

correct syntax应为

CREATE FUNCTION Get_RearEnd_Outflow( @I_TXN_ID INT)
RETURNS int
AS BEGIN
  DECLARE @V_COUNT INT
  SELECT @V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M)
  RETURN @V_COUNT
END

然而SET is the better way在T-SQL中分配变量

  SET @V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M)

编辑2 :对于多项作业,您可以使用SELECT

对于多个assin

SELECT  @variableOne = someColumn,  @variableTwo = someColumn from tblTable

编辑:从有关slect的MSDN链接

  

对于分配变量,我们建议您使用SET @local_variable而不是SELECT @local_variable。有关更多信息,请参阅SET @local_variable。

答案 1 :(得分:1)

    CREATE FUNCTION Get_RearEnd_Outflow()
     RETURNS int
     AS
     BEGIN
     DECLARE @COUNT INT;
    SET @COUNT =(SELECT COUNT(*) FROM IRR_CHARGES_M);
return @COUNT
    END
     GO

或使用:

CREATE FUNCTION Get_RearEnd_Outflow()
     RETURNS int
     AS
     BEGIN
    return  (SELECT COUNT(*) FROM IRR_CHARGES_M)

    END
     GO

答案 2 :(得分:0)

尝试这样的事情:

CREATE FUNCTION Get_RearEnd_Outflow( @I_TXN_ID INT)
RETURNS int
AS BEGIN
  DECLARE @V_COUNT INT;
    SET @V_COUNT = (SELECT COUNT(*) FROM IRR_CHARGES_M);
  RETURN @V_COUNT;
END