Trying to using parts of datetime as a variable in a procedure, so a parameter would be a month like ‘June’. Here’s what I wrote
/* 3. Create a stored procedure called sp_product_listing listing a specified product ordered during a specified month and year. The product and the month and year will be input parameters for the stored procedure. Display the product name, unit price, and quantity in stock from the products table, and the supplier name from the suppliers table. Run the stored procedure displaying a product name containing Jack and the month of the order date is June and the year is 2001. The stored procedure should produce the result set listed below.*/
CREATE PROCEDURE sp_product_listing
(
@product varchar(40),
@month datetime,
@year datetime
)
AS
SELECT
'product_name'=products.name,
'unit_price'=products.unit_price,
'quantity_in_stock'=products.quantity_in_stock,
'supplier_name'=suppliers.name
FROM
products
INNER JOIN suppliers ON suppliers.supplier_id=products.supplier_id
INNER JOIN order_details ON order_details.product_id=products.product_id
INNER JOIN orders ON orders.order_id=order_details.order_id
WHERE
products.name LIKE '%@product%' AND MONTH(orders.order_date) = @month AND YEAR(orders.order_date) = @year;
GO
/*Execute procedure*/
EXECUTE sp_product_listing 'Jack','June','2001'
Procedure is tested working fine until I add the variables, then it goes to H trying to convert varchar to datetime?
I’ve tried things like @month MONTH(datetime), etc. Not sure how to approach this? Maybe that’s not even the problem?
You should not make “@month” datetime because, as its nametype implies, it expects data on the form “date and time” like YYYY/MM/DD hh:mm:ss, and “June” does not fits on the pattern I just presented. If you want just make one column for dates, let’s call it [dateofsomething], then, you can pass a parameter “@date” (with type datetime) with a value like a normal date.
Of course, if you want the current date, just use getdate()
Good luck