Can someone help me create a stored procedure from the following query.
SELECT MakeName, ModelName
FROM Data.Make AS MK
INNER JOIN Data.Model AS MD
ON MK.MakeID = MD.MakeID
INNER JOIN Data.Stock AS ST
ON ST.ModelID = MD.ModelID
WHERE DateBought BETWEEN '2018-08-30' AND '2018-08-31'
I would like variables to represent the between dates e.g @FromDate and @ToDate. I would like to include the following variables to represent MakeName and ModelName.
The query is as follows:
SELECT
MK.MakeName
,MD.ModelName
,ST.DateBought
FROM Data.Make MK
INNER JOIN Data.Model MD
ON MK.MakeID = MD.MakeID
INNER JOIN Data.Stock ST
ON ST.ModelID = MD.ModelID
WHERE ST.DateBought BETWEEN '2018-08-30' AND '2018-08-31'
The following is sample data to work with:
CREATE TABLE #tmpTable (
MakeName nvarchar(100),
ModelName nvarchar(150),
DateBought date)
INSERT #tmpTable VALUES
(N'Bentley',N'Mulsanne',CONVERT(DATETIME, '2018-08-31', 120)),
(N'Bentley',N'Continental',CONVERT(DATETIME, '2018-08-31', 120)),
(N'Bentley',N'Flying Spur',CONVERT(DATETIME, '2018-08-31', 120))
SELECT * FROM #tmpTable
Thanks