Possible Duplicate:
SQL Msg 8114, Level 16, State 5, Procedure sp_product_listing, Line 0 Error converting data type varchar to datetime
I’ve seen that this was asked a while ago by someone in another semester at my school, haha. But the answer they got didn’t really satisfy me. Here is the link to the other question: SQL Msg 8114, Level 16, State 5, Procedure sp_product_listing, Line 0 Error converting data type varchar to datetime.
Most of what the answer says makes not much sense to me as I am very new to SQL. If we could open up another discussion on this, it would be great. Especially minus the teacher bashing that has been going on in my questions lately…
The following statement completes successfully, BUT:
CREATE PROCEDURE sp_product_listing
(
@product varchar(30),
@month datetime,
@year datetime
)
AS
SELECT
'product_name' = products.name,
products.unit_price,
products.quantity_in_stock,
'supplier_name' = suppliers.name
FROM
suppliers
INNER JOIN
products ON suppliers.supplier_id = products.supplier_id
INNER JOIN
order_details ON products.product_id = order_details.product_id
INNER JOIN
orders ON order_details.order_id = orders.order_id
WHERE
products.name = @product
AND MONTH ('orders.order_date') = @month
AND YEAR ('orders.order_date') = @year;
GO
When I try and execute the procedure, I get an error message:
Msg 8114, Level 16, State 5, Procedure sp_product_listing, Line 0
Error converting data type varchar to datetime.
Here’s how I execute the stored procedure:
EXECUTE sp_product_listing @product = 'Jack%', @month = 'June', @year = 2001;
GO
I’ve tried changing the two datetime datatypes to varchars and that has not worked because the column itself is datetime datatype originally. I’m not sure what I’m missing here?
UPDATE:
This was the final solution as guided by https://stackoverflow.com/users/1554034/valex.
CREATE PROCEDURE sp_product_listing
(
@product varchar(30),
@month int,
@year int
)
AS
SELECT
'product_name' = products.name,
products.unit_price,
products.quantity_in_stock,
'supplier_name' = suppliers.name
FROM
suppliers
INNER JOIN
products ON suppliers.supplier_id = products.supplier_id
INNER JOIN
order_details ON products.product_id = order_details.product_id
INNER JOIN
orders ON order_details.order_id = orders.order_id
WHERE
products.name LIKE @product
AND MONTH (orders.order_date) = @month
AND YEAR (orders.order_date) = @year;
GO
Also, for anyone still reading, here is the solution my teacher gave me which gives the same results.
CREATE PROCEDURE sp_product_listing
(
@product varchar(30),
@month varchar(10),
@year char(4)
)
AS
SELECT
'product_name' = products.name,
products.unit_price,
products.quantity_in_stock,
'supplier_name' = suppliers.name
FROM
suppliers
INNER JOIN
products ON suppliers.supplier_id = products.supplier_id
INNER JOIN
order_details ON products.product_id = order_details.product_id
INNER JOIN
orders ON order_details.order_id = orders.order_id
WHERE
products.name LIKE @product
AND DATENAME(MONTH,orders.order_date) = @month
AND YEAR (orders.order_date) = @year;
GO
You get this error because of ‘June’ is not a
DATEIMEvalue and MONTH function returnsint. Also if you input @product as a MASK (with ‘%’) you should useLIKEinstead of=So you should declare your procedure as:And call it