How to return a SQL with result?

1

I need to select multiple tables bound by the "user id" and return the data to compose a profile page, however some of those tables may be empty at first, ie the query will not find the "id user "in one of them and will return an error or no data. How can I select multiple tables by returning the query regardless of the data found or not?

    
asked by anonymous 15.07.2016 / 14:06

1 answer

2

I have an example of a similar situation that I have experienced. You should use left join :

SELECT PERMISSAO.Codigo CODIGO,
       PERMISSAO.Descricao DESCRICAO,
       ISNULL(permissaoUsuario.acesso, 'N') ACESSO
  FROM permissao 
  LEFT JOIN permissaoUsuario ON PERMISSAO.Codigo = permissaoUsuario.codpermisao
   AND permissaoUsuario.codusuario = @CODUSUARIO
 WHERE PERMISSAO.DATAEXCLUSAO IS NULL

In this example I used left join (bring from the left) to bring the permissions of the PERMISSAO table even though the record is not inserted in the PERMISSAOUSUARIO

    
15.07.2016 / 14:24