Anda di halaman 1dari 11

use master go if exists( select * from sysdatabases where name='dbBiblioteca') drop database dbBiblioteca go create database dbBiblioteca go use

dbBiblioteca go create table tblLibro ( CodLibro varchar(4)primary key, titulo varchar(50), Autor varchar(50), Edicion varchar(20), Idioma varchar(30), ) go create table tblEjemplar ( NroEjemplar varchar(4) primary key, EstadoConservacion varchar(20), Ubicacion varchar(20), CodLibro varchar(4), foreign key(CodLibro) references tblLibro, ) go create table tblLector ( Nrocarnet varchar(4) primary key, Nombre varchar(50), Telefono varchar(25), Habilitado bit, ) go create table tblPrestamo ( NroPrestamo varchar(4) primary key, FechaPrestamo datetime, FechaDevolucion datetime, NroEjemplar varchar(4), NroCarnet varchar(4), foreign key(NroEjemplar) references tblEjemplar, foreign key(NroCarnet) references tblLector, ) go --procedimiento a if exists( select*from sysobjects where name='sp_ManteLector') drop proc sp_ManteLector go create proc Sp_ManteLector @NroCarne Varchar(4),@Nombre Varchar(50),@Telefono Va rchar(25),@Habilitado bit, @Operacion varchar(1) As Begin

IF(@Operacion = 'I') if exists (Select * from tblLector Where NroCarnet = @NroCarne) Select 2 CodError else begin Insert into tblLector values (@NroCarne,@Nombre,@Telefono,@Habi litado) Select 0 CodError end --end Else If(@Operacion ='U') begin if exists(select * from tblLector where NroCarnet=@NroCarne) begin Update tblLector set Nombre=@Nombre,Telefono=@telefono,Habilitado=@Habilitado where NroCarnet=@NroCarne select 0 CodError end else select 1 CodError end else --D = Eliminar poco a poco (borados en casacada es peligro eso lo hac el admini stardor) if(@Operacion='D') begin if exists(select * from tblLector where NroCarnet=@NroCarne) if exists(select * from tblPrestamo where NroCarnet=@NroCarne) select 3 CodError else begin Delete from tblLector where NroCarnet=@NroCarne Select 0 CodError End else select 1 CodError End End--Fin del Procediento

execute execute execute execute execute execute execute

Sp_ManteLector Sp_ManteLector Sp_ManteLector Sp_ManteLector Sp_ManteLector Sp_ManteLector Sp_ManteLector

'L001','oo Fernandez','345678',1,'I' 'L002','Pedro Perez','412698',1,'I' 'L003','Pamela Delgado','234578',1,'I' 'L004','Adrian Valer','290153',1,'I' 'L005','Leo Leon','567463',1,'I' 'L006','Maria Gonzales','863449',1,'I' 'L007','juan Vargas','412698',1,'I'

execute Sp_ManteLector 'L002','Pedro Perez','412698',1,'D' select*from tblLector if exists( select*from sysobjects where name='sp_ManteLibro') drop proc sp_ManteLibro

go create proc sp_ManteLibro @codLibro varchar(4),@Titulo varchar(50),@Autor varcha r(50),@Edicion varchar(20),@Idioma varchar(30),@Operacion varchar(1) as begin if(@Operacion='I') if exists(select*from tbllibro where codlibro=@codlibro) select 2 coderror else begin insert into tbllibro values(@codlibro,@titulo,@autor,@edicion,@Idioma) select 0 coderror end else select 1 coderror end if(@Operacion='U') if exists(select*from tbllibro where codlibro=@codlibro) begin update tbllibro set titulo=@titulo,autor=@autor,edicion=@edicion,idioma=@idi oma where codlibro=@codlibro select 0 coderror end else select 1 coderror else if(@Operacion='D') begin if exists(select * from tbllibro where codLibro=@CodLibro) if exists(select * from tblejemplar where codlibro=@codLibro) select 3 CodError else begin Delete from tbllibro where codlibro=@codlibro Select 0 CodError End else select 1 CodError End go execute sp_mantelibro ion','espaol','I' execute sp_mantelibro paol','I' execute sp_mantelibro paol','I' execute sp_mantelibro ion','espaol','I' execute sp_mantelibro aol','I' execute sp_mantelibro dicion','espaol','I' execute sp_mantelibro les','I' execute sp_mantelibro dicion','frances','I' 'L001','El viejo y el mar','Ernest Hemiguay','tercera edic 'L002','Ciudad y los perros','Vargas llosa','4 edicion','es 'L003','El viejo ','Ernest Hemiguay','tercera edicion','es 'L004','Juan salvador Gaviota','Pedro Perez','tercera edic 'L005','El Principe','Pablo Juarez','tercera edicion','esp 'L006','Las mil y una noches','Ernest Hemiguay','tercera e 'L007','El Principe','Pablo Juarez','tercera edicion','ing 'L008','Las mil y una noches','Ernest Hemiguay','tercera e

exec sp_mantelibro 'L003','los perros','Juan perez','5 edicion','espaol','U' select*from tbllibro

if exists(select*from sysobjects where name='sp_ManteEjemplar') drop proc sp_ManteEjemplar go create proc sp_ManteEjemplar @nroEjemplar varchar(4),@EstadoConservacion varchar (20),@Ubicacion varchar(20),@CodLibro varchar(4),@Operacion varchar(1) as begin if(@Operacion='I') if exists(select*from tblejemplar where nroejemplar=@nroejemplar) select 2 coderror else if exists(select*from tbllibro where codlibro=@codlibro) begin insert into tblejemplar values(@nroejemplar,@estadoconservacion,@ubicacion, @codlibro) select 0 coderror end else select 4 coderror if(@Operacion='U') if exists(select*from tblejemplar where nroejemplar=@nroejemplar) if exists(select*from tbllibro where codlibro=@codlibro) begin update tblejemplar set estadoconservacion=@estadoconservacion,ubicacion=@u bicacion,codlibro=@codlibro where nroejemplar=@nroejemplar select 0 coderror end else select 4 coderror else select 1 coderror else if(@Operacion='D') if exists(select*from tblejemplar where nroejemplar=@nroejemplar) if exists(select*from tblprestamo where nroejemplar=@nroejemplar) select 3 coderror else begin delete from tblejemplar where nroejemplar=@nroejemplar select 0 coderror end else select 1 coderror end execute sp_ManteEjemplar 'E001','Bueno','Estante A-1','L001','I' execute sp_ManteEjemplar 'E002','Malo','Estante A-2','L002','I' execute sp_ManteEjemplar 'E003','Regular','Estante A-3','L005','I' execute sp_ManteEjemplar 'E004','Malo','Estante B-1','L004','I' execute sp_ManteEjemplar 'E005','Bueno','Estante B-1','L001','I' execute sp_ManteEjemplar 'E006','Bueno','Estante B-3','L001','I' execute sp_ManteEjemplar 'E007','Bueno','Estante B-2','L002','I' execute sp_ManteEjemplar 'E008','Bueno','Estante B-1','L002','I' execute sp_ManteEjemplar 'E009','Bueno','Estante B-3','L003','I' select*from tblejemplar

--PA DE PRESTAMO if exists(select*from sysobjects where name='sp_MantePrestamo' ) drop proc sp_MantePrestamo Go create proc sp_MantePrestamo @NroPrestamo varchar(4),@FechaPrestamo datetime,@N roEjemplar varchar(4), @NroCarnet varchar(4),@Operacion varchar(1) as Begin --I=insertar if(@Operacion='I') begin if exists(select * from tblPrestamo where NroPrestamo=@NroPrestamo) select 2 CodError --el 2 es ya existe else if (exists(select * from tblEjemplar where NroEjemplar = @NroEjemplar) an d exists(select * from tblLector where NroCarnet = @NroCarnet)) Begin insert into tblPrestamo values(@NroPrestamo,@FechaPrestamo ,null, @NroEjemplar, @NroCarn et) select 0 Coderror --el 0 es satisfactoriamente end else select 4 CodError --el 4 es no existe dato relacionado End else --U = modificar if(@Operacion='U') begin if exists(select * from tblPrestamo where NroPrestamo = @NroPrestamo ) if (exists(select * from tblEjemplar where NroEjemplar = @NroEjemplar) an d exists(select * from tblLector where NroCarnet = @NroCarnet)) begin Update tblPrestamo set FechaPrestamo=@FechaPrestamo,NroEjemplar=@NroEjemplar,NroCarnet= @NroCarnet where select end else select 4 else select 1 end NroPrestamo = @NroPrestamo 0 CodError CodError CodError

else --D = Eliminar if(@Operacion='D') ---preguntamos si el ejemplar existe if exists(select * from tblPrestamo where NroPrestamo = @NroPrestamo )

begin --preguntamos si existe el prestamo delete from tblPrestamo where NroPrestamo = @NroPrestamo select 0 Coderror end else select 1 CodError end execute sp_MantePrestamo 'P001','01/01/2007','E001','L001','I' execute sp_MantePrestamo 'P002','01/01/2007','E002','L001','I' execute sp_MantePrestamo 'P003','01/02/2007','E003','L002','I' execute sp_MantePrestamo 'P004','01/02/2007','E002','L002','I' execute sp_MantePrestamo 'P005','01/03/2007','E004','L002','I' execute sp_MantePrestamo 'P006','01/03/2007','E005','L003','I' execute sp_MantePrestamo 'P007','01/04/2007','E004','L003','I' execute sp_MantePrestamo 'P008','01/04/2007','E003','L003','I' execute sp_MantePrestamo 'P009','01/04/2007','E001','L003','I' select*from tblPrestamo end--frin del procedimiento if exists(select*from sysobjects where name='sp_SeleccionaEjemplar') drop proc sp_SeleccionaEjemplar go create proc sp_SeleccionaEjemplar as begin select* from tblEjemplar order by NroEjemplar end go if exists(select*from sysobjects where name='sp_SeleccionaLibro') drop proc sp_SeleccionaLibro go create proc sp_SeleccionaLibro as begin select* from tblLibro order by Titulo end go if exists(select*from sysobjects where name='sp_SeleccionaLector') drop proc sp_SeleccionaLector go create proc sp_SeleccionaLector as begin select* from tblLector order by NroCarnet end go if exists(select*from sysobjects where name='sp_SeleccionaPrestamo') drop proc sp_SeleccionaPrestamo go create proc sp_SeleccionaPrestamo as begin

select* from tblPrestamo order by NroPrestamo end go -- Siguiente Prestamo create proc sp_SiguientePrestamo as begin declare @sgte varchar(4) select @sgte=max(Nroprestamo) from tblprestamo if(@sgte is null) set @sgte='P001' else begin set @sgte=right(@sgte,1) set @sgte=cast(cast(@sgte as int)+1 as varchar(4)) while(len (@sgte)<3) set @sgte ='0'+@sgte set @sgte='P'+@sgte end select @sgte sgte end go --Fin --Busca un libro en funcion al codigo if exists (select*from sysobjects where name='sp_BuscarLibroporCodigo' ) drop proc sp_BuscarLibroporCodigo go create proc sp_BuscarLibroporCodigo @CodLibro varchar(4), @titulo varchar(50)=nu ll output,@Autor varchar(50)=null output,@Edicion varchar(20)=null output,@Idiom a varchar(30)=null output as begin select @titulo=titulo,@autor=autor,@edicion=edicion,@idioma=idioma from tblLibro where codLibro=@CodLibro if(@Titulo is null) begin set @Titulo='' set @Autor='' set @Edicion ='' set @Idioma='' select 1 codError end else select 0CodError end go --Busca libros en funcion a un patron de busqueda if exists(select*from sysobjects where name='sp_BuscarLibroporPatron') drop proc sp_BuscarLibroporPatron go create proc sp_BuscarLibroporPatron @Patron varchar(50) as

begin select*from tblLibro where titulo like @Patron+'%' order by Titulo end --fin-if exists(select*from sysobjects where name='sp_BuscarLibroDebido') drop proc sp_BuscarLibroDebido go create proc sp_BuscarLibroDebido as begin select P.NroPrestamo, Le.NroCarnet Carnet,Le.Nombre Lector,E.NroEjemplar,L.Titul o,L.Autor,P.FechaPrestamo Fecha from tblPrestamo P inner join tblLector Le on P.NroCarnet=Le.NroCarnet inner join tblEjemplar E On E.NroEjemplar=P.NroEjemplar inner join tblLibro L On L.CodLibro=E.CodLibro where FechaDevolucion is null end go if exists (select*from sysobjects where name='sp_RegistrarDevolucion') drop proc sp_RegistrarDevolucion go create proc sp_RegistrarDevolucion @NroPrestamo varchar(4)as begin if exists(select*from tblprestamo where NroPrestamo=@Nroprestamo) begin update tblprestamo set fechadevolucion=getdate() where NroPrestamo=@NroPrestamo select 0 CodError end else select 1 CodError end go --Busca un ejemplar en funcion al numero if exists (select*from sysobjects where name='sp_BuscarEjemplarporNumero' ) drop proc sp_BuscarEjemplarporNumero go create proc sp_BuscarEjemplarporNumero @NroEjemplar varchar(4), @EstadoConservac ion varchar(20)=null output,@Ubicacion varchar(20)=null output,@CodLibro varchar (4)=null output as begin select @EstadoConservacion=EstadoConservacion,@Ubicacion=Ubicacion,@CodLibro=Cod Libro from tblEjemplar where NroEjemplar=@NroEjemplar if(@NroEjemplar is null) begin set @NroEjemplar='' set @EstadoConservacion=''

set @Ubicacion ='' set @CodLibro='' select 1 codError end else select 0CodError end go --Busca ejemplares en funcion a un patron de busqueda if exists(select*from sysobjects where name='sp_BuscarEjemplarporPatron') drop proc sp_BuscarEjemplarporPatron go create proc sp_BuscarEjemplarporPatron @Patron varchar(50) as begin select*from tblEjemplar where NroEjemplar like @Patron+'%' order by NroEjemplar end --fin-go --Busca un lector en funcion al numero de carnet if exists (select*from sysobjects where name='sp_BuscarLectorporNumero' ) drop proc sp_BuscarLectorporNumero go create proc sp_BuscarLectorporNumero @NroCarnet varchar(4), @Nombre varchar(50)= null output,@Telefono varchar(25)=null output,@Habilitado bit=null output as begin select @Nombre=Nombre,@Telefono=Telefono,@Habilitado=Habilitado from tblLector where NroCarnet=@NroCarnet if(@Nombre is null) begin set @NroCarnet='' set @Nombre='' set @Telefono ='' set @Habilitado=0 select 1 codError end else select 0CodError end go --Busca ejemplares en funcion a un patron de busqueda if exists(select*from sysobjects where name='sp_BuscarLectorporPatron') drop proc sp_BuscarLectorporPatron go create proc sp_BuscarLectorporPatron @Patron varchar(50) as begin select*from tblLector where Nombre like @Patron+'%' order by Nombre end --fin-go

--seleccionar ejemplar go if exists(select * from sysobjects where name='sp_SeleccionaEjemplarLibro' ) drop proc sp_SeleccionaEjemplarLibro Go create proc sp_SeleccionaEjemplarLibro @Patron varchar(50) as Begin select E.NroEjemplar,L.Titulo, L.Autor, E.Ubicacion from tbllibro L inner join tblEjemplar E on E.CodLibro = L.CodLibro where L.Titulo like @Patron+'%' end go --Listar Prestamos if exists (select * from sysobjects where name='sp_ListarPrestamos') drop proc sp_ListarPrestamos go create proc sp_ListarPrestamos @Patron varchar(50) as begin select P.NroPrestamo,L.NroCarnet Carnet,L.Nombre Lector,E.NroEjemplar,T. Titulo,P.FechaPrestamo Fecha,P.FechaDevolucion FechaDevolucion from tblPrestamo P inner join tblLector L on P.NroCarnet=L.NroCarnet inner join tblEjemplar E on E.NroEjemplar=P.NroEjemplar inner join tblLibro T on T.CodLibro =E.CodLibro --where FechaDevolucion is null and L.Nombre like @Patron+'%' end go execute sp_ListarPrestamos '' --Cantidad de Ejemplares por Libro (cod libro,titulo,autor,edicion,idioma,cant e jemplares) if exists (select * from sysobjects where name='sp_CantEjemplares') drop proc sp_CantEjemplares go create proc sp_CantEjemplares as begin select L.CodLibro [Cdigo],L.Titulo [Ttulo],L.Autor,L.Edicion [Edicin],L.Idioma, count(E.NroEjemplar) [Cant. Ejemplares] from tblLibro L,tblEjemplar E where L.CodLibro=E.CodLibro group by L.CodLibro,L.Titulo,L.Autor,L.Edicion,L.Idioma end go -------Fin procedimieto execute sp_CantEjemplares --Otro procedimiento if exists (select * from sysobjects where name='sp_PrestamoMes') drop proc sp_PrestamoMes go create proc sp_PrestamoMes @mes int,@annio int As begin select NroPrestamo,FechaPrestamo,Titulo,Nombre,E.NroEjemplar from tblPrestamo P,tblEjemplar E,tblLibro L,tblLector Le where E.NroEjemplar=P.NroEjemplar

and and and and end go

L.CodLibro=E.CodLibro Le.NroCarnet=P.NroCarnet year(FechaPrestamo)=@annio month(FechaPrestamo)=@mes

--RElacion de libros de un idioma especifico if exists (select * from sysobjects where name='sp_IdiomaLibro') drop proc sp_IdiomaLibro go create proc sp_IdiomaLibro @idioma varchar(30) as begin select codLibro,autor,edicion,idioma from tbllibro where idioma like @idioma+'%' end go --Fin execute sp_IdiomaLibro 'frances'

Anda mungkin juga menyukai