SQL Server Stored Procedures.

Slides:



Advertisements
Similar presentations
Procedure and Functions. Procedures Syntax: [CREATE [OR REPLACE]] PROCEDURE Pname [(p1, p2…)][IS | AS] [declarations] BEGIN [Execution Statements] [EXCEPTIONS.
Advertisements

Transact-SQL. 1. Declare float = 10 select * from customers where discnt
Stored procedures Procedural programming in Microsoft SQL Server 1Stored procedures.
Module 9: Implementing Stored Procedures. Introduction to Stored Procedures Creating Executing Modifying Dropping Using Parameters in Stored Procedures.
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.
Advanced SQL: Stored Procedures Instructor: Mohamed Eltabakh 1.
2015 International TechNet Wiki Summit 2015 Saeid Hasani Structured Error Handling Mechanism in SQL Server 2012 & 2014.
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.
Module 12 Handling Errors in T-SQL Code. Module Overview Understanding T-SQL Error Handling Implementing T-SQL Error Handling Implementing Structured.
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.
Stored Procedures A stored procedure is a named collection of SQL statements language. You can create stored procedures for commonly used functions and.
Lecture 4 PL/SQL language. PL/SQL – procedural SQL Allows combining procedural and SQL code PL/SQL code is compiled, including SQL commands PL/SQL code.
Defining Stored Procedures Named Collections of Transact-SQL Statements Encapsulate Repetitive Tasks Five Types (System, Local, Temporary, Remote, and.
Dinamic SQL & Cursor. Why Dinamic SQL ? Sometimes there is a need to dynamically create a SQL statement on the fly and then run that command. This can.
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.
Module 1: Introduction to Transact-SQL
T-SQL Transact-SQL is microsoft implementation of SQL. It contains additional programming constracts T-SQL enables you to write programs that contain SQL.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Advanced SQL: Cursors & Stored Procedures
SQL Server. اسکریپت درج مقدار در جدول USE Accounting; int; INSERT INTO Orders (CustomerNo,OrderDate, EmployeeID) VALUES (gETDATE,1); SELECT.
Stored Procedures Week 9. Test Details Stored Procedures SQL can call code written in iSeries High Level Languages –Called stored procedures SQL has.
Module 8: Implementing Stored Procedures. Overview Implementing Stored Procedures Creating Parameterized Stored Procedures Working With Execution Plans.
PL/SQL Oracle's Database Programming Language. Remember: Set serveroutput on With serveroutput off (default) executing procedure: With serveroutput on:
10 | Programming with Transact-SQL Graeme Malcolm | Senior Content Developer, Microsoft Geoff Allix | Principal Technologist, Content Master.
1 SQL Constraints and Programming. 2 Agenda Constraints in SQL Systems aspects of SQL.
06 | Modifying Data in SQL Server Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
SQL Server User Defined Functions. CREATE FUNCTION [ schema_name. ] function_name ( [ [ AS ][ type_schema_name. ] parameter_data_type.
G. Green 1.  Options include:  Script Files  already covered  APIs  last course topic  Database-Stored Code  our focus 2.
IBuySPY Shopping Store. Data Model for IBuySPY Shopping Store.
Stored Procedure Optimization Preventing SP Time Out Delay Deadlocking More DiskReads By: Nix.
Slide 1 Chapter 7 – Part 3 Stored Procedure, Function &Trigger.
SQL Server 2012 Session: 1 Session: 12 Triggers Data Management Using Microsoft SQL Server.
Module 3: Using XML. Overview Retrieving XML by Using FOR XML Shredding XML by Using OPENXML Introducing XQuery Using the xml Data Type.
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
IT420: Database Management and Organization Triggers and Stored Procedures 24 February 2006 Adina Crăiniceanu
Ch 3. Working with Tables and Views. Data type Specify type of data to be entered into a column (text, number, datetime, etc) Unicode (National) Datatypes.
Ch 7. Working with relational data. Transactions Group of statements executed as a group. If all statements execute successfully, changes are committed.
Advanced SQL: Cursors & Stored Procedures Instructor: Mohamed Eltabakh 1.
Course 2778A Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008 Presentation: 60 minutes Lab: 60 minutes Module 1: Getting Started.
Module 9: Using Advanced Techniques. Considerations for Querying Data Working with Data Types Cursors and Set-Based Queries Dynamic SQL Maintaining Query.
Module 8: Using Programming Objects for Data Retrieval.
1 Stored Procedure, Function and Trigger. 2Objectives 1. Database Programming 2. Stored Procedure 3. Function 4. Trigger.
Implementing Functions Advanced Database Dr. AlaaEddin Almabhouh.
1 c6212 Advanced Database and Client Server MS SQL Server 2000 Stored Procedures and Parameters What ? Why ? How ?
Service Broker Overview Janis Griffin Senior DBA, Confio Software
Creating Database Objects
BE PREPARED Error Handling & Optimizing T-SQL Code in SQL Server 2000
User-defined functions, Procedures, Triggers and Transactions
Task oriented processing
Module 2: Creating Data Types and Tables
11 | Error Handling and Transactions
10 | Programming with Transact-SQL
PROCEDURES, CONDITIONAL LOGIC, EXCEPTION HANDLING, TRIGGERS
STORED PROCEDURES AND FUNCTION (9.6.1)
මොඩියුල විශ්ලේෂණය Buffer Pool Extension භාවිතය.
T-SQL Transact - Structured Query Language (Microsoft)
Stored Procedure, Function and Trigger
Super Scaling The LMAX Queue Pattern.
මොඩියුල විශ්ලේෂණය Stored Procedure හඳුන්වා දීම.
Special Registers, Date functions, Case and User Defined Functions!!
Creating Database Objects
Presentation transcript:

SQL Server Stored Procedures

CREATE PROCEDURE dbo.CategoryList WITH ENCRYPTION AS SELECT ProductCategoryName, ProductCategoryDescription FROM dbo.ProductCategory; go EXEC sp_helptext ‘dbo.CategoryList’; Result: The text for object ‘dbo.CategoryList’ is encrypted.

اجرای یک رویه راه دور EXEC [MAUINoli\SQL2COPENHAGEN].OBXKites.dbo.pProductCategory_AddNew ‘Food’, ‘Eatables’; SELECT CustomerTypeName, DiscountPercent, [Default] FROM OPENQUERY( [MAUI\COPENHAGENNoli\SQL2], ‘EXEC OBXKites.dbo.pCustomerType_Fetch;’);

USE OBXKites; go CREATE PROCEDURE dbo.CategoryGet ( @CategoryName NVARCHAR(35) )AS SELECT ProductCategoryName, ProductCategoryDescription FROM dbo.ProductCategory WHERE ProductCategoryName = @CategoryName; Go EXEC dbo.CategoryGet N’Kite’;

CREATE PROCEDURE dbo.pProductCategory_Fetch2 @Search NVARCHAR(50) = NULL -- If @Search = null then return all ProductCategories -- If @Search is value then try to find by Name AS SET NOCOUNT ON; SELECT ProductCategoryName, ProductCategoryDescription FROM dbo.ProductCategory WHERE ProductCategoryName = @Search OR @Search IS NULL; IF @@RowCount = 0 BEGIN; RAISERROR( ‘Product Category ‘’%s" Not Found.’,14,1,@Search); END;

Create Procedure with Table-Valued Parameters CREATE TYPE OrderDetailsType AS Table ( LineNumber INT, ProductID INT, IsNew BIT, IsDirty BIT, IsDeleted BIT );

Create Procedure with Table-Valued Parameters CREATE PROC OrderTransactionUpdateTVP (@OrderID INT OUTPUT, @CustomerID INT, @OrderDate DateTime,@Details as OrderDetailsType READONLY)AS SET NoCount ON ; Begin Try Begin Transaction; -- If @OrderID is NULL then it’s a new order, so Insert Order If @OrderID IS NULL BEGIN; Insert Orders(OrderDate, CustomerID) Values (@OrderDate, @CustomerID); SET @OrderID = Scope_Identity(); END; SELECT * FROM @Details ; Commit Transaction; End Try Begin Catch; RollBack; End Catch RETURN;

نحوه فراخوانی رویه با پارامتر از نوع جدول Declare @OrderID INT; DECLARE @DetailsTVP as OrderDetailsType; INSERT @DetailsTVP (LineNumber,ProductID,IsNew,IsDirty,IsDeleted) VALUES (5, 101, -1, -1, 0), (2, 999, 0, -1, 0), (3, null, 0, 0, 0); EXEC OrderTransactionUpdateTVP @OrderID = @OrderID Output , @CustomerID = ‘78’, @OrderDate = ‘2008/07/24’, @Details = @DetailsTVP;

برگرداندن مقدار از رویه USE OBXKites; go CREATE PROC dbo.GetProductName ( @ProductCode CHAR(10), @ProductName VARCHAR(25) OUTPUT) AS SELECT @ProductName = ProductName FROM dbo.Product WHERE Code = @ProductCode; RETURN;

نحوه فراخوانی رویه اخیر USE OBXKites; DECLARE @ProdName VARCHAR(25); EXEC dbo.GetProductName ‘1001’, @ProdName OUTPUT; PRINT @ProdName;

RETURNاستفاده از CREATE PROC dbo.IsItOK ( @OK VARCHAR(10))AS IF @OK = ‘OK’ BEGIN; RETURN 0; END; ELSE RETURN -100;

Procedureشکل کلی دستور ایجاد CREATE PROCEDURE|PROC <name> [<parameter name> [schema.]<data type> [VARYING] [= <default value>] [OUT [PUT]] [READONLY] [, ……]] [WITH RECOMPILE| ENCRYPTION | [EXECUTE AS { CALLER|SELF|OWNER|<’user name’>}] AS <code> | EXTERNAL NAME <assembly name>.<assembly class>.<method>

RAISERROR (<message ID | message string | variable>, <severity>, <state> [, <argument> [,<...n>]] ) [WITH LOG|SETERROR|NOWAIT]

sp_addmessage [@msgnum =] <msg id>, [@severity =] <severity>, [@msgtext =] <’msg‘> [, [@lang =] <’language‘>] [, [@with_log =] [TRUE|FALSE]] [, [@replace =] ‘replace‘]

sp_addmessage @msgnum = 60000, @severity = 10, @msgtext = ‘%s is not a valid Order date. Order date must be within 7 days of current date.’;

sp_dropmessage <message number>