0

我的表值函数将返回空结果。我不确定我的表值函数格式是否正确。

我已经对所有变量进行了硬编码并自行运行每个 select 语句,但我无法让表返回结果。

我还填写了一个表变量并选择了@Table_Var,这确实返回了正确的结果,所以它与我的代码结构方式有关。

所有语句都正常工作,我无法从函数返回数据。

任何人都立即看到我做错了什么?

编辑

我已经将那个松散的代码移到一个函数中,这样它就不会那么混乱了。

该功能正常工作,其他一切,当我尝试返回表格时接受。

--select * from  [dbo].[fn_Professionals_GetMiniUserInfo](null,'a9cec535-ac48-4197-b8a0-87702cb14e52') 
ALTER FUNCTION [dbo].[fn_NonPros_GetMiniUserInfo] 
(
 @NonProID  INT = NULL,
 @UserID UNIQUEIDENTIFIER = NULL
) 
RETURNS 
    @Table_Var TABLE 
(
    -- Add the column definitions for the TABLE variable here
    Avatar     NVARCHAR(50), 
    ID         int,
    FName      varchar(50),
    LName      varchar(50),
    Title      varchar(20),
    UserID     UNIQUEIDENTIFIER,
    CompanyID  INT,
    CoWorkers  INT,
    Street     varchar(60),
    City       varchar(30),
    StateCD    char(2),
    ZipCode    varchar(5),
    WorksFor   VARCHAR(30),
    Phone      VARCHAR(12),
    Email      NVARCHAR(256)
)
AS
BEGIN

INSERT INTO @Table_Var

SELECT
    (SELECT [AFCCInc_Com].[dbo].[aspnet_fn_GetProfileElement]('Avatar',ap.PropertyNames,ap.PropertyValuesString )FROM AFCCInc_Com.dbo.aspnet_Profile ap
                                                                                                                 WHERE ap.UserId = nonPro.UserID)as'Avatar',
            na.NonProID'ID',c.FirstName'FName',c.LastName'LName',c.Title,@UserID 'UserID',
            ci.CompanyID,ci.CoWorkers,a.AddressLine, cit.City,s.StateCD,z.ZIPCode,ci.CompanyName'WorksFor',
            p.Phone,
                  (SELECT Email FROM AFCCInc_Com.dbo.aspnet_Membership m WHERE m.UserId = nonPro.UserID)'Email'

                  FROM  NonPros.NonPros nonPro         with(nolock)                                   JOIN
                  NonPros.utbNonProsUtbAddresses    na with(nolock) on na.NonProID = nonPro.NonProID  JOIN
                  dbo.fn_GetCompanyInfo(@UserID)    ci              ON ci.UserID   = nonPro.UserID    JOIN
                  Person.utbAddresses                a with(nolock) ON a.AddressID = na.AddressID     JOIN
                  [Person].[UtbAddressesutbContact] ac with(nolock)on ac.AddressID = a.AddressID      JOIN
                  Person.utbContact                  c with(nolock)ON ac.ContactID = c.ContactID      JOIN
                  Person.utbContactUtbPhones        cp with(nolock)ON c.ContactID = cp.ContactID      JOIN
                  Person.utbPhones                   p with(nolock)ON p.PhoneID = cp.PhonesID         JOIN
                  Person.utbZipCodes                 z WITH(NOLOCK) ON z.ZipCodeID = a.ZipCodeID      JOIN
                  Person.utbCitys                  cit WITH(NOLOCK) ON cit.CityID  = z.CityID         JOIN
                  Person.utbStates                   s WITH(NOLOCK) ON s.StateCD = z.StateCD

                  WHERE c.ContactTypeID = 7/*me*/ 
                  AND p.PhoneTypeID = 6/*main*/
                  AND nonPro.UserID = 'a9cec535-ac48-4197-b8a0-87702cb14e52';
    RETURN 
    END;


    alter FUNCTION fn_GetCompanyInfo (@UserID UNIQUEIDENTIFIER)
RETURNS 
@Table_Var TABLE 
(
    UserID      UNIQUEIDENTIFIER,
    CompanyID   INT, 
    CompanyName VARCHAR(50),
    CoWorkers   INT
)
AS
BEGIN

    INSERT INTO @Table_Var

          SELECT @UserID,
                 p.ProfessionalID, 
                 p.ProfessionalName,
                (SELECT COUNT(NonProID)'CoWorkers' FROM BuildingPros.utbProfessionalsUtbNonPros with(nolock)WHERE ProfessionalID = p.ProfessionalID)

                                    FROM BuildingPros.utbProfessionalsUtbNonPros bp with(nolock)                                       JOIN
                                         BuildingPros.utbProfessionals            p with(nolock)ON p.ProfessionalID = bp.ProfessionalID

                      WHERE bp.NonProID = (SELECT NonProID FROM NonPros.NonPros where UserID = @UserID);
    RETURN 
END

4

1 回答 1

0

在我清理代码并将插入上方的那些选择移动到我的表变量中之后,一切正常。

我相信这部分是我的问题。

**(SELECT [AFCCInc_Com].[dbo].[aspnet_fn_GetProfileElement]('Avatar',ap.PropertyNames,ap.PropertyValuesString)),**

.

SELECT
    (SELECT [AFCCInc_Com].[dbo].[aspnet_fn_GetProfileElement]('Avatar',ap.PropertyNames,ap.PropertyValuesString)),
            na.NonProID'ID',c.FirstName'FName',c.LastName'LName',c.Title,'a9cec535-ac48-4197-b8a0-87702cb14e52' 'UserID',
            ci.CompanyID,ci.CoWorkers,a.AddressLine, cit.City,s.StateCD,z.ZIPCode,ci.CompanyName'WorksFor',
            p.Phone,
                  (SELECT Email FROM AFCCInc_Com.dbo.aspnet_Membership m WHERE m.UserId = nonPro.UserID)'Email'

                  FROM  
                  NonPros.NonPros nonPro                                                  JOIN
                  AFCCInc_Com.dbo.aspnet_Profile ap      ON ap.UserId =nonPro.UserID       JOIN
                  NonPros.utbNonProsUtbAddresses    na   ON na.NonProID = nonPro.NonProID  JOIN
                  dbo.fn_GetCompanyInfo('a9cec535-ac48-4197-b8a0-87702cb14e52')    ci   ON ci.UserID   = nonPro.UserID    JOIN
                  Person.utbAddresses                a   ON a.AddressID = na.AddressID     JOIN
                  [Person].[UtbAddressesutbContact] ac   ON ac.AddressID = a.AddressID     JOIN
                  Person.utbContact                  c   ON ac.ContactID = c.ContactID     JOIN
                  Person.utbContactUtbPhones        cp   ON c.ContactID = cp.ContactID    JOIN
                  Person.utbPhones                   p   ON p.PhoneID = cp.PhonesID       JOIN
                  Person.utbZipCodes                 z   ON z.ZipCodeID = a.ZipCodeID      JOIN
                  Person.utbCitys                  cit   ON cit.CityID  = z.CityID         JOIN
                  Person.utbStates                   s   ON s.StateCD = z.StateCD

                  WHERE c.ContactTypeID = 7/*me*/ 
                  AND p.PhoneTypeID = 6/*main*/
                  AND nonPro.UserID = 'a9cec535-ac48-4197-b8a0-87702cb14e52'
    RETURN 
于 2013-02-15T04:09:42.707 回答