Search code examples
sqlsql-serverstored-proceduressql-like

SQL Procedure with LIKE


I am trying to learn more about procedures in SQL, and so i am trying to do this:

CREATE PROCEDURE GetCarInformation
(@ModelName Varchar(256))
AS
Begin
SELECT Car.LicensePlate, CarBrand.ManufacturerName, CarModel.ModelName, CarSpecs.ModelYear, CarSpecs.Doors, CarSpecs.Seats, CarSpecs.ModelYear
FROM Car
INNER JOIN CarModel ON Car.ModelID = CarModel.ID
INNER JOIN CarBrand ON CarModel.ManufacturerID = CarBrand.ID
INNER JOIN CarSpecs ON CarModel.CarSpecsID = CarSpecs.ID
WHERE CarModel.ModelName LIKE '%@ModelName%'
END

But it doesn't seem to work, on the LIKE part of it. It wont search correctly. Is it because i am using it incorrectly ? Or can't i add % with the @ModelName ? and single quotes ?


Solution

  • You should use

    LIKE '%'+@ModelName+'%'
    

    It's the way MSSQL use to CONCAT string . You can use CONCAT() too.

    Please remember to use LIKE '%xxx%' with caution. It can have really bad effects on performances.