Código SQL:
Ver original
CREATE DATABASE ULTIMARESE GO USE ULTIMARESE DROP TABLE MESA DROP TABLE HORARIOS DROP TABLE RESERVACIONES DROP TABLE #horarios_ocupados DROP TABLE #temp CREATE TABLE MESA( COD_MESA INT IDENTITY(1,1)PRIMARY KEY, MESA_DESC VARCHAR(30) , MESA_SILLAS INT , SEDE_COD CHAR(6), MESA_FILA INT, MESA_COLUMNA INT, MESA_ESTADO CHAR(1) , MESA_TIPO CHAR(1), MESA_PRECIO DECIMAL(7,2), MESA_IMG VARCHAR(20) ) GO INSERT INTO MESA VALUES('MESA D-30',12,'SED004',6,5,'2','0',10.00,'MESA_12.jpg') INSERT INTO MESA VALUES('MESA D-31',12,'SED004',6,5,'2','0',10.00,'MESA_12.jpg') GO SELECT * FROM MESA GO CREATE TABLE HORARIOS( COD_HORA INT IDENTITY(1,1)PRIMARY KEY, HORA_INICIO TIME ) INSERT INTO HORARIOS VALUES ('08:00') INSERT INTO HORARIOS VALUES ('09:00') INSERT INTO HORARIOS VALUES ('10:00') INSERT INTO HORARIOS VALUES ('11:00') INSERT INTO HORARIOS VALUES ('12:00') INSERT INTO HORARIOS VALUES ('13:00') INSERT INTO HORARIOS VALUES ('14:00') INSERT INTO HORARIOS VALUES ('15:00') INSERT INTO HORARIOS VALUES ('16:00') INSERT INTO HORARIOS VALUES ('17:00') INSERT INTO HORARIOS VALUES ('18:00') INSERT INTO HORARIOS VALUES ('19:00') INSERT INTO HORARIOS VALUES ('20:00') INSERT INTO HORARIOS VALUES ('21:00') GO SELECT * FROM HORARIOS GO CREATE TABLE RESERVACIONES( COD_RESE INT IDENTITY(1,1)PRIMARY KEY, RESE_REGISTRO DATE , RESE_FECHA DATE , RESE_NPER INT , HORA_COD INT , RESE_HORAS TIME , RESE_ESTADO CHAR(1), CLI_COD CHAR(6)NULL, MESA_COD INT, CONSTRAINT FK_RESER_HORA FOREIGN KEY (HORA_COD) REFERENCES HORARIOS(COD_HORA), CONSTRAINT FK_RESER_MESA FOREIGN KEY (MESA_COD) REFERENCES MESA(COD_MESA) ) GO SELECT RESE_HORAS,HORA_COD FROM RESERVACIONES GO INSERT INTO RESERVACIONES VALUES (GETDATE(),dateadd(DAY,2,GETDATE()),4,2,'11:00','0','CLI001',1) INSERT INTO RESERVACIONES VALUES (GETDATE(),dateadd(DAY,1,GETDATE()),4,3,'11:00','0','CLI001',1) INSERT INTO RESERVACIONES VALUES (GETDATE(),GETDATE(),4,1,'10:00','0','CLI001',1) INSERT INTO RESERVACIONES VALUES (GETDATE(),GETDATE(),4,3,'12:00','0','CLI001',2) INSERT INTO RESERVACIONES VALUES (GETDATE(),GETDATE(),4,1,'10:00','0','CLI001',1) GO --Horas ocupadas SELECT M.MESA_DESC,H.HORA_INICIO,R.RESE_HORAS,R.RESE_FECHA FROM RESERVACIONES AS r,HORARIOS AS h, MESA AS m WHERE R.HORA_COD=H.COD_HORA AND M.COD_MESA=R.MESA_COD AND R.RESE_fECHA='04/05/2013'-- convert(varchar(20),GETDATE(),101) AND MESA_COD=COD_MESA AND H.COD_HORA IN (SELECT COD_HORA FROM HORARIOS WHERE (HORA_INICIO BETWEEN '00:00' AND '23:00') AND (R.RESE_HORAS BETWEEN '00:00' AND '23:00') ) ORDER BY M.MESA_DESC --Horas Libres DROP TABLE #temp DROP TABLE #horarios_ocupados SELECT HORA_INICIO,RESE_HORAS,MESA_COD,RESE_FECHA,IDENTITY(INT,1,1) AS rn INTO #temp FROM HORARIOS,RESERVACIONES WHERE HORARIOS.COD_HORA=RESERVACIONES.HORA_COD SELECT * FROM #temp CREATE TABLE #horarios_ocupados ( id INT, id_mesa INT, r_fecha DATE ) DECLARE @x INT DECLARE @id_mesa INT DECLARE @fecha VARCHAR(20) SET @x=1 SET @id_mesa=0 SET @fecha='04/05/2013' while @x<=(SELECT COUNT(*) FROM #temp) BEGIN SELECT @id_mesa=MESA_COD FROM #temp WHERE rn=@x INSERT INTO #horarios_ocupados SELECT COD_HORA,@id_mesa,@fecha FROM HORARIOS h,#temp t WHERE h.HORA_INICIO BETWEEN (SELECT HORA_INICIO FROM #temp WHERE rn=@x) AND (SELECT RESE_HORAS FROM #temp WHERE rn=@x)AND RESE_FECHA='04/05/2013' SET @x=@x+1 END SELECT DISTINCT HORA_INICIO,MESA_DESC,RESE_FECHA FROM RESERVACIONES AS r,HORARIOS AS h, MESA AS m,#horarios_ocupados WHERE RESE_FECHA='04/06/2013'--convert(varchar(20),GETDATE(),101) AND RESE_NPER NOT BETWEEN 0 AND 4-1 AND CONVERT(VARCHAR(20),H.COD_HORA) + '|' + CONVERT(VARCHAR(20),M.COD_MESA) NOT IN ( SELECT CONVERT(VARCHAR(20),id) + '|' + CONVERT(VARCHAR(20),id_mesa) FROM #horarios_ocupados) ORDER BY MESA_DESC DELETE FROM #horarios_ocupados SELECT * FROM MESA SELECT * FROM RESERVACIONES SELECT * FROM HORARIOS SELECT * FROM #temp SELECT * FROM #horarios_ocupados GO DROP TABLE MESA DROP TABLE HORARIOS DROP TABLE RESERVACIONES DROP TABLE #horarios_ocupados DROP TABLE #temp USE SR_OOD