When programming in SQL, you have probably created
various types of SQL statements to execute many common actions. You use
SQL statements to create databases, create tables, modify tables, or
perform data entry. These actions are performed by you the programmer as
you need them. In SQL, you can create a statement to be treated as an
object and use it only when needed. You can create the statement as an
object called a stored procedure.
Practical
Learning: Creating the Database
|
|
- Start Microsoft SQL Server Management Studio and click Connect to connect
to the database server
- To create a database, right-click the Databases node and click New
Database...
- Set the Database Name to SuperMarket and click OK
- Expand the SuperMarket node and the Tables node
- Right-click Tables and click click New Table...
- Set the name of the first column to EmployeeID and its data Type to
int
- In the lower section of the window, expand the Identity Specification and
set the (Is Identity) to Yes
- Right-click EmployeeID and click Set Primary Key
- Create the other columns as follows:
Column Name |
Data Type |
Allow Nulls |
EmployeeID |
|
|
FirstName |
varchar(20) |
|
LastName |
varchar(20) |
Unchecked |
FullName |
varchar(50) |
|
HourlySalary |
smallmoney |
|
- Close the table
- When asked whether you want to save, click Yes
- Set the table name to Employees and press Enter
- Under the Tables node of the SuperMarket database, right-click
dbo.Employees and click Open Table
- Fill it up with a few records as follows:
FirstName |
LastName |
HourlySalary |
Anselme |
Roberts |
14.82 |
Justine |
Keys |
5.85 |
Edward |
Ross |
22.15 |
Tracey |
Kirkland |
6.88 |
Kimberly |
Eisner |
8.58 |
Jonathan |
Adamson |
10.95 |
Steve |
Fox |
9.44 |
Andrew |
Buroughs |
6.15 |
Randy |
Ettenson |
18.04 |
Patrick |
Swanson |
12.48 |
- Close the table
A stored procedure is a SQL statement created and
saved in a database as an object. Before creating a stored procedure, you
must identify the desired database, expand its Programmability node,
right-click its Stored Procedures node, and click New Stored Procedure...
The code of a stored procedure primarily that of a regular SQL statement.
After creating the SQL statement, you must execute it. You can do this by
clicking the Execute button. Once the stored procedure has been executed,
its name is added as a new item to the Stored Procedures node of the
database. This indicates that it is ready to be used.
|
|