/* SCRIPT: CREATE_DB.sql */
/* 创建TestDB数据库 */
-- This is the main caller for each script
SET NOCOUNT ON
GO
PRINT '开始创建TestDB数据库'
IF EXISTS (SELECT 1 FROM SYS.DATABASES WHERE NAME = 'TestDB')
DROP DATABASE TestDB
GO
CREATE DATABASE TestDB
GO
:On Error exit
:r c:\Scripts\CREATE_TABLES.sql
:r c:\Scripts\TABLE_INSERTS.sql
:r c:\Scripts\CREATE_INDEXES.sql
:r c:\Scripts\CREATE_PROCEDURES.sql
PRINT '创建完毕'
GO
/* 创建存储过程 */
PRINT '正在创建存储过程'
GO
USE TestDB
GO
IF OBJECT_ID('GET_EMPLOYEE_TIMECARDS') IS NOT NULL
DROP PROCEDURE DBO.GET_EMPLOYEE_TIMECARDS
GO
CREATE PROCEDURE DBO.GET_EMPLOYEE_TIMECARDS @EMPLOYEEID INT
AS
SET NOCOUNT ON
SELECT *
FROM DBO.EMPLOYEE E
JOIN DBO.TIMECARD T ON E.EMPLOYEEID = T.EMPLOYEEID
WHERE E.EMPLOYEEID = @EMPLOYEEID
ORDER BY DATEWORKED
GO
/* 创建数据表 */
PRINT '正在创建数据表 '
GO
USE TestDB
GO
IF OBJECT_ID('EMPLOYEE') IS NOT NULL
DROP TABLE DBO.EMPLOYEE
GO
CREATE TABLE DBO.EMPLOYEE
(
EMPLOYEEID INT IDENTITY(1, 1)
NOT NULL
PRIMARY KEY ,
FIRSTNAME VARCHAR(50) ,
LASTNAME VARCHAR(50)
)
GO
IF OBJECT_ID('TIMECARD') IS NOT NULL
DROP TABLE DBO.TIMECARD
GO
CREATE TABLE DBO.TIMECARD
(
TIMECARDID INT IDENTITY(1, 1)
NOT NULL
PRIMARY KEY ,
EMPLOYEEID INT NOT NULL ,
HOURSWORKED TINYINT NOT NULL ,
HOURLYRATE MONEY NOT NULL ,
DATEWORKED DATETIME NOT NULL
)
GO
DECLARE @TOTAL_TABLES INT
SET @TOTAL_TABLES = 2
/* 插入表数据 */
PRINT 'TOTAL TABLES CREATED = ' + CAST(@TOTAL_TABLES AS VARCHAR)
GO
PRINT '正在插入数据到表 EMPLOYEE'
GO
USE TestDB
GO
INSERT INTO DBO.EMPLOYEE
( FIRSTNAME, LASTNAME )
SELECT 'JOHN' ,
'DOE'
GO
INSERT INTO DBO.EMPLOYEE
( FIRSTNAME, LASTNAME )
SELECT 'JANE' ,
'DOE'
GO
INSERT INTO DBO.EMPLOYEE
( FIRSTNAME, LASTNAME )
SELECT 'JEFF' ,
'DOE'
GO