`

Fundamentals of Stored Procedures

阅读更多

http://www.functionx.com/sqlserver/Lesson17.htm

 

Stored Procedures

 

 

Fundamentals of Stored Procedures

 
 

Introduction

 

In Lesson 6, we had an introduction to some types of actions that could be performed on a database. These actions were called functions. The SQL provides another type of action called a stored procedure. If you have developed applications in some other languages such as Pascal or Visual Basic, you are probably familiar with the idea of a procedure. Like a function, a stored procedure is used to perform an action on a database.

 

Practical Learning Practical Learning: Introducing Stored Procedures

 
  1. Start Microsoft SQL Server Management Studio and log in to your server
  2. On the main menu, click File -> New -> Query With Current Connection
  3. To create a new database, enter the following code in the window
     
    -- =============================================
    -- Database: WattsALoan
    -- =============================================
    USE master
    GO
    -- Drop the database if it already exists
    IF  EXISTS (
    	SELECT name 
    		FROM sys.databases 
    		WHERE name = N'WattsALoan'
    )
    DROP DATABASE WattsALoan
    GO
    
    CREATE DATABASE WattsALoan
    GO
    -- =========================================
    -- Table: Employees
    -- =========================================
    USE WattsALoan
    GO
    
    IF OBJECT_ID('dbo.Employees', 'U') IS NOT NULL
      DROP TABLE dbo.Employees
    GO
    
    CREATE TABLE dbo.Employees
    (
        EmployeeID int identity(1,1) NOT NULL,
        EmployeeNumber nchar(10) NULL,
        FirstName varchar(20) NULL,
        LastName varchar(10),
        FullName AS ((LastName+ ', ') + FirstName),
        Title varchar(100),
        HourlySalary money,
        Username varchar(20),
        Password varchar(20),
        CONSTRAINT PK_Employees PRIMARY KEY(EmployeeID)
    )
    GO
    INSERT INTO dbo.Employees(EmployeeNumber, FirstName, LastName, Title, HourlySalary)
    VALUES('293747', 'Jeanne', 'Tryler', 'Accounts Manager', 22.24);
    GO
    INSERT INTO dbo.Employees(EmployeeNumber, FirstName, LastName, Title, HourlySalary)
    VALUES('492947', 'Helene', 'Gustman', 'Accounts Representative', 14.55);
    GO
    INSERT INTO dbo.Employees(EmployeeNumber, FirstName, LastName, Title, HourlySalary)
    VALUES('804685', 'Ernest', 'Thomas', 'Accounts Representative', 12.75);
    GO
    -- =========================================
    -- Table: LoanTypes
    -- =========================================
    USE WattsALoan
    GO
    
    IF OBJECT_ID('dbo.LoanTypes', 'U') IS NOT NULL
      DROP TABLE dbo.LoanTypes
    GO
    
    CREATE TABLE dbo.LoanTypes
    (
        LoanTypeID int identity(1,1) NOT NULL, 
        LoanType varchar(50) NOT NULL, 
        CONSTRAINT PK_LoanTypes PRIMARY KEY(LoanTypeID)
    );
    GO
    INSERT INTO LoanTypes(LoanType) VALUES('Personal Loan');
    GO
    INSERT INTO LoanTypes(LoanType) VALUES('Car Financing');
    GO
    INSERT INTO LoanTypes(LoanType) VALUES('Credit Card');
    GO
    INSERT INTO LoanTypes(LoanType) VALUES('Furniture Loan');
    GO
    -- =========================================
    -- Table: Customers
    -- =========================================
    USE WattsALoan
    GO
    
    IF OBJECT_ID('dbo.Customers', 'U') IS NOT NULL
      DROP TABLE dbo.Customers
    GO
    
    CREATE TABLE dbo.Customers
    (
        CustomerID int identity(1,1) NOT NULL,
        DateCreated datetime NULL,
        FullName varchar(50) NOT NULL,
        BillingAddress varchar(100),
        BillingCity varchar(50),
        BillingState varchar(50),
        BillingZIPCide varchar(10),
        EmailAddress varchar(100),
        CONSTRAINT PK_Customers PRIMARY KEY(CustomerID)
    )
    GO
    INSERT INTO Customers(DateCreated, FullName,
        BillingAddress, BillingCity, BillingState,
        BillingZIPCide, EmailAddress)
    VALUES('2/26/2004', 'Julius Ramse',
           '927 Feuler Ave', 'Silver Spring', 
           'MD', '20904', 'ramses1990@netscape.net');
    GO
    INSERT INTO Customers(DateCreated, FullName,
        BillingAddress, BillingCity, BillingState,
    	BillingZIPCide)
    VALUES('06/22/2006', 'Gertrude Vaillant',
           '10055 Larsenic Rd', 'Takoma Park', 
           'MD', '20910');
    GO
    INSERT INTO Customers(DateCreated, FullName,
        BillingAddress,	BillingCity, BillingState,
    	BillingZIPCide, EmailAddress)
    VALUES('12/3/2004', 'James Barrouch',
           '4204 Fallon Drive', 'Silver Spring',
           'MD', '20906', 'barrouchj@hotmail.com');
    GO
    INSERT INTO Customers(DateCreated, FullName,
        BillingAddress,	BillingCity, BillingState,
    	BillingZIPCide)
    VALUES('08/02/2006', 'Christine Rougher',
           '825 Manning Street', 'Alexandria', 
           'VA', '22231');
    GO
    INSERT INTO Customers(DateCreated, FullName,
        BillingAddress,	BillingCity, BillingState,
    	BillingZIPCide, EmailAddress)
    VALUES('10/08/2006', 'Patrick Heller',
           '2480 Clarington Drive NW', 'Washington', 
           'DC', '20006', 'hellerp@yahooo.com');
    GO
    -- =========================================
    -- Table: LoanAllocation
    -- =========================================
    USE WattsALoan
    GO
    
    IF OBJECT_ID('dbo.LoanAllocations', 'U') IS NOT NULL
      DROP TABLE dbo.LoanAllocations
    GO
    
    CREATE TABLE dbo.LoanAllocations
    (
        LoanAllocationID int identity(1,1) NOT NULL,
        DatePrepared datetime NOT NULL, 
        EmployeeID int NULL
    		CONSTRAINT FK_LoanPreparer
    		FOREIGN KEY REFERENCES Employees(EmployeeID),
        CustomerID int NOT NULL
    		CONSTRAINT FK_LoanReceiver
    		FOREIGN KEY REFERENCES Customers(CustomerID),
        AccountNumber char(10),
        LoanTypeID int NOT NULL
    		CONSTRAINT FK_LoanTypes
    		FOREIGN KEY REFERENCES LoanTypes(LoanTypeID),
        LoanAmount money NOT NULL,
        InterestRate decimal(6,2) NOT NULL,
        Periods decimal(6,2) NOT NULL,
        InterestAmount AS ((LoanAmount*(InterestRate/(100)))*(Periods/(12))),
        FutureValue AS (LoanAmount+(LoanAmount*(InterestRate/(100)))*(Periods/(12))),
        MonthlyPayment AS ((LoanAmount+(LoanAmount*(InterestRate/(100)))*(Periods/(12)))/Periods),
        Notes Text,
        CONSTRAINT PK_LoanAllocations PRIMARY KEY(LoanAllocationID)
    )
    GO
    INSERT INTO LoanAllocations(DatePrepared, EmployeeID,
    	CustomerID, AccountNumber, LoanTypeID, LoanAmount,
        InterestRate, Periods, Notes)
    VALUES('2/26/2004', 2, 1, '9171394', 4, 6500.00, 12.65, 36, 
           'The loan will be delivered by our furniture business partner Helios Furnian');
    GO
    INSERT INTO LoanAllocations(DatePrepared, EmployeeID,
    	CustomerID, AccountNumber, LoanTypeID, LoanAmount,
        InterestRate, Periods, Notes)
    VALUES('06/22/2007', 2, 2, '8628064', 2, 16500.00, 10.20, 60, 
           'For this car loan, our partner Arlington Honda will process and deliver the car.');
    GO
    INSERT INTO LoanAllocations(DatePrepared, EmployeeID,
    	CustomerID, AccountNumber, LoanTypeID, LoanAmount,
        InterestRate, Periods, Notes)
    VALUES('12/3/2006', 1, 3, '8468364', 3, 500.00, 18.65, 48, 
           'This is a regular credit card.');
    GO
    INSERT INTO LoanAllocations(DatePrepared, EmployeeID,
    	CustomerID, AccountNumber, LoanTypeID, LoanAmount,
        InterestRate, Periods, Notes)
    VALUES('08/02/2006', 3, 4, '2483047', 1, 3500.00, 12.74, 36, 
           'This is personal/cash loan allocated to a customer who walked in the store and requested it.');
    GO
    INSERT INTO LoanAllocations(DatePrepared, EmployeeID,
    	CustomerID, AccountNumber, LoanTypeID, LoanAmount,
        InterestRate, Periods, Notes)
    VALUES('10/08/2006', 2, 5, '1311804', 4, 22748.36, 12.28, 60, 
           'This is a regular car financing loan');
    GO
    -- =========================================
    -- Table: Payments
    -- =========================================
    USE WattsALoan
    GO
    
    IF OBJECT_ID('dbo.Payments', 'U') IS NOT NULL
      DROP TABLE dbo.Payments
    GO
    
    CREATE TABLE dbo.Payments
    (
        PaymentID int identity(1, 1) NOT NULL,
        PaymentDate datetime NOT NULL, 
        EmployeeID int NULL
    		CONSTRAINT FK_Employees
    		FOREIGN KEY REFERENCES Employees(EmployeeID),
        LoanAllocationID int NOT NULL
    		CONSTRAINT FK_LoanAllocations
    		FOREIGN KEY REFERENCES LoanAllocations(LoanAllocationID),
        PaymentAmount money NOT NULL,
        Balance money,
        Notes Text,
        CONSTRAINT PK_Payments PRIMARY KEY(PaymentID)
    )
    GO
  4. To execute the code, press F5
  5. In the Object Explorer, expand the Databases node if necessary and expand WattsALoan
  6. Click Database Diagram
  7. When the message box comes up, read it and click Yes
  8. Right-click Database Diagram and click New Database Diagram...
  9. In the dialog box, double-click each table and, when all tables have been added, click Close
     
  10. Save the diagram as dgmWattsALoan and close it 

 

分享到:
评论

相关推荐

    MySql存储过程编程.chm

    Part I: Stored Programming Fundamentals Chapter 1. Introduction to MySQL Stored Programs Section 1.1. What Is a Stored Program? Section 1.2. A Quick Tour Section 1.3. Resources for Developers...

    Learning PostgreSQL 10

    Understand the fundamentals of relational databases, relational algebra, and data modeling Install a PostgreSQL cluster, create a database, and implement your data model Create tables and views, ...

    Beginning C# 2008 Databases From Novice to Professional

    How to write stored procedures in T–SQL and call them from C# programs How to use XML in database applications How to use LINQ to simplify C# database programming How to install SQL Server 2005 ...

    Beginning T-SQL with Microsoft SQL Server 2005 and 2008

    The book concludes with a comprehensive set of reference appendixes for command syntax, system stored procedures, information schema views, file system commands, and system management commands. ...

    oracle 小布学习视频重点图解Fundamentals_I

    4. **数据库对象**:了解并创建各种数据库对象,如表(Table)、视图(View)、索引(Index)、存储过程(Stored Procedures)和函数(Functions)。 5. **用户管理和权限**:学习如何创建和管理用户,理解角色...

    SQL_Fundamentals_I_VOL_2

    ##### 4.3 存储过程(Stored Procedures) - **存储过程**:预先编写的SQL代码块,可以在数据库中存储并重复调用。它们可以提高应用程序的性能和安全性。 #### 五、实践案例与练习 为了帮助考生更好地理解并掌握上述...

    Inside SQL Server 2005 Tools

    721 <br>Appendix D Complete List of Data Mining Stored <br>Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 723 <br>Index . . . . . . . . . . . . . . . . . . . . . . . . ...

Global site tag (gtag.js) - Google Analytics