EDIT Thank you for all your help.
First thing, I have read tons of post and question of people having this same error, mines varies for int and bigint but the error stays the same, I can not insert or update. I am working with Microsoft SQL Server 2017 Management Studio and I've having this problem for the last two to three days, I know there are tons of answers I just don't know the F is wrong.
This is my stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[myproc]
(@name VARCHAR(50),
@ID BIGINT,
@birthdate DATETIME,
@Enabled BIT,
@married BIT,
@employees INT,
@hiredDate DATETIME)
AS
BEGIN
SET NOCOUNT ON
DECLARE @sql AS NVARCHAR(MAX)
SET @sql ='IF EXISTS(SELECT * FROM MyTable WHERE MyTable.ID ='+@ID+')
UPDATE MyTable
SET
name ='+@name+'
,birthdate='+@birthdate+'
,Enabled ='+@Enabled+'
,married='+@married+'
,employees='+@employees+'
,hiredDate='+@hiredDate+'
where ID = '+@ID+'
ELSE
INSERT INTO MyTable(name,ID,birthdate,Enabled,married,employees,hiredDate)VALUES(
'+@name+','+@ID+','+convert(varchar,@birthdate,20)+','+@Enabled+','+@married+','+@employees+','+convert(varchar,@hiredDate,20)+')'
EXEC (@SQL)
END
I am sick of this damn error and I am losing it.
Msg 8114, Level 16, State 5, Procedure testinsert, Line 0 [Batch Start Line 0]
Error converting data type varchar to datetime.
I made this little table just so i could ask and try to make this trial
I run this stored procedure with:
DECLARE @return_value INT
EXEC @return_value = testinsert
@name = 'asd',
@ID = 25,
@birthdate = '2018-12-10 16 45 00',
@Enabled = 1,
@married = NULL,
@employees = NULL,
@hiredDate = NULL
SELECT 'Return Value' = @return_value
GO
Table:
[dbo].[MYDTABLE]
([name]
,[ID]
,[birthdate]
,[Enabled]
,[married]
,[employees]
,[hiredDate])
VALUES
(<name, varchar(50),>
,<ID, bigint,>
,<birthdate, datetime,>
,<Enabled, bit,>
,<married, bit,>
,<employees, int,>
,<hiredDate, datetime,>)
GO
sp_executesqland pass the values in as parameters. Then you won't have type problems with dynamic SQL. It is unclear why you are using dynamic SQL here in the first place. This looks like regular SQL -- in fact, regular SQL that might be replaced with a singleMERGEstatement.