Inner Join in Procedure

Asked

Viewed 525 times

1

I am creating a small database on SQL Server, for an ASP.NET mvc project of my course.

Using ADO.NET I am creating procedures, to use in my DAL class, so far everything well has a lot of information about and everything else, however, what I need to do is to perform a Join within a previous, I haven’t found anything about or at least anything I’ve understood how it works.

The problem is that I am not understanding how to put the parameters of the two tables in my previous to perform the select with Inner Join.

I tried this way:

CREATE TABLE PRODUTO(
ID_PRATO INTEGER PRIMARY KEY IDENTITY,
FK_CATEGORIA_ID INT,
DESCRICAO_PRATO VARCHAR(200),
VALOR DECIMAL(5,2) NOT NULL,
CONSTRAINT FK_CATEGORIA_ID FOREIGN KEY (FK_CATEGORIA_ID) REFERENCES CATEGORIA (ID_CATEGORIA)
)
GO

CREATE TABLE ENTREGA(
ID_ENTREGA INTEGER PRIMARY KEY IDENTITY,
FK_PRATO_ID2 INT,
CONSTRAINT FK_PRATO_ID2 FOREIGN KEY (FK_PRATO_ID2) REFERENCES PRODUTO (ID_PRATO),
STATUS_ENTREGA VARCHAR(10) CHECK (STATUS_ENTREGA IN ('CANCELADA','TRANSITO','REALIZADA'))
)
GO


create procedure sp_select_produto
(
  @id_prato int,
	@descricao_cat varchar(200),
	@descricao_prato varchar(200),
	@valor decimal(5,2)
)	
as
select prod.id_prato,cat.descricao_cat,prod.descricao_prato,prod.valor from CATEGORIA 
as cat inner join PRODUTO prod 
on cat.id_categoria=prod.FK_CATEGORIA_ID
where prod.id_prato=ID_PRATO
return

But when I give an exec an error appears:

Mensagem 201, Nível 16, Estado 4, Procedimento sp_select_produto, Linha 225 O procedimento ou a função 'sp_select_produto' espera o parâmetro '@id_prato', que não foi fornecido.

Any idea what I can do in this case ?

  • Simply on your call from procedure (that you did not show in your question) there is no passage to the parameter id_prato.

  • At the moment, I’m not performing the call in the project I was trying to test within sql server manager only, but I understood what you said I will perform more tests thank you

1 answer

3


The message:

Mensagem 201, Nível 16, Estado 4, Procedimento sp_select_produto, Linha 225 O procedimento ou a função 'sp_select_produto' espera o parâmetro '@id_prato', que não foi fornecido.

Indicates that the parameter @id_prato was added to your declaration procedure but was not added to the call. To run the procedure you must pass all parameters that are required:

EXEC sp_procedure @parametro1, @parametro2

Or, if the parameter is not required, add a default value to the declaration:

CREATE PROCEDURE sp_procedure @parametro_obrigatorio INTEGER, @parametro_opcional INTEGER = NULL AS
-- ...

And make the call with named parameters:

EXEC sp_procedure @parametro_obrigatorio = 1
  • Got it, the Where I put in needs to have the @id_dish filled at the time of the run so I can run the Procedure

  • 1

    @Nelsonfrancisco then really just add the parameter in the call that the error will disappear

  • I just tested it all worked out thank you very much ..

Browser other questions tagged

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