Documentos de Académico
Documentos de Profesional
Documentos de Cultura
--Crear BD
GO
CREATE DATABASE BD_Examen_I
--Crear Tablas
GO
USE BD_Examen_I
GO
CREATE TABLE CLIENTE
(
cliente_id integer IDENTITY ( 1,1 ) ,
estado varchar(5) NOT NULL ,
saldo money NOT NULL
)
go
--Condiciones
GO
USE BD_Examen_I
GO
GO
-- RETORNA UNA CADENA DE ACUERDO A LA POSICION SOLICITADA
CREATE FUNCTION dbo.indexOf(@cadena VARCHAR(1000),@indice INTEGER)
RETURNS VARCHAR(50) AS
BEGIN
declare @posicion int
declare @valor varchar(100)
declare @contador integer
set @contador = 0
set @cadena = @cadena + ','
while patindex('%,%' , @cadena) <> 0
begin
set @posicion = patindex('%,%' , @cadena)
set @valor = left(@cadena, @posicion - 1)
if @contador = @indice
return @valor
set @cadena = stuff(@cadena, 1, @posicion, '')
set @contador = @contador + 1
end
return 'error'
END
GO
CREATE PROCEDURE SP_registrar_venta
@idCliente INTEGER, @idVendedor INTEGER, @id_productos VARCHAR(100),
@cantidad_productos varchar(100),
@numeroProductos INTEGER
AS
BEGIN
-- VARIABLES
declare @estado varchar(10) -- A Activo, N No Activo
declare @saldo money
declare @contProductos integer
declare @idProducto integer
declare @importeTotal money
declare @cantidadProducto integer
declare @precioTemp money
declare @stockProducto integer
declare @idVenta integer
declare @idDocumentoVenta integer
set @cantidadProducto = 0
set @importeTotal = 0
set @contProductos = 0
SELECT @estado = C.estado, @saldo = C.saldo FROM CLIENTE AS C WHERE
C.cliente_id = @idCliente
-- VERIFICACION DE ESTADO
if @estado = 'N'
begin
RAISERROR('El cliente no esta activo',16,16)
return
end
--VERIFICACION DE STOCK
if @cantidadProducto > @stockProducto
begin
raiserror('No esiste stock suficiente para el producto',16,16)
return
end
set @contProductos = 0
while @contProductos < @numeroProductos
begin
set @idProducto =
CONVERT(integer,dbo.indexOf(@id_Productos,@contProductos))
set @cantidadProducto =
CONVERT(integer,dbo.indexOf(@cantidad_productos,@contProductos))
SELECT @precioTemp = P.precio, @stockProducto = P.stock
FROM PRODUCTO AS P WHERE P.producto_id = @idProducto
INSERT INTO
DETALLE_DOCUMENTO(documento_id,producto_id,stock_actual,sock_anterior,cantidad_vend
ida)
VALUES(@idDocumentoVenta,@idProducto,@stockProducto -
@cantidadProducto,@stockProducto,@cantidadProducto)
UPDATE PRODUCTO SET stock = @stockProducto -
@cantidadProducto WHERE producto_id = @idProducto
END
--Prueba
GO
USE BD_Examen_I
GO
--DELETE CLIENTE
--DELETE VENDEDOR
--DELETE PRODUCTO
--INSERT INTO CLIENTE(estado,saldo) VALUES('A',100),('N',100),('A',0)
--INSERT INTO VENDEDOR(nombre) VALUES('Alex')
--INSERT INTO PRODUCTO(nombre,stock,precio)
VALUES('p04',10,13),('p05',15,10),('p6',6,15)
GO
EXEC SP_registrar_venta 1,1,'2,4,5','3,1,1',3
GO
SELECT * FROM CLIENTE
SELECT * FROM DOCUMENTO_VENTA
SELECT * FROM DETALLE_DOCUMENTO
SELECT * FROM PRODUCTO
SELECT * FROM COMISION