In that case, you can create a FUNCTION that returns a string with the name of the colors passing the IDS that you want, as follows.
create FUNCTION StringListToTexto
(
@List varchar(MAX)
)
RETURNS varchar(max)
AS
BEGIN
DECLARE @item varchar(800), @Pos int
SET @List = LTRIM(RTRIM(@List))+ ','
SET @Pos = CHARINDEX(',', @List, 1)
declare @texto varchar(max) = '';
---- sua tabela substituar por sua tabela
declare @tbl_coresAux TABLE (id INT, cor VARCHAR(50))
INSERT INTO @tbl_coresAux VALUES
(1 ,' azul'),
(2 ,' verde'),
(3 ,' amarelo'),
(4 ,' vermelho')
-----
WHILE @Pos > 0
BEGIN
SET @item = LTRIM(RTRIM(LEFT(@List, @Pos - 1)))
IF @item <> ''
BEGIN
declare @IdAux int = (CAST(@item AS int));
set @texto = @texto + (Select cor from @tbl_coresAux where id = @IdAux);
END
SET @List = RIGHT(@List, LEN(@List) - @Pos)
SET @Pos = CHARINDEX(',', @List, 1)
END
RETURN @texto
END
And you’d call it like this.
declare @tbl_cores TABLE (id INT, cor VARCHAR(50))
INSERT INTO @tbl_cores VALUES
(1 ,' azul'),
(2 ,' verde'),
(3 ,' amarelo'),
(4 ,' vermelho')
declare @tbl_mesa TABLE (id INT, cores VARCHAR(50))
INSERT INTO @tbl_mesa VALUES
(1 ,'2,4'),
(2 ,'1,3'),
(3 ,'2,3,4,1')
select id, ([dbo].[StringListToTexto] (cores)) from @tbl_mesa
where id = 2
You mean the color column is to type string/varchar?
– Sergio
You want the table id tbl_cores for = 1 it returns the 2.4 colors of table tbl_mesa. the comparison of these tables are the ids themselves?
– Marco Souza
yes, sweep...
– Marcio Roberto Zacko
tbl_mesa brings the tbl_cores ID. Thanks for any help.
– Marcio Roberto Zacko
I tried to help, I think I can
– Pedro Luzio