1
I have a consultation with N
relationships, in one of these related tables I need to select a specific column that follows the pattern mes_valor
, where mes
is an abbreviation for one of the possible twelve jan_valor
, fev_valor
, ..._valor
.
The SELECT
is something like:
SELECT [dbo.mes_valor(MONTH(tabela_relacionada.data_emissao))] as valor FROM tabela
Where from the date of issue, I will obtain the month and select the specific column to obtain the required value. dbo.mes_valor
is a simple function just to return the column name:
CREATE FUNCTION dbo.mes_valor (@mes INT)
RETURNS NVARCHAR(MAX)
AS BEGIN
DECLARE @mes_valor NVARCHAR(MAX)
SET @mes_valor = (SELECT case @mes
WHEN 1 THEN 'jan_valor'
WHEN 2 THEN 'fev_valor'
WHEN 3 THEN 'mar_valor'
WHEN 4 THEN 'abr_valor'
WHEN 5 THEN 'mai_valor'
WHEN 6 THEN 'jun_valor'
WHEN 7 THEN 'jul_valor'
WHEN 8 THEN 'ago_valor'
WHEN 9 THEN 'stm_valor'
WHEN 10 THEN 'out_valor'
WHEN 11 THEN 'nov_valor'
WHEN 12 THEN 'dez_valor'
END)
RETURN @mes_valor
END
While performing the SELECT
as demonstrated above, the SQL SERVER
interprets as a string
, keeping the static query, example:
VALOR
jan_valor
ago_valor
dez_valor
When to return the column value
VALOR
10.3
23.1
55.2
http://answall.com/questions/144285/pivot-sql-colunas-din%C3%A2micas-com-Vari%C3%A1vel/144313#144313
– Marco Souza
But in your answer is that the value is defined before the consultation, the value I need to pass is tied to the relationship, the column
tabela_relacionada.data_emissao
.– Marcelo de Andrade
When you call your FUNCTION dbo.mes_value (@mes INT) it will only return this, ie WHEN 1 THEN 'jan_value' instead of its 'jan_value' you would have to select to get the value
– Marco Souza