Return fields from parameters

Asked

Viewed 57 times

2

Good night,

I need the return of a SELECT to be dependent on the parameter passed in SELECT

Example declare @variavel nvarchar(50)

Select peasant, campob, campoc, campod, peasant,

from table

I need to get:

@variaval = field return: select field, campob from table

if

@variable = field return: select campob, campoc, campod from table

How to make that logic?

  • This is done through the programming language that will call the bank. Each language has a way and each bank has a way. What is your bank and language you are using ?

  • @Good morning...thanks for the reply By the rule the solution should be made via sql server database

1 answer

1

I believe this is what you need.

DECLARE @Variavel AS VARCHAR(MAX); DECLARE @ComandoSQL AS VARCHAR(MAX);
SET @Variavel = 'campoa';

IF (@Variavel = 'campoa')
    BEGIN 
        SET @ComandoSQL = 'SELECT campoa, campob, campoc, campod, campoe FROM Tabela' 
    END 
ELSE IF (@Variavel = 'campob')
    BEGIN 
        SET @ComandoSQL = 'SELECT campob, campoc, campod FROM Tabela'
    END 

/*Mostra o comando sql gerado*/
SELECT @ComandoSQL

/*Executa o comando sql gerado*/
EXEC (@ComandoSQL)

Browser other questions tagged

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