1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?

Slides:



Advertisements
Similar presentations
PHP II Interacting with Database Data. The whole idea of a database-driven website is to enable the content of the site to reside in a database, and to.
Advertisements

AS ICT Finding your way round MS-Access The Home Ribbon This ribbon is automatically displayed when MS-Access is started and when existing tables.
AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
Using Visual Basic 6.0 to Create Web-Based Database Applications
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
Database Connectivity Rose-Hulman Institute of Technology Curt Clifton.
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Sara SartoliAkbar Siami Namin NSF-SFS workshop July 14-18, 2014.
Chapter 7 Managing Data Sources. ASP.NET 2.0, Third Edition2.
8/6/ ITE 370: SQL Stored Procedures. 8/6/ Stored Procedures A stored procedure is A stored procedure is a collection of SQL statements saved.
Stored Procedures Dr. Ralph D. Westfall May, 2009.
Module 2: Using Transact-SQL Querying Tools. Overview SQL Query Analyzer Using the Object Browser Tool in SQL Query Analyzer Using Templates in SQL Query.
CHAPTER 9 DATABASE MANAGEMENT © Prepared By: Razif Razali.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 9 Using the SqlDataSource Control. References aspx.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
UNIT TESTING FOR SQL Prepared for SUGSA CodeLabs Alain King Paul Johnson.
1 PHP and MySQL. 2 Topics  Querying Data with PHP  User-Driven Querying  Writing Data with PHP and MySQL PHP and MySQL.
Using Visual Basic 6.0 to Create Web-Based Database Applications
PHP meets MySQL.
9 Chapter Nine Compiled Web Server Programs. 9 Chapter Objectives Learn about Common Gateway Interface (CGI) Create CGI programs that generate dynamic.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
Dexterity | CONFIDENTIAL 2009 MRO | Analytics | Insights 1 Stored Procedures.
Stored Procedures, Transactions, and Error-Handling
Module 8: Implementing Stored Procedures. Introducing Stored Procedures Creating, Modifying, Dropping, and Executing Stored Procedures Using Parameters.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
NMED 3850 A Advanced Online Design January 12, 2010 V. Mahadevan.
By: Matt Batalon, MCITP  Another form of temporary storage that can be queried or joined against, much like a table variable, temp.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
Programmatic SQL Shaista Khan CS 157B. Topic Embedded SQL statements in high-level programming languages.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Web Server Administration Chapter 7 Installing and Testing a Programming Environment.
Slide 1 of 19Session 13 Ver. 1.0 Querying and Managing Data Using SQL Server 2005 In this session, you will learn to: Implement stored procedures Implement.
BlackBerry Applications using Microsoft Visual Studio and Database Handling.
Implementing and Using the SIRWEB Interface Setup of the CGI script and web procfile Connecting to your database using HTML Retrieving data using the CGI.
Creating a simple database This shows you how to set up a database using PHPMyAdmin (installed with WAMP)
INSERT BOOK COVER 1Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall. Exploring Getting Started with VBA for Microsoft Office 2010 by.
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
Mr. Justin “JET” Turner CSCI 3000 – Fall 2015 CRN Section A – TR 9:30-10:45 CRN – Section B – TR 5:30-6:45.
© 2007 by Prentice Hall2-1 Introduction to Oracle 10g Chapter 2 Overview of SQL and SQL*Plus James Perry and Gerald Post.
Using ADO.Net to Build a Login System Dr. Ron Eaglin.
Stored Procedures / Session 4/ 1 of 41 Session 4 Module 7: Introducing stored procedures Module 8: More about stored procedures.
Oracle10g Developer: PL/SQL Programming1 Objectives Named program units How to identify parameters The CREATE PROCEDURE statement Creating a procedure.
Ch 5. Introducing More Database Objects. Database Objects Table (ch2) View (ch3) Stored Procedure Trigger Function User-defined types.
IMS 4212: Application Architecture and Intro to Stored Procedures 1 Dr. Lawrence West, Management Dept., University of Central Florida
SQL Query Analyzer. Graphical tool that allows you to:  Create queries and other SQL scripts and execute them against SQL Server databases. (Query window)
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
Creating Functions If you came to this presentation via a web browser, right-click and choose “Full Screen” before proceeding. Click mouse or press space.
Advanced Database & Client Server Introduction to MS SQL Server 2000 and Transact SQL -
Create Stored Procedures and Functions Database Management Fundamentals LESSON 2.4.
SQL Injection By Wenonah Abadilla. Topics What is SQL What is SQL Injection Damn Vulnerable Web App SQLI Demo Prepared Statements.
Dynamic SQL Writing Efficient Queries on the Fly ED POLLACK AUTOTASK CORPORATION DATABASE OPTIMIZATION ENGINEER.
In this session, you will learn to: Create and manage views Implement a full-text search Implement batches Objectives.
Creating Functions This presentation was prepared by Professor Steve Ross, with the advice of other MIS Faculty, for use in MIS Classes at Western Washington.
ASP – Web Programming Class  Ravi Anand. ASP – Active Server Pages What is ASP? - Microsoft Technology - Can Run using IIS/PWS/Others - Helps us create.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 11 Creating Web Applications and Writing Data to a Database.
Stored Procedures Dr. Ralph D. Westfall May, 2011.
CS320 Web and Internet Programming SQL and MySQL
Dynamic SQL Writing Efficient Queries on the Fly
Chapter 2 – Introduction to the Visual Studio .NET IDE
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
Dynamic SQL Writing Efficient Queries on the Fly
Introduction To Structured Query Language (SQL)
Accessing Databases with ADO.NET, Handling Exceptions, and Printing
CS3220 Web and Internet Programming SQL and MySQL
CS3220 Web and Internet Programming SQL and MySQL
COP 2700 – Data Structures (SQL)
Presentation transcript:

1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?

2 What ? A named script made up of SQL & T-SQL statements Can call other procedures Can have input and / or output parameters Is stored in the database May be user defined or system provided Provides a good mechanism for error handling

3 A Stored Procedure is an Object stored in the Database table1 table2 table4 sp1 sp2 sp3 tr1 tr2tr3 view1view2

4 What ? The procedure's name is stored in the SysObjects Table The text is stored in the SysComments Table The execution plan is stored in the procedure cache May be external: Extended SP's – DLL's

5 Why ? A T-SQL script is compiled and optimised every time it is run. A Stored Procedure is compiled,optimised and stored. A Stored Procedure is much more efficient Can be reused. Provides a programming framework Provides security features

6 How ? -EM In Enterprise Manager: select your database, then right click on Stored Procedures and select New Stored Procedure CREATE PROCEDURE ex_1 AS SELECT * FROM Staff;

7 Enterprise Manager

8 How? -Templates T SQL frameworks that you can modify Found on Query Analyser top menu bar

9 How ? Use the Object Browser Icon on Query Analyser top menu bar Tools menu (show / hide) or F8 to toggle Right Click / Edit

10

11 How ? Directly –EXECUTE proc_name –EXEC ex_1 Use within another command –INSERT INTO tablename EXEC proc_name (refresh F5 / right click on database)

12 System Stored Procedures sp_help sp_helpconstraint sp_rename sp_who rename & drop – no cascade

13 Example 1 Create Procedure Pr_Insert_One AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (1, 'Anderson', 'Neo'); RETURN Exec Pr_Insert_One

14 Example 2 Create Procedure Pr_Insert_Two AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (2, 'Smith', 'Mr'); RETURN EXEC Pr_Insert_Two

15 use of message window

16

17 Error Handling LEVEL STATE

18 Using Stored Procedures from a Web site Create the procedure in your SQL Server database. Create a web page to –connect to the server –execute the stored procedure Enhance the web page to send values to the database (input parameters) Enhance the web page to receive & display results (output parameters) from the database

19 Enhancing Stored Procedures Variables Input parameters Output Parameters Transactions Error Handling

20 Why use parameters? Simplify procedures Reuse procedures

21 Example 1 Example 2 Create Procedure Pr_Insert_One AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (1, 'Anderson', 'Neo'); RETURN Exec Pr_Insert_One Create Procedure Pr_Insert_Two AS INSERT INTO staff (staff_Id, last_name, first_name) VALUES (2, 'Smith', 'Mr'); RETURN EXEC Pr_Insert_Two

22 Example 3 Parameters in Stored Procedures Create Procedure Pr_Insert_Three varchar varchar(30) ) AS INSERT INTO staff (staff_Id, last_name, RETURN exec Pr_Insert_Three 333,Smith, Mary exec Pr_Insert_Three '444','Smith','Mary' exec =555, Smith exec input variable rather than column

23 Input Parameters tables screen grid stored procedure input parameter values data values from tables results

24 Input & Output Parameters tables stored procedure 1 input parameter values data values from tables output parameters may become input parameters stored procedure 2

25 Input & Output Parameters tables stored procedure 1 input parameter values data values from tables output parameters may become input parameters stored procedure 2

26 Example 5 Output Parameter Create Procedure Pr_five varchar(30) output ) AS = last_name + first_name from staff where staff_id RETURN

27 Using the output value varchar(30) exec pr_five output local variable SmithFred message window

28 Using a Scalar in Select varchar(30) exec pr_five output select "Full Name " varchar(30) exec pr_five output "Full Name " results in grid window

29

30 alter Procedure Pr_six varchar(30) output ) AS <0 BEGIN Print 'You must enter a positive value' return END = last_name + first_name from staff where staff_id RETURN Must use BEGIN.... END Error Handling

31 create Procedure Pr_seven varchar(30) output ) AS <0 BEGIN RAISERROR ('You must enter a positive value',16,10) RETURN END = last_name + first_name from staff where staff_id RETURN Message appears in the messages window RAISERROR

32 Using Parameters from a Web Page Web PageWeb Server DBMS VB Script including SP name +parameter values SP name +parameter values return values

33 Web Page Set up

34 Sub Page_Load(Sender As Object, E As EventArgs) ' TODO: Update the ConnectionString for your application Dim ConnectionString As String = "server=Galba;database=Northwind;uid=******;pwd=******;" ' TODO: Updatd the name of the Stored Procedure for your application Dim CommandText As String = "CustOrdersDetail" Dim myConnection As New SqlConnection(ConnectionString) Dim myCommand As New SqlCommand(CommandText, myConnection) Dim workParam As New SqlParameter() myCommand.CommandType = CommandType.StoredProcedure ' TODO: Set the input parameter, if necessary, for your application SqlDbType.Int).Value = Inputparam.text myConnection.Open() DataGrid1.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection) DataGrid1.DataBind() End Sub 1 2 3

35 Simple Stored Procedure

36 Stored Procedures What ? –named pieces of SQL & T-SQL code Why ? –compiled & stored in the database –reusable How ? –EM –QA –Web page Why use parameters? Simplify procedures Reuse procedures