ERROR SUBCONSULTA RETURNS MORE THAN ONE VALUE

0

Hello, I need help with this topic, in the work they have left me a project, to enter users through an excel file and that by selecting a user already registered in the database all their permissions pass to the new users, my problem is when I want to duplicate the permissions of that user in another table, I do not know if they understand me, here is the query that I used:

INSERT INTO tabla1 /*tabla*/ 
            (wgru_id, 
             wusr_id, 
             wcli_cod, 
             wclte_id, 
             wcli_fecha, 
             wcli_default)/*campos a los que insertare*/ 
SELECT wgru_id, 
       (SELECT TOP (@cantidad_filas) wusr_id 
        FROM   tabla2 
        ORDER  BY wusr_id DESC), 
       /*estos son los id que quiero recuperar de la otra tabla*/ 
       wcli_cod, 
       wclte_id, 
       Getdate(), 
       wcli_default 
FROM   tabla1 
WHERE  wusr_id = @usuario_seleccionado 

In this query I want to make one or more duplicates of a record just by changing the field wusr_id, the duplicates depend on the new records that I have inserted, I need to take out all the IDs (wusr_id) and make the query.

It returns the following error

Msg 512, Level 16, State 1, Procedure Mant_spDoubleClient_WordRecords, Line 8 [Batch Start Line 69] Subquery returned more than 1 value. This is not allowed when the subquery follows =,! =, > = or when the subquery is used as an expression.

I hope you can help me, I am new to this online question, GREETINGS.

    
asked by JiMel 16.09.2018 в 17:04
source

2 answers

0

I'm really not sure I understand the question ...

But good I think you would need this,

    INSERT INTO tabla1 /*tabla*/ 
                (wgru_id, 
                 wusr_id, 
                 wcli_cod, 
                 wclte_id, 
                 wcli_fecha, 
                 wcli_default)/*campos a los que insertare*/ 
    SELECT wgru_id, 
           wusr_id, 
           /*estos son los id que quiero recuperar de la otra tabla*/ 
           wcli_cod, 
           wclte_id, 
           Getdate(), 
           wcli_default 
    FROM   tabla1,(SELECT TOP (@cantidad_filas) wusr_id 
            FROM   tabla2 
            ORDER  BY wusr_id DESC) 
    WHERE  wusr_id = @usuario_seleccionado 

Try to see if it does what you ask ... otherwise it explains again what you need to see if the second I understand hehe

    
answered by 17.09.2018 / 10:41
source
0

I already managed to do it, I know it was not understandable, I did not understand it at first, thanks anyway. This is a cursor that consists of going through all the rows of my table.

create procedure Mant_spIngresarRegistro_wusuario_cliente ( @filas int, @id_seleccionado int ) as begin declare @usr_id smallint declare cursor1 cursor global for select TOP (@filas) wusr_id from dbo.WUSUARIO order by wusr_id desc open cursor1 fetch cursor1 into @usr_id while(@@fetch_status = 0) begin insert into dbo.wusuario_cliente /tabla/ (wgru_id,wusr_id,wcli_cod,wclte_id,wcli_fecha,wcli_default)/campos a los que insertare/ select wgru_id , @usr_id, wcli_cod,wclte_id,GETDATE(),wcli_default from dbo.wusuario_cliente where wusr_id=@id_seleccionado fetch cursor1 into @usr_id end close cursor1 deallocate cursor1 end

    
answered by 17.09.2018 в 20:43