Stored Procedure for log data insertion

Asked

Viewed 619 times

3

I need to create a Stored Procedure in my SQL Server database. The purpose of this Stored Procedure is to record data in a Log table, which has three fields: date, time, ID of who logged in.

Date and time would be the time of login, while the Login ID comes from a table of logins. The site will already have the time to call this Stored Procedure at login.

The big question is what code I put in the Stored Procedure, since I hardly work with it in SQL Server. Who can help in this?

  • Are you having trouble creating one or calling in the application? Here’s a nice tutorial on how to create. http://imasters.com.br/artigo/223/sql-server/criacao-e-uso-de-stored-procedures

  • To create even. In the application, a way has been made to call the stored.

  • Got it, the tutorial I gave you helps a lot. It’s worth a study in it.

2 answers

0


Basically you just need the following.

CREATE Proc [dbo].[sp_LogUsuario_Update]
  @IdUsuario int,
  @DtInclusao int
  -- se precisa de mais campo adicione aqui.
as
begin
  Set Xact_Abort on
  Set Nocount on
  Begin Tran

  begin
     -- informe os campos que você que inseri na sua tabela
    Insert into tb_LogUsuario( IdUsuario,  DtInclusao) values
     ( 
        --- aqui você passa seus campos caso a data seja a hora local user o GETDATE() , o tipo de campo  da tabela tem 
        -- que ser DAteTime
        @IdUsuario, @DtInclusao 
     )

  end

  Commit
end
GO

Change what is necessary according to your need.

0

CREATE PROCEDURE [dbo].[sp_LogUsuario]
(
  @id int,
  @data int
)

AS
BEGIN
---------
-- LOG --
---------
DECLARE @txtransacao VARCHAR(1000) = '@data=' + CONVERT(VARCHAR(200), ISNULL(GETDATE(), '')) + ',@id=' + CONVERT(VARCHAR(100), ISNULL(@id, 0))

INSERT INTO log_acaoUsuario (log_txmensagem)
VALUES (@txtransacao) -- TENS QUE CRIAR ESSA TABELA COM ESSA COLUNA (qualquer nome)

---------------
-- VARIÁVEIS --
---------------

------------------
-- PROCEDIMENTO --
------------------

Now just do

SELECT * FROM log_acaoUsuario

And there’s the log

Browser other questions tagged

You are not signed in. Login or sign up in order to post.