2017-12-31 34 views
0

iはTourId = 4のための法案としてだけで1つの行の結果を得るために習慣が、それは行数がTourIdである与え= 4関数を呼び出して1行結果を取得すると、行数が表示されます。なぜですか?ここで

--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を持つ複数の行がある場合、1つの行を取得するには、何らかの集約クエリを作成するか、where句にさらに制限を加えて1つの行しか返されないようにする必要があります。 – ADyson

+0

同じIDを持つ複数の行はありません。各ツアー@ADysonの合計コストは – rajitha

+0

です。あなたが「TourId = 4の行数があります」と以上です。したがって、私のコメント。 – ADyson

答えて

0

読みやすいコードを書くことを学びます。そして、あなたのコードをデバッグすることを学びます。

このパラメータは、現在、この機能を使用しようとする人を混乱させること以外には目的がありません。明らかに、リテラルをパラメータに置き換える必要があります。そして人類にとっては、あなたのFROM句にある馬鹿馬鹿しい括弧をすべて削除してください。これらの括弧は何の目的も果たさず、効果がなく、コードを読みやすく理解するだけです。

関連する問題