I’m fairly new to SQL and was trying to write a procedure that would check a passed in value and set a local datetime variable and then return that variable.
USE [MyDB]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Common].[Update_Date]
@Status_ID int
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Date_Value DATETIME
IF @Status_ID = 2
SET @Date_Value = GETDATE()
ELSE
SET @Date_Value = NULL
RETURN @Date_Value
END
GO
When I try to execute this script I get the following error:
Msg 257, Level 16, State 3, Procedure Update_Date, Line 19
Implicit conversion from data type datetime to int is not allowed. Use the CONVERT function to run this query.
Is it trying to do something with my @Status_ID parameter?
Stored procedures return values by using OUTPUT parameters:
When invoked from ADO.Net, you use
ParameterDirection.Output. From T-SQL you invoke with an OUTPUT clause:In general is better not to mix procedure output with the return and with the result set (so that would be a vote against most other recommendation you got to use
SELECT). Using OUTPUT makes procedures reusable from other T-SQL code, using result set (SELECT) makes it much harder to use as T-SQL has problems capturing the result set of an invoked procedure (you’d have to use INSERT … SELECT and deal with all the problems that has).