Travel variable with a Store

0

I'm doing store procedure. But I have a doubt about the query that its results are two values. (@VarId)

I need to pass the two values in the second query, so that I can return the data that is not found in the tb_change

CREATE PROCEDURE sp_CambioSinAsignar
@IdProducto int

AS
BEGIN
    declare @varId int
    set  @varId= (SELECT IdCambio from tb_Producto_Cambio where IdProducto=@IdProducto)


    select distinct tb_Cambio.IdCambio, tb_Cambio.Nombre,tb_Cambio.Descripcion, 
    tb_Cambio.AproxHoras 
    from tb_Cambio
    right join 
    tb_Producto_Cambio on tb_Cambio.IdCambio=tb_Producto_Cambio.IdCambio
    where tb_Producto_Cambio.IdProducto<> @IdProducto and tb_Producto_Cambio.IdCambio<> 
    @varId
END
    
asked by magi0 28.10.2018 в 19:59
source

1 answer

0

In an int variable you can not save multiple values, you would need a table variable:

  declare @varId table (id int)
insert into @varId
SELECT IdCambio from tb_Producto_Cambio where IdProducto=@IdProducto


select distinct tb_Cambio.IdCambio, tb_Cambio.Nombre,tb_Cambio.Descripcion, 
tb_Cambio.AproxHoras 
from tb_Cambio
right join 
tb_Producto_Cambio on tb_Cambio.IdCambio=tb_Producto_Cambio.IdCambio
where tb_Producto_Cambio.IdProducto<> @IdProducto and 
tb_Producto_Cambio.IdCambio not in (select id from @varId)

I hope it serves you.

    
answered by 29.10.2018 в 10:59