Created
August 30, 2017 02:11
-
-
Save andre-f-paggi/26c5bbf20129b5a4d62750b993746ccd to your computer and use it in GitHub Desktop.
Exemplo SubQuery MSSQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- <Tabela Feia de exemplo> | |
SELECT | |
'Caixa de canetas BIC' AS txt_produto, | |
'23765' AS txt_cod_prod, | |
CAST(1 as bit) AS bit_ativo | |
INTO #Tabela_Feia -- Tabela Temporária | |
INSERT INTO #Tabela_Feia (txt_produto, txt_cod_prod, bit_ativo) VALUES ('Elásticos', '12345', 0); | |
INSERT INTO #Tabela_Feia (txt_produto, txt_cod_prod, bit_ativo) VALUES ('Caderno', '23456', 1); | |
INSERT INTO #Tabela_Feia (txt_produto, txt_cod_prod, bit_ativo) VALUES ('Borracha', '34567', 0); | |
INSERT INTO #Tabela_Feia (txt_produto, txt_cod_prod, bit_ativo) VALUES ('Lápis', '45678', 1); | |
-- </Tabela Feia de exemplo> | |
-- Melhorar o nome das colunas com subselect | |
SELECT * FROM ( | |
SELECT | |
tf.txt_produto as 'Produto', | |
tf.txt_cod_prod as 'CodigoProduto', | |
tf.txt_cod_prod + ' - ' + tf.txt_produto as 'ColunaCustomizada' | |
FROM #Tabela_Feia tf | |
) TabelaComNomeBonito | |
WHERE ColunaCustomizada = '23456 - Caderno' -- Consigo usar nomes bonitos para regras complexas no where | |
-- Removo a tabela temporária | |
DROP TABLE #Tabela_Feia |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment