2017-12-31 113 views
0

은 내가 TourId = 4 청구서와 같은 하나의 행 결과를 얻을 수 실 거예요,하지만 것입니다 행 수는 TourId와가 제공 = 4함수를 호출하여 한 행 결과를 얻으면 행 수가 늘어납니다. 이유는 무엇입니까? 여기에서

--Functions 
--(1)Tour Bill 
-- when enter a TourID it will genarate Tour Cost 
--Multi statement table valued functions 


CREATE FUNCTION dbo.Tour_Bill(@TourID int) 
RETURNS @Bill TABLE 
( TourID int, 
    ItineraryID int, 
    StartDate date, 
    EndDate date, 
    Duration int, 
    Distance float, 
    CustomerId varchar(30) 
    Name varchar(180) NOT NULL, 
    ContractNo varchar(12) NOT NULL, 
    GuideID int, 
    PaymentForGuide money, 
    SpecielActivityCost money, 
    VisitingPlaceTicketCost money, 
    NumberOfPeople int, 
    CostForMeal money, 
    Accomadation varchar(100), 
    TotalAccommodationCost money, 
    TourPackegeCost money, 
    TotalSpecielActivityCost money, 
    TotalVisitingPlaceTicketCost money, 
    GRAND_COST money 
) 
AS 
BEGIN 

INSERT INTO @Bill 
     SELECT Tour.TourId, 
        Itinerary.ItineraryId, 
        StartDate, 
        EndDate, 
        DATEDIFF(day,StartDate,EndDate) AS Duration, 
        EstTravelDist, 
        CustomerId, 
        FirstName+' '+LastName AS FullName, 
        ContactNo, 
        Guide.IdNo, 
        CAST(500 * DATEDIFF(day,StartDate,EndDate) AS money) AS PaymentForGuide, 
        SpecialActivity.Cost AS SpecialActivityCost, 
        VisitingPlaces.Cost AS VisitingPlacesCost, 
        NumberOfPeople, 
        CAST(UnitPrice * DATEDIFF(day,StartDate,EndDate) * NumberOfPeople AS money) AS CostForMeal, 
        Location AS Accomadation, 
        CAST(UnitPrice * DATEDIFF(day,StartDate,EndDate) AS money) AS TotalAccommodationCost, 
        CAST(EstTravelDist * 40 AS money) AS TourPackegeCost, 
        SpecialActivity.Cost * NumberOfPeople AS TotalSpecielActivityCost, 
        VisitingPlaces.Cost * NumberOfPeople AS TotalVisitingPlaceTicketCost, 
        CAST(EstTravelDist * 40 + SpecialActivity.Cost * NumberOfPeople + VisitingPlaces.Cost * NumberOfPeople + 500 * DATEDIFF(day,StartDate,EndDate) 
        + UnitPrice * DATEDIFF(day,StartDate,EndDate) + UnitPrice * DATEDIFF(day,StartDate,EndDate) * NumberOfPeople AS money) AS GRAND_COST 

    FROM (((((((((Itinerary 
      INNER JOIN SpecialActivity ON 
      Itinerary.ItineraryId = SpecialActivity.ItineraryId) 
      INNER JOIN VisitingPlaces ON 
      VisitingPlaces.ItineraryId = Itinerary.ItineraryId) 
      INNER JOIN Tour ON 
      Tour.TourId = Itinerary.TourId) 
      INNER JOIN Guide ON 
      Guide.TourId = Tour.TourId) 
      INNER JOIN Vehicle ON 
      Vehicle.TourId = Tour.TourId) 
      INNER JOIN Accommodation ON 
      Accommodation.TourId = Tour.TourId) 
      INNER JOIN Participant ON 
      Participant.TourId = Tour.TourId) 
      INNER JOIN Person ON 
      Person.IdNo = Guide.IdNo) 
      INNER JOIN Contract ON 
      Itinerary.ItineraryId = Contract.ItineraryId) 
    WHERE Tour.TourId = 4 
RETURN; 
END 

--How to Call the Function 

SELECT * FROM dbo.Tour_Bill(101) 

---These are the tables 
create table Itinerary(
    ItineraryId int primary key identity, 
    Description varchar(50)NOT NULL, 
    TourId int, 
    CONSTRAINT FK_Tour3 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId) 
); 
--ALTER TABLE ADD COLUMN Distance 
ALTER TABLE Itinerary 
ADD EstTravelDist float NOT NULL 

create table SpecialActivity(
    Activity varchar(100), 
    ItineraryId int NOT NULL, 
    Dates date , 
    PRIMARY KEY(Activity,ItineraryId,Dates),  
    CONSTRAINT FK_Dates3 FOREIGN KEY (Dates,ItineraryId) 
    REFERENCES Dates(Dates,ItineraryId),   
); 
---ADD COLUMN Cost 
ALTER TABLE SpecialActivity 
ADD Cost money NOT NULL 

create table VisitingPlaces(
    Place varchar(100), 
    ItineraryId int, 
    Dates date ,  
    PRIMARY KEY(Place,ItineraryId,Dates),  
    CONSTRAINT FK_Dates5 FOREIGN KEY (Dates,ItineraryId) 
    REFERENCES Dates(Dates,ItineraryId),   
); 
---ADD COLUMN Cost 
ALTER TABLE VisitingPlaces 
ADD Cost money NOT NULL 

create table Person(
    IdNo varchar(30) primary key, 
    FirstName varchar(50) NOT NULL , 
    LastName varchar (100) NOT NULL, 
    Gender char(1) NOT NULL, 
    Nationality varchar (50) NOT NULL, 
    Email varchar(100) UNIQUE, 
    Fax varchar(12) , 
    PostBox varchar (6) NOT NULL, 
    Street varchar (60) NOT NULL, 
    City varchar (60) NOT NULL, 
    Province varchar (60) NOT NULL, 
    Country varchar(100) NOT NULL  
); 

create table Tour(
    TourId int PRIMARY KEY IDENTITY, 
    ResavationDate date NOT NULL, 
    StartDate date NOT NULL, 
    EndDate date NOT NULL, 
    NumberOfPeople int NOT NULL, 
    TourStatus varchar(20) NOT NULL, 
    CustomerId varchar(30), 
    CONSTRAINT FK_Person3 Foreign key (CustomerId) 
    REFERENCES Person(IdNo) 
); 


create table Guide(
    IdNo VARCHAR(30) PRIMARY KEY , 
    GovermentRegistrationNo varchar(10) NOT NULL UNIQUE, 
    YearsOfExperence int NOT NULL, 
    TourId int, 
    CONSTRAINT FK_Tour2 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId), 
    CONSTRAINT FK_Person4 FOREIGN KEY (IdNo) 
    REFERENCES Guide(IdNo) 
); 

create table Participant(
    ParticipantId varchar(30) primary key, 
    Name varchar(50) NOT NULL, 
    ContactNo varchar(30) NOT NULL, 
    PickupLocation varchar(50) NOT NULL, 
    DropLocation varchar(50) NOT NULL, 
    TourId int 
    CONSTRAINT FK_Tour FOREIGN KEY (TourId) REFERENCES Tour(TourId) 
); 

create table Accommodation(
    ContractNo int primary key , 
    Location varchar (50) NOT NULL, 
    Class varchar(10), 
    RentedRoomFacilitiesDescription varchar(1000), 
    PvtRoomFacilitiesDescription varchar(1000), 
    T1 bit, 
    T2 bit, 
    T3 bit, 
    Dates date, 
    ItineraryId int, 
    TourId int, 
    CONSTRAINT Fk_Dates FOREIGN KEY (Dates,ItineraryId) 
    REFERENCES Dates(Dates,ItineraryId),  
    CONSTRAINT FK_Tour4 FOREIGN KEY (TourId) 
    REFERENCES Tour(TourId), 
); 

create table Contract(
    ContractNo int primary key IDENTITY, 
    PricingCriteria varchar(100), 
    UnitPrice money, 
    MiniPeople int, 
    MaxPeople int, 
    ContractType varchar(100), 
    FinancialPenalty money, 
    ItineraryId int NOT NULL, 
    SupplierId int, 
    CONSTRAINT FK_Itinerary2 FOREIGN KEY (ItineraryId) 
    REFERENCES Itinerary(ItineraryId), 
    CONSTRAINT FK_SupplierRegNo FOREIGN KEY (SupplierId) REFERENCES Supplier(SupplierRegNo) 
); 
+0

여행 ID가 동일한 행이 여러 개있는 경우 원하는 항목은 무엇입니까? 그 여행을위한 몇 가지 필드의 합계. 아님, 아니면? 귀하의 요구 사항이 무엇인지, 그리고 바람직하게는 귀하가 원하는 결과물을 제시하십시오. 동일한 ID를 가진 여러 행이있는 경우 한 행을 가져 오려면 일종의 집계 쿼리를 만들거나 where 절에 추가 제한을 만들어 한 행만 반환 될 수 있도록해야합니다. – ADyson

+0

동일한 ID를 가진 행이 여러 개 없으며 각 Tour @ADyson의 총 비용을 얻지 않을 것입니다. – rajitha

+0

위에서 "TourId = 4 인 행 수가 있습니다."라고 말했습니다. 그러므로 내 의견. – ADyson

답변

0

읽을 수있는 코드를 작성 알아보세요. 그런 다음 코드를 디버깅하는 방법을 배우십시오. 귀하의 기능에 다음이 포함되어 있습니다.

WHERE Tour.TourId = 4 

매개 변수는 현재이 기능을 사용하려고 시도하는 사람을 혼동시키는 것 이외의 목적을 제공하지 않습니다. 분명히 리터럴을 매개 변수로 대체해야합니다. 그리고 인류애를 위해서, FROM 절에있는 말도 안되는 괄호를 모두 제거하십시오. 아무런 효과가 없으며 효과가 없으며 코드를 읽고 이해하기가 더 어렵습니다.