How to run an INSERT procedure with the GETDATE field? SQL Server
Next, I created a procedure that has two parameters Dt_inclusion and Dt_Alteracao, I would like to know how do I insert (or if it is possible to use) a getdate to get the current date, instead of having to always manually type A as for example ('2020-05-07')
Below is the Execute of the procedure.
EXEC [dbo].[sp_CRUDLogin]
@ID = 10,
@Usuario = N'Teste',
@Senha = N'123456',
@Nome = N'Teste',
@Email = N'[email protected]',
@Telefone = N'8002-4922',
@Dt_inclusao = '2020-05-07' <-- esse campo gostaria de utilizar um GETDATE no lugar da data especificada.
@Dt_alteracao = NULL,
@StatementType = N'Insert'
My procedure:
ALTER PROCEDURE [dbo].[sp_CRUDLogin]
(
@ID integer,
@Usuario varchar(60),
@Senha varchar(60),
@Nome varchar(60),
@Email varchar(60),
@Telefone varchar(15),
@Dt_inclusao datetime,
@Dt_alteracao datetime,
@StatementType nvarchar(20)='')
AS
BEGIN
IF @StatementType='Insert'
BEGIN
INSERT INTO TbCADLogin
(ID,
Usuario,
Senha,
Nome,
Email,
Telefone,
Dt_inclusao,
Dt_alteracao)
VALUES (@ID,
@Usuario,
@Senha,
@Nome,
@Email,
@Telefone,
@Dt_inclusao,
@Dt_alteracao)
END
IF @StatementType='Select'
BEGIN
SELECT *
FROM TbCADLogin
END
IF @StatementType = 'Update'
BEGIN
UPDATE TbCADLogin
SET Usuario = @Usuario,
Senha = @Senha,
Nome = @Nome,
Email = @Email,
Telefone = @Telefone,
Dt_inclusao = @Dt_inclusao,
Dt_alteracao = @Dt_alteracao
WHERE ID = @ID
END
ELSE IF @StatementType='Delete'
begin
delete from TbCADLogin
where ID = @ID
END
END
Attempt to run with GETDATE:
EXEC [dbo].[sp_CRUDLogin]
@ID = 10,
@Usuario = N'Teste',
@Senha = N'123456',
@Nome = N'Teste',
@Email = N'[email protected]',
@Telefone = N'8002-4922',
@Dt_inclusao = GETDATE, <--- GETDATE definido em vez do '2020-05-07'
@Dt_alteracao = NULL,
@StatementType = N'Insert'
When trying to use GETDATE I get the next result
"Msg 8114, Level 16, State 5, Procedure dbo.sp_CRUDLogin, Line 0 [Batch Start Line 0]
Erro ao converter tipo de dados nvarchar em date.
Completion time: 2020-05-07T15:22:07.2551684-03:00"
What should I do for this procedure to work, so that with just GETDATE I get the current date and time when using insert?
Thank you right now!
1 answers
I managed to solve, put in place of the Dt_inclusion in the procedure the GETDATE like this
BEGIN
INSERT INTO TbCADLogin
(ID,
Usuario,
Senha,
Nome,
Email,
Telefone,
Dt_inclusao,
Dt_alteracao)
VALUES (@ID,
@Usuario,
@Senha,
@Nome,
@Email,
@Telefone,
GETDATE(),
@Dt_alteracao)
And can do the same in UPDATE:
UPDATE TbCADLogin
SET Usuario = @Usuario,
Senha = @Senha,
Nome = @Nome,
Email = @Email,
Telefone = @Telefone,
Dt_inclusao = GETDATE(),
Dt_alteracao = @Dt_alteracao
WHERE ID = @ID
When running the procedure only leave the Dt_inclusion field blank:
EXEC @return_value = [dbo].[sp_CRUDLogin]
@ID = 12,
@Usuario = N'Riptde',
@Senha = N'123456',
@Nome = N'Ronnan',
@Email = N'[email protected]',
@Telefone = N'212354632',
@Dt_inclusao = N'',
@Dt_alteracao = NULL,
@StatementType = N'Insert'
With this, it worked :)