Concatenate SQL Server records

0

I have a problem in a query, where I need to concatenate records from a table. I need select to take the FormID and concatenate the records of AcaoID only if Permitir is 1. p>

Query:

Declare @Result varchar(MAX) 
Declare @FormID varchar(MAX) 
Set @Result = ''
Select @FormID = FormID, @Result = COALESCE(@Result + AcaoID + '; ', '') FROM PermissaoAcoesForms 
where GrupoUsuario = '{0}' AND FormID = '{1}' AND Permitir = '1'
if  @Result <> '' Begin    
Set @Result = SUBSTRING(@Result, 1, LEN(@Result) - 1) end 
Select @FormID as FormID, @Result as AcaoID

The problem is that when I scroll to query , if FormID exists and Permitir equals 0, it returns nothing.

In this case I need to return FormID regardless of the value of Permitir , but if Permitir is 1 it has to concatenate all records where Permitir is 1.

    
asked by anonymous 28.06.2016 / 16:01

1 answer

0

When you want to make conditions in SQL use CASE

Select CASE WHEN Permitir = '1' THEN CONCAT(FormID, AcaoID) ELSE FormID END 
FROM PermissaoAcoesForms 

Is this what you need?

    
28.06.2016 / 16:33