이 테이블 값 함수가 있으며 동일한 "PeopleID"를 가진 모든 레코드를 표시하고 싶습니다. 내가 어떻게 해? 현재 가능한 결과 중 하나만 표시되고 있습니다.
ALTER FUNCTION dbo.ufnGetContactInformation(@FuncID int)
RETURNS @retContactInformation TABLE
(
-- Columns returned by the function
FuncID int PRIMARY KEY NOT NULL,
Full_Name nvarchar(75) NULL,
Phone nvarchar(100) NULL,
Email nvarchar(50) NULL,
City nvarchar(20) NULL
)
AS
BEGIN
DECLARE
@fullname nvarchar(75),
@phonenumber nvarchar(100),
@email nvarchar(50),
@city nvarchar(20);
-- > Get common contact information
SELECT
@fullname = p.Full_Name,
@phonenumber = c.Phone,
@email = c.Email,
@city = c.CityID
FROM d_People p, d_Contacts c
WHERE p.PeopleID=@FuncID;
-- < Get common contact information
IF @FuncID IS NOT NULL
BEGIN
INSERT @retContactInformation
SELECT @FuncID, @fullname, @phonenumber, @email, @city;
END;
RETURN;
END;
GO
join
두 테이블에 적절한 것이 필요합니다 . 그렇지 않으면 쿼리가 의미가 없습니다. 당신이 지금 가지고있는 것은 당신에게 한 사람 줄 것이다 full_name
, 그러나 모든 접촉 phone
, email
등을 city
.
걷어차는 나쁜 습관 : 구식 JOIN 사용-Aaron Bertrand
스칼라 변수를 선택하면 각 변수에서 하나의 결과 만 얻습니다. 한 행만 유지하도록 설정 한 후 변수를 삽입하면 최대 한 행만 생성됩니다.
다음과 같이 인라인 테이블 반환 함수로 함수를 단순화 할 수 있습니다.
alter function dbo.ufngetcontactinformation(@funcid int)
returns table as return (
select
funcid = p.peopleid
, fullname = p.full_name
, phonenumber = c.phone
, email = c.email
, city = c.cityid
from d_people p, d_contacts c
where p.peopleid = @funcid;
);
go
참고
성능 저하에도 불구하고 다중 문 테이블 값 함수가 있어야하는 경우 :
alter function dbo.ufngetcontactinformation(@funcid int)
returns @retcontactinformation table
(
-- columns returned by the function
funcid int primary key not null,
full_name nvarchar(75) null,
phone nvarchar(100) null,
email nvarchar(50) null,
city nvarchar(20) null
)
as
begin
insert @retcontactinformation
select
@funcid
, p.full_name
, c.phone
, c.email
, c.cityid
from d_people p, d_contacts c
where p.peopleid = @funcid;
return;
end;
go
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다