SQL Server表函数不返回值

时间:2017-01-02 21:26:23

标签: sql sql-server

我正在写这个表函数

CREATE FUNCTION udf_RealEstateAgentSales
    (@FirstName NVARCHAR, @SecondName NVARCHAR, @LastName NVARCHAR)
RETURNS @salesAmoutAndAgent TABLE (FirstName NVARCHAR(25),
                                   SecondName NVARCHAR(25),
                                   LastName NVARCHAR(25),
                                   sales INT)
AS
BEGIN
    DECLARE @sales INT;
    SET @sales = COUNT(*);

    INSERT @salesAmoutAndAgent
        SELECT @FirstName, @SecondName, @LastName, @sales
        FROM Purchases AS P
        INNER JOIN Employees AS E ON P.DealMadeByEmployeeID = E.EmployeeID
        WHERE 
            @FirstName = FirstName 
            AND @SecondName = SecondName 
            AND @LastName = LastName

    RETURN
END

我希望它根据他的名字,姓名和姓氏返回firstName,secondName,lastName和sales ...

但是当我称之为

SELECT * 
FROM dbo.RealEstatudf_eAgentSales('somename', 'somename', 'somename');

它返回一个空表......我的错误是什么?

2 个答案:

答案 0 :(得分:3)

首先:Bad habits to kick : declaring VARCHAR without (length) - 您应始终为您使用的任何(n)varchar变量和参数提供长度。如果您使用NVARCHAR - 那么您会得到完全 1个字符长度的字符串通常而不是您想要的.....为...提供合理的长度你的参数!否则,您在调用中传递的值somename将被截断为s .....

下一步:您正在选择,但是您正在使用参数名称(带有前导@) - 而不是列名称 - 赢得了工作

第三:你的WHERE条款都错了 - 你需要写它

WHERE (column name) = @(parameter name)

试试这段代码:

CREATE FUNCTION udf_RealEstateAgentSales
    (@FirstName NVARCHAR(25),
     @SecondName NVARCHAR(25), 
     @LastName NVARCHAR(25) )
RETURNS @salesAmoutAndAgent TABLE (FirstName NVARCHAR(25),
                                   SecondName NVARCHAR(25),
                                   LastName NVARCHAR(25),
                                   sales INT)
AS
BEGIN
    DECLARE @sales INT;
    SET @sales = COUNT(*);

    INSERT @salesAmoutAndAgent
        SELECT 
            FirstName, SecondName, LastName, sales
        FROM 
            Purchases AS P
        INNER JOIN 
            Employees AS E ON P.DealMadeByEmployeeID = E.EmployeeID
        WHERE 
            FirstName = @FirstName 
            AND SecondName = @SecondName 
            AND LastName = @LastName

    RETURN
END

另外:那行

SET @sales = COUNT(*);

根本没有任何意义 - 你需要重新思考(并重新编码)这个更有意义的东西......你从

中计算 ?您肯定需要至少一个FROM (table name),并且很可能还需要一些合理的WHERE条款......

答案 1 :(得分:0)

我在您的代码中发现了一些错误,您可以在下面查看

<强>说明

  1. 在您的描述中,您调用了名为 RealEstatudf_eAgentSales 的函数,根据您的示例,它应该是 udf_RealEstateAgentSales

    < / LI>
  2. 您必须为NVARCHAR类型的参数指定大小。它们没有大小,例如我分配了25个。

  3. COUNT 聚合函数,您必须将其包含在查询中,并且可以避免创建变量。

  4. 应更改var width = 720, height = 720; var data = new ImageData( new Uint8ClampedArray(4 * width * height), 720, 720 ); console.log(data);部分中的顺序。首先是字段然后是参数。但是,这不是一个错误,因为你现在拥有它,但这是一个不好的做法。

  5. 我留下了我使用的代码,增强功能和一些示例

  6. 代码:

    WHERE

    现在功能应该是这样的:

    --Purchases
    IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Purchases]') AND TYPE IN (N'U'))
    BEGIN
        DROP TABLE Purchases
    END
    
    CREATE TABLE Purchases 
    (
        PurchaseID INT IDENTITY
        , DealMadeByEmployeeID INT
        , PurchaseValue MONEY
    )
    
    INSERT INTO Purchases ( DealMadeByEmployeeID, PurchaseValue )
    VALUES                 ( 1,                    250.15        )
    ,                      ( 2,                    15.50         )
    ,                      ( 1,                    100           )
    ,                      ( 1,                    300.15        )
    ,                      ( 2,                    500.15        )
    
    
    SELECT *
    FROM Purchases
    
    
    --Employees
    IF EXISTS (SELECT * FROM sys.objects WHERE OBJECT_ID = OBJECT_ID(N'[dbo].[Employees]') AND TYPE IN (N'U'))
    BEGIN
        DROP TABLE Employees
    END
    
    CREATE TABLE Employees 
    (
        EmployeeID INT IDENTITY
        , FirstName VARCHAR(50)
        , SecondName VARCHAR(50)
        , LastName VARCHAR(50)
    )
    
    INSERT INTO Employees ( FirstName, SecondName, LastName )
    VALUES                 ( 'J',       'E',        'P'      )
    ,                      ( 'Mitko',   '',         'Z'      )
    
    
    SELECT *
    FROM Employees
    GO
    

    一个。员工somename

    --FUNCTION
    DECLARE @strSQL nvarchar(1000)
    IF EXISTS (select * from dbo.sysobjects where id = object_id(N'[dbo].[udf_RealEstateAgentSales]'))
    BEGIN
        SET @strSQL = 'DROP FUNCTION [dbo].[udf_RealEstateAgentSales]'
        EXEC sp_executesql @strSQL
    END
    GO
    
    CREATE FUNCTION udf_RealEstateAgentSales 
        (@FirstName NVARCHAR(25)      -- Missed length
        , @SecondName NVARCHAR(25)    -- Missed length
        , @LastName NVARCHAR(25)      -- Missed length
        )
    RETURNS @salesAmoutAndAgent TABLE (
        FirstName NVARCHAR(25)
        , SecondName NVARCHAR(25)
        , LastName NVARCHAR(25)
        , sales INT
        )
    AS
    BEGIN
    
        INSERT @salesAmoutAndAgent
        SELECT @FirstName
            , @SecondName
            , @LastName
            , COUNT(*) AS Sales
        FROM Purchases AS P
            INNER JOIN Employees AS E
                ON P.DealMadeByEmployeeID = E.EmployeeID
        WHERE FirstName = @FirstName
            AND SecondName = @SecondName
            AND LastName = @LastName
    
        RETURN
    
    END
    
    GO
    

    - &GT;

    SELECT * FROM dbo.udf_RealEstateAgentSales('somename','somename','somename')
    

    湾员工J E P

    FirstName                 SecondName                LastName                  sales
    ------------------------- ------------------------- ------------------------- -----------
    somename                  somename                  somename                  0
    
    (1 row(s) affected)
    

    - &GT;

    SELECT * FROM dbo.udf_RealEstateAgentSales('J','E','P');
    

    ℃。员工Mitko Z

    FirstName                 SecondName                LastName                  sales
    ------------------------- ------------------------- ------------------------- -----------
    J                         E                         P                         3
    
    (1 row(s) affected)
    

    - &GT;

    SELECT * FROM dbo.udf_RealEstateAgentSales('Mitko','','Z');
    

    我希望你觉得它很有用