我的表值函数将返回空结果。我不确定我的表值函数是否正确格式化。
我已对所有变量进行了硬编码并按自己的方式运行每个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
GO
答案 0 :(得分: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