stored procedures [closed]

-3

I need to know how to do stored procedures in SQL Server for working I have a small database something like this:

use Inventario
create table Clientes (
Nit int,
Nombre char (50),
Direccion char(50),
Telefono int,
primary key (Nit)
);

create table Representante_de_ventas (
Id_vdor int,
Nombre char (50),
Cuota_Vtas int,
primary key (Id_vdor)
);

create table Productos (
Id int,
Descripcion char (50),
Precio int,
primary key (Id)
);

create table Pedido (
Num_pedido int,
Vdor int,
Nit_cliente int,
Fecha_pedido date,
Producto int,
Cantidad int,
primary key (Num_pedido,Nit_cliente,Vdor,Producto),
foreign key (Nit_cliente) references Clientes(Nit),
foreign key (Vdor) references Representante_de_ventas(Id_vdor),
foreign key (Producto) references Productos(Id),
);
    
asked by Jhon Chavez 23.06.2017 в 16:31
source

2 answers

3

To create a stored procedure you must position yourself in the database in which you want to generate the procedure and then do the following:

1.- You open the database by pressing the "+" sign

2 .- Go to the folder called Programmability and press again the "+"

3.- You position yourself in the folder Stored Procedures and press right click

4.- Once you right click, you select the Stored Procedure option ...

5.- Once the previous step is completed, the body of the stored procedure will appear as follows:

CREATE PROCEDURE <Nombre_del_procedimiento>
-- Declara los parametros aquí
@Parametro1 int
AS
BEGIN

    SET NOCOUNT ON;

    -- Inserta el Query aquí
    SELECT 
       Ejemplo
    FROM
       Tabla_Ejemplo
    WHERE
       Ejemplo = @Parametro1
END
GO

6.- Press Execute or (F5) to save the Query

    
answered by 23.06.2017 в 17:31
2

You have to follow a structure and maintain an order, it would help a lot to know what you want to do with your stored procedure.

For example, one that consults the orders of a certain client, but only the order number, date and the name of the client, would be like this:

CREATE PROCEDURE SPR_OBTENERPEDIDOXCLIENTE
 @NIT_CLIENTE INT

AS  
BEGIN  

SELECT
P.NUM_PEDIDO,
P.FECHA_PEDIDO,
C.CLIENTE

FROM PEDIDO P
INNER JOIN CLIENTE C ON C.NIT = P.NIT_CLIENTE
WHERE P.NIT_CLIENTE = @NIT_CLIENTE

END  
    
answered by 23.06.2017 в 16:40