'Write a Procedure, cursor to display cars

I have these tables:

  • Car (CarID, brand, Buyprice, YearManufacture, Color)
  • Client (ClientId, name, address, salary)
  • Vendor (VendorID, name)
  • Sale (CarID, ClientID, VendorID, SaleDate, SalePrice)

I have to write a procedure for displaying all cars. If car is of type 'Mercedes', show its purchase price and whether it is already sold or not.

This is my code:

CREATE PROCEDURE PD
AS
BEGIN
    DECLARE Crs CURSOR FOR 
         SELECT CarID, brand, Buyprice 
         FROM Car;

    DECLARE @Carid INT;
    DECLARE @Type varchar (100);
    DECLARE @Price INT;
    DECLARE @N INT;

    OPEN Crs;

    FETCH Crs INTO @Carid, @Type, @Price;

    WHILE @@Fetch_status = 0
        IF @Type = 'MERCEDES' 
        BEGIN
            PRINT @Price;

            SELECT @N = COUNT(*)
            FROM Sale
            WHERE CarID = @Carid;

            IF @N > 0  
            BEGIN
                PRINT 'Car already sold');
            ELSE
                PRINT 'Car is sold');
            END
        END 
    END;

    CLOSE Crs;
    DEALLOCATE Crs;

END;

Is it correct, or am I missing something?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source