-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsql_create_test_objects.sql
101 lines (81 loc) · 3.09 KB
/
sql_create_test_objects.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
-- **********************************************************************************************************
-- PURPOSE : This creates the tables and stored procedures required to run the tests for the Data Access Layer
-- WARNING : This script DROPS and RECREATES the following named database objects:
-- country (TABLE)
-- spGetItems (STORED PROC)
-- spDeleteItem (STORED PROC)
-- **********************************************************************************************************
SET NOCOUNT ON;
-- ================================================================
-- Creating the data tables and populating with test data
-- ================================================================
IF (EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'country'))
BEGIN
DROP TABLE [dbo].[country]
END
GO
CREATE TABLE [dbo].[country]
(
[country_id] [int] IDENTITY(1,1) PRIMARY KEY,
[country_name] [nvarchar] (200) NULL,
[country_region] [nvarchar] (50) NOT NULL
)
GO
BEGIN
insert into [dbo].[country] ([country_name],[country_region]) values ('United Kingdom','EUROPE')
insert into [dbo].[country] ([country_name],[country_region]) values ('France','EUROPE')
insert into [dbo].[country] ([country_name],[country_region]) values ('Germany','EUROPE')
insert into [dbo].[country] ([country_name],[country_region]) values ('Spain','EUROPE')
insert into [dbo].[country] ([country_name],[country_region]) values ('Japan','ASIA')
insert into [dbo].[country] ([country_name],[country_region]) values ('India','ASIA')
END
GO
-- ================================================================
-- Creating the stored procedures to write and read from the tables
-- ================================================================
IF EXISTS (SELECT *
FROM sysobjects
WHERE id = object_id(N'[dbo].[spGetItems]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
DROP PROCEDURE [dbo].[spGetItems]
END
GO
-- This proc demonstrates passing 1 argument to the database and returning the results of a query
CREATE PROCEDURE [dbo].[spGetItems] @region varchar(50) = null
AS
BEGIN
SET NOCOUNT ON;
IF @region is null
SELECT country_name, country_region
FROM dbo.country;
ELSE
SELECT country_name, country_region
FROM dbo.Country
WHERE country_region = @region;
END
GO
-- This proc demonstrates executing a proc on the database that accepts 1 variable and returning a
-- a result indicating its success.
IF EXISTS (SELECT *
FROM sysobjects
WHERE id = object_id(N'[dbo].[spDeleteItem]')
and OBJECTPROPERTY(id, N'IsProcedure') = 1 )
BEGIN
DROP PROCEDURE [dbo].[spDeleteItem]
END
GO
CREATE PROCEDURE [dbo].[spDeleteItem]
@name varchar(50),
@rowcount int OUTPUT,
@err int OUTPUT
AS
BEGIN
DELETE FROM dbo.country
WHERE country_name = @name
SELECT @err=@@error, @rowcount = @@ROWCOUNT
End
GO