Concatenate SQL Server records

Asked

Viewed 370 times

0

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

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 turn the query, case FormID exists and Permitir is equal to 0, it returns nothing.

In that case I must return FormID regardless of the value of Permitir, but case Permitir be 1 he has to concatenate all records where Permitir be 1.

  • Are these three fields from the same table? If yes, they no longer return on the same query line?

  • Yes yes, they are from the same table. I need to return FormID regardless of the value of Permitir, in my query just return FormID case Permitir be 1

  • but you are declaring it in the Where: "AND Allow" = '1', if the value of allow does not matter, just take it out of the clause

  • Is that my frontend displays the result only of assets (when Permitir is equal to 1)

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 

This is what you need?

  • More or less this, I need to concatenate the values, Ex: Formid = Portfolios Acaoid = Print; View

Browser other questions tagged

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