简体   繁体   中英

Two select statements in stored procedure

I have the following stored procedure:

create procedure Get_CarInfo
@PlateNo nvarchar(10)
as
select Administration.City AS AdCity 
from Administration 
where Administration.AdministrationNo = (select Car.AdministrationNo from Car WHERE car.PlateNo = @PlateNo)

select car.Brand, car.model, car.Color, car.AdministrationNo, car.InsuranceNo, car.RegistrationExp
from Car
where car.PlateNo = @PlateNo

What I would like to do is to retrieve the car information (brand, color, .. etc) but the problem in the Car table there is administration number field and I would like to retrieve the administratrion city based on the administration no in the administration table.

Here is the table of Car:

PlateNo - Brand - Color - Model - AdministrationNo - InsuranceNo - RegistrationExp

The Administration table:

AdministrationNo - City

I put two select statements but it didn't work. Any suggestions please !!

You do not need a procedure to achieve this, the following query will do :

select 
    car.Brand, 
    car.model, 
    car.Color, 
    car.AdministrationNo, 
    car.InsuranceNo, 
    car.RegistrationExp,
    Administration.City
from Car
left join Administration
    ON Administration.AdministrationNo = Car.AdministrationNo
where car.PlateNo = xxx

Where xxx is the car plate number

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM