Está en la página 1de 2

CREATE table Ventas

(
Idventa int IDENTITY(1,1) PRIMARY KEY,
Fecha datetime NOT NULL DEFAULT GETDATE(),
Cant int NOT NULL,
Idproduct int FOREIGN KEY REFERENCES Productos(Idproducto)
)
CREATE TABLE Productos
(
Idproducto int IDENTITY(1,1) PRIMARY KEY,
ProductName varchar(255) NOT NULL,
ProductDesc varchar(255),
Cantidad int NOT NULL,
Precio decimal NOT NULL,
CONSTRAINT check_cont CHECK (Cantidad>-1)
)
drop TABLE Productos
select * from productos
select * from ventas
INSERT INTO Productos(ProductName,ProductDesc,Cantidad,Precio)
VALUES ('Cuaderno','cuadernos marca balboa',50,2.50);
CREATE PROCEDURE vendiendo
@Idproc int,
@quant int
as
--INSERT INTO productos(ProductName ,ProductDesc , Cantidad, Precio)
--VALUES(@product_name , @product_desc , @cant,@precio);
GO
EXECUTE vendiendo 1, 25

USE [Northwind]
GO
/****** Object: StoredProcedure [dbo].[vendiendo]
Script Date: 04/10/2016 21
:08:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[vendiendo]
@Idproc int,
@quant int
as
--INSERT INTO productos(ProductName ,ProductDesc , Cantidad, Precio)

--VALUES(@product_name , @product_desc , @cant, @precio);


DECLARE @qt int = (SELECT Cantidad FROM Productos where Idproducto = @Idproc);
IF @quant<=0 --SE VALIDA QUE LA VENTA SEA 1 O MAS
PRINT 'CANTIDAD IGUAL A 0'
ELSE
BEGIN
IF(@qt < @quant) -- SE VALIDA QUE LA CANTIDAD DISPONIBLE SEA MAYOR A LA
QUE QUIERE VENDER
PRINT 'NO SE PUEDE VENDER ESTA CANTIDAD'
ELSE
BEGIN
SET @qt = @qt - @quant;
UPDATE Productos SET Cantidad = @qt where Idproducto = @
Idproc
INSERT INTO ventas(Idventa,Idproduct, cant, fecha)VALUEs
(1,@Idproc,@quant, GETDATE())
END
END
GO

También podría gustarte