How can I do a Select and an Update in the same query?

0

I have 2 tables:

Users [Id, UserType, Email, Password]

Clients [Id, TypeUser, Email, Password, UltimaSesion]

I have created a login query through the Email and password looking in both tables to know if it is a User of the company or a client:

SELECT Usuario.Id, Usuario.TipoUsuarioId, CONCAT(Usuario.Nombre, ' ', Usuario.Apellidos) AS UsuarioNombre, 
Usuario.TipoPerfilId, TipoUsuario.PaginaInicio 
FROM Usuario INNER JOIN TipoUsuario
ON Usuario.TipoUsuarioId = TipoUsuario.Id
WHERE Usuario.Email = @Email AND Usuario.Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS
AND Usuario.Status = 1
UNION ALL
SELECT Cliente.Id, Cliente.TipoUsuarioId, CONCAT(Cliente.Nombre, ' ', Cliente.Apellidos) AS UsuarioNombre, 
'0' AS TipoPerfilId, TipoUsuario.PaginaInicio 
FROM Cliente INNER JOIN TipoUsuario
ON Cliente.TipoUsuarioId = TipoUsuario.Id
WHERE Cliente.Email = @Email AND Cliente.Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS
AND Cliente.Status >= 3

Now, what I need is to update the client table to set the current date in its "UltimaSesion" field.

Is it possible to do this within my current query?

    
asked by Ivan.Enriquez 18.08.2017 в 02:43
source

2 answers

3

As you are trying to do it is not possible in the same query to do an update of the table, then it occurs to me that you can open the query in two parts by placing the results in a temporary table and in the end make a call to said table , so in the query part of the client you can update the field that you need to update. the possible solution would be something like this:

Set Nocount on

SELECT Cliente.Id, Cliente.TipoUsuarioId, CONCAT(Cliente.Nombre, ' ', Cliente.Apellidos) AS UsuarioNombre, 
        '0' AS TipoPerfilId, TipoUsuario.PaginaInicio 
Into #tmpLogin
    FROM Cliente 
        INNER JOIN TipoUsuario ON Cliente.TipoUsuarioId = TipoUsuario.Id
    WHERE Cliente.Email = @Email AND Cliente.Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS AND Cliente.Status >= 3
if @@rowcount>=1
begin
    Update Cliente set UltimaSesion=1 
        WHERE Cliente.Email = @Email AND Cliente.Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS AND Cliente.Status >= 3
end
else
begin
    SELECT Usuario.Id, Usuario.TipoUsuarioId, CONCAT(Usuario.Nombre, ' ', Usuario.Apellidos) AS UsuarioNombre, 
            Usuario.TipoPerfilId, TipoUsuario.PaginaInicio 
    Into #tmpLogin
    FROM Usuario 
        INNER JOIN TipoUsuario ON Usuario.TipoUsuarioId = TipoUsuario.Id
    WHERE Usuario.Email = @Email AND Usuario.Password = @Password COLLATE SQL_Latin1_General_CP1_CS_AS AND Usuario.Status = 1
end

Select * from #tmpLogin
    
answered by 18.08.2017 / 13:38
source
0

You could create an SQL function (in the database engine that you are using) that performs both actions, perform the user's selection and if the user is performing the update. Also, return some true or false value, if the login was successful. Greetings.

    
answered by 18.08.2017 в 15:58