View with store Procedure, mysql

Asked

Viewed 330 times

1

I have a procedure correct and a view which uses the Procedure parameter, only the view does not see the Procedure parameter, how to do to solve this problem?

Create a store So that from the customer’s code you can get the total amount of purchases with a discount of 10%

delimiter $$
drop view if exists visao  $$
create view visao as select n.cod_cliente as codCli, (sum(i.qtd_vedida * i.pco_recebido)*0.9) as valorTotal
        from nota_fiscal as n, item_nota_fiscal as i
        where n.numero_nf = i.numero_nf and n.cod_cliente = AQUI SERIA O PARÂMETRO DA PROCEDURE, SÓ QUE A PROCEDURE VEM DEPOIS E ELE NÃO CONSEGUE PEGAR O PARÂMETRO, EXISTE ALGUMA SOLUÇÃO??(pCodCli )
        group by codCli $$

DROP PROCEDURE IF EXISTS uspDesconto $$
CREATE PROCEDURE uspDesconto (pCodCli int(11))

BEGIN
  DECLARE done BOOLEAN DEFAULT FALSE;
  DECLARE vCodCli, vQtd_vedida int(11);  
  DECLARE vValorTotal decimal(10,2);
  DECLARE cont integer;

  declare cursor_a cursor for  select * from visao;

  DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = TRUE;
  set cont = 0;

  DROP TEMPORARY TABLE IF EXISTS tmp_Prod_Forn;
  CREATE TEMPORARY TABLE tmp_Prod_Forn (
        tmpCodCli int(11),
        tmpValorTotal decimal(10,2)
  );

  OPEN cursor_a;
  REPEAT FETCH cursor_a INTO vCodCli, vValorTotal;
     IF NOT done THEN
        insert into tmp_Prod_Forn values(vCodCli, vValorTotal);
        -- set cont = cont + 1;
     END IF;

  UNTIL (done) END REPEAT;
  close cursor_a;

  SELECT * FROM tmp_Prod_Forn;
END $$

delimiter ;
  • You are creating a view inside the Database?

  • above the view and below the precedent, if I didn’t have to call a parameter, the code would work normal, but I have to have this parameter and I don’t know how to call

  • No questions, why does the view need to take the parameter of the previous one? Where does this parameter come from?

  • is to see if the code of the client that exists in the table is equal to the past in the Procedure call

  • You do not need to create a view to check this. Check inside the process itself through an if....

  • Maybe @rray can help you in this better, Process in mysql I don’t know much.

  • what is @rray? thanks in advance!

  • What parameter are you talking about? At what point in your view should you see the parameter? Remember that a view is a table (virtual table) if you need to pass something to it, it would be through the Where.

  • the pCodCli parameter you have there on the previous page. The view needs it to display the query

Show 4 more comments

1 answer

1


Knowing that a view is the same as a table, as I explained in answer to your previous question the only thing you need to do is use this parameter as a filter for the query in the view, so when creating the cursor, simply use the Where parameter of the query in the view:

declare cursor_a cursor for  select * from visao where codCli = pCodCli;

EDIT

Seeing your edition I realized where the code of the parameter is.

Your view should then be created as follows:

create view visao as 
    select n.cod_cliente as codCli, 
          (sum(i.qtd_vedida * i.pco_recebido)*0.9) as valorTotal
      from nota_fiscal as n, 
           item_nota_fiscal as i
     where n.numero_nf = i.numero_nf 
     group by codCli $$

No need Add the parameter directly in the view, this view will work as a table that will have the following format:

"Tabela" visao
  codCli
  valorTotal     

So all you need to do is use the field codCli which is the same field n.cod_cliente that you were trying to use. What you’re doing here is putting the filter to be used outside the view. So just use the view as reported before here:

declare cursor_a cursor for  select * from visao where codCli = pCodCli;
  • but in case I would have to take Where out of the view and put it in select * from visao? like this

  • drop view if exists visao $$
create view visao as select n.cod_cliente as codCli, (sum(i.qtd_vedida * i.pco_recebido)*0.9) as valorTotal
 from nota_fiscal as n, item_nota_fiscal as i
 group by codCli $$

  • but a strange result

  • 1

    No. Understand that the query-based view is like a table, a virtualization of that query as if it were a new table. If you have an x table and it has A, B and C records, then you create a view like this: select campo from tabela where campo in ('a','b') means your view is a table that only has A and B records so if you do select * from viewcriada where campo=C will return no record.

  • If the result is weird it means your trial is not doing what it should do properly ousyour view is not returning only what it should return!

  • but the view needs a Where and this Where needs the pCodCli record, I don’t know what else to do!

  • 1

    I edited the answer @Gabriella see if you can understand.

  • Obg Jorge Campos!

Show 3 more comments

Browser other questions tagged

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