Transact SQL The language of Microsoft SQLServer Copyright © 2012 – 2014 by Curt Hill.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

AN INTRODUCTION TO PL/SQL Mehdi Azarmi 1. Introduction PL/SQL is Oracle's procedural language extension to SQL, the non-procedural relational database.
PL/SQL. Introduction to PL/SQL PL/SQL is the procedure extension to Oracle SQL. It is used to access an Oracle database from various environments (e.g.
Chapter 4B: More Advanced PL/SQL Programming
PHP Intro/Overview Squirrel Book pages Server-side Scripting Everything you need to know in one slide 1.Web server (with PHP “plug-in”) gets a.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
A Guide to Oracle9i1 Advanced SQL And PL/SQL Topics Chapter 9.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to PL/SQL Chapter 9. Objectives Explain the need for PL/SQL Explain the benefits of PL/SQL Identify the different types of PL/SQL blocks.
Introduction to scripting
Introduction to PL/SQL. Procedural Language extension for SQL Oracle Proprietary 3GL Capabilities Integration of SQL Portable within Oracle data bases.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2013.
Copyright © Curt Hill SQL The Data Definition Language.
Bordoloi and Bock CURSORS. Bordoloi and Bock CURSOR MANIPULATION To process an SQL statement, ORACLE needs to create an area of memory known as the context.
PL / SQL P rocedural L anguage / S tructured Q uery L anguage Chapter 7 in Lab Reference.
Using the Select Case Statement and the MsgBox Function (Unit 8)
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Week 9 PHP Cookies and Session Introduction to JavaScript.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
PHP meets MySQL.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
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.
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.
Stored Procedures, Transactions, and Error-Handling
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Input, Output, and Processing
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
Overview · What is PL/SQL · Advantages of PL/SQL · Basic Structure of a PL/SQL Block · Procedure · Function · Anonymous Block · Types of Block · Declaring.
Copyright © 2013 Curt Hill Database Security An Overview with some SQL.
Copyright Curt Hill SQL Queries Yet Another Set of Query Features.
Copyright © Curt Hill Stored Procedures In Transact-SQL.
Session Title: Using SQL and PL/SQL for Queries and Reporting Presented By: Stephen Frederic Institution: IHL September 16, 2014.
CpSc 462/662: Database Management Systems (DBMS) (TEXNH Approach) Stored Procedure James Wang.
LECTURE 1 INTRODUCTION TO PL/SQL Tasneem Ghnaimat.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
PL / SQL By Mohammed Baihan. What is PL/SQL? PL/SQL stands for Procedural Language extension of SQL. PL/SQL is a combination of SQL along with the procedural.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 Copyright © 2004, Oracle. All rights reserved. Introduction to PL/SQL.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 8 Advanced SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Chapter 15 Introduction to PL/SQL. Chapter Objectives  Explain the benefits of using PL/SQL blocks versus several SQL statements  Identify the sections.
Guide to Oracle 10g ITBIS373 Database Development Lecture 4a - Chapter 4: Using SQL Queries to Insert, Update, Delete, and View Data.
Copyright © Curt Hill Joins Revisited What is there beyond Natural Joins?
Copyright Curt Hill Variables What are they? Why do we need them?
Statement Level Flow of Control Iteration Structures Copyright © by Curt Hill.
A Guide to SQL, Eighth Edition Chapter Eight SQL Functions and Procedures.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Introduction to Scripting.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Function, Trigger used in PosgreSQL.
Copyright © 2013 Curt Hill Triggers The Generation of Indirect Actions.
Text TCS INTERNAL Oracle PL/SQL – Introduction. TCS INTERNAL PL SQL Introduction PLSQL means Procedural Language extension of SQL. PLSQL is a database.
Lab 2 Writing PL/SQL Blocks CISB514 Advanced Database Systems.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Copyright © Curt Hill SQL The Data Manipulation Language.
CS422 Principles of Database Systems Oracle PL/SQL Chengyu Sun California State University, Los Angeles.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1. Advanced SQL Functions Procedural Constructs Triggers.
Lawson Mid-America User Group Spring 2016 Meeting.
Chapter 6 JavaScript: Introduction to Scripting
MATLAB: Structures and File I/O
PHP Intro/Overview Bird Book pages 1-11,
PHP.
Programming in JavaScript
CMSC 202 Java Primer 2.
Programming in JavaScript
Chapter 8 Advanced SQL.
Database Systems: Design, Implementation, and Management Tenth Edition
COP 2700 – Data Structures (SQL)
Presentation transcript:

Transact SQL The language of Microsoft SQLServer Copyright © 2012 – 2014 by Curt Hill

SQL Most of the standard SQL is contained in the Data Definition Language and Data Manipulation Language Much, but not all, has been covered The problem is that can we do what we want to do with just this? Generally there is more programming going on

Example When you contact an online travel agency several things happen The origination and destination cities are given as well as dates and number flying System then searches for possible carriers Gives the user choices of flights Once chosen these are booked Receipts and the like are ed to user This has to be done by a program and only part of this program generates SQL Copyright © 2012 – 2014 by Curt Hill

Approaches How do we do the previous or similar tasks involving one or more databases? Three possibilities: –Embedded SQL –Generated SQL –Database Programming Language Copyright © 2012 – 2014 by Curt Hill

Embedded SQL A program written in a non-database programming language handles this –Java, C++, COBOL or any other general purpose language In the course of execution it issues SQL commands then modifies and displays the results –SQL commands are fixed strings in the program This is usually client side Copyright © 2012 – 2014 by Curt Hill

Generated SQL Similar to the above except the SQL commands are not fixed strings –Above languages as well as JavaScript, Perl, PhP The program generates them –Fills in tables, fields, and comparison constants depending on the need This could be on client side, server side or in-between (middleware) ERPs, CRMs and the like usually this Copyright © 2012 – 2014 by Curt Hill

Database Programming Language Do everything in extensions to standard SQL Language must include comments, operators, variables, flow of control, functions and the like General purpose languages may construct the types needed for a database Database Languages have them built in Copyright © 2012 – 2014 by Curt Hill

Transact SQL A Database Programming Language supported by Microsoft SQLServer –Most databases have similar language This presentation then looks at the rudiments of programming such a beast Copyright © 2012 – 2014 by Curt Hill

Comments Comments are ignored by SQLServer but allow people to read the code more easily Two types Multiline is like C –Starts with /* and ends with */ Single line –Starts with two dashes -- and ends at the end of line Copyright © 2012 – 2014 by Curt Hill

Operators We have seen most of these + addition or string concatenation - subtraction * multiplication / division % division for remainder or as a wildcard in a Like | bitwise OR & bitwise AND ~ bitwise NOT Comparisons: = = <> !> !< != Copyright © 2012 – 2014 by Curt Hill

Predefined Variables These represent available server values They always start with and amount of time used since last started – the error number of the last statement, zero means none – the number of rows affected by the last statement – disk read/write errors since start Copyright © 2012 – 2014 by Curt Hill

Variables Variables must be declared using a declare statement Variables must be named starting with a Declaration syntax: type –Type is any type you can use in a create table Multiple variables can be done in one Declare, separated by commas Initialization may use a trailing = value Copyright © 2012 – 2014 by Curt Hill

Display We display variables like we display anything else, by specifying in a select A Select that does not need any tables does not need a From clause or others We are now ready for our first program Copyright © 2012 – 2014 by Curt Hill

Assignment statements How do we change a variable’s value? With the set statement Format: = expression Where –Set is required reserved word is the variable to be changed –Expression is a constant, variable or variables and constants combined with operators Copyright © 2012 – 2014 by Curt Hill

Simple Display Copyright © 2012 – 2014 by Curt Hill -- Simple junky program bit = + /* Declares do not have to precede executables */ int @snerd,

In Management Console Copyright © 2012 – 2014 by Curt Hill

Cursors You might think we could declare a variable of type table and then assign a query result to that table That is not the way it is usually done Most languages provide a cursor into the resulting query The cursor looks at only one row However it may be moved forward or backward –We may also move to a specific numbered row Copyright © 2012 – 2014 by Curt Hill

About Cursors Although cursors are variables they do not use convention for names Their declaration is a little different as well Cursors are not general, but instead are tied to one specific query When we are done with them we release them –This frees the resources connected with them Copyright © 2012 – 2014 by Curt Hill

Declaration The format for a cursor is: DECLARE cur_name CURSOR FOR query Where –Declare, cursor and for are reserved –Cur_name is the name of this cursor –Query is a Select with From and as many options as you want Into is not allowed Copyright © 2012 – 2014 by Curt Hill

Cursor actions Open –The parameter is the cursor –This is when the query is executed Close –Closes a cursor but retains the data to be reopened Fetch – See next slide Deallocate –Releases the cursor and the query values –Parameter is cursor Copyright © 2012 – 2014 by Curt Hill

Fetch Determines which row to get from the query results Format: Fetch direction From Where Fetch, from, into are reserved words are local variables to receive values Direction is next, prior, first, last, absolute n or relative n Copyright © 2012 – 2014 by Curt Hill

Fetch Again When the cursor is open the next one is the first one –You may start anywhere and scroll forward of backward The global variable contains and integer result –Value -1 means the fetch failed –Value -2 means row is no longer available If the value is negative then the variables are unchanged Copyright © 2012 – 2014 by Curt Hill

Rows does not work for cursors, only for queries will show the number of rows as soon as the cursor is opened Copyright © 2012 – 2014 by Curt Hill

Example Copyright © 2012 – 2014 by Curt Hill use college declare fac_curse cursor for select f_naid, f_name, f_age, f_years from faculty where f_age > 40 open fac_curse int fetch next from … deallocate fac_curse

Notes The number of variables given in the Fetch should match the number of items in the Select If the into and variables are left off, we get output like a select Copyright © 2012 – 2014 by Curt Hill

Flow of Control We will look at two flow statements –If – a decision statement –While – a looping statement Before this we need to consider blocks SQL uses a Pascal-like BEGIN and END Begin and End wrap a group of statements –Makes them into a block Copyright © 2012 – 2014 by Curt Hill

If Format 1: if (condition) statement Format 2: if(condition) statement else statement Copyright © 2012 – 2014 by Curt Hill

Notes The condition may use the normal comparison operators with local variables and constants Either statement may be a BEGIN/END block If there is no else, it is the same as else do nothing Copyright © 2012 – 2014 by Curt Hill

While Format: while (condition) statement The statement may be a single statement or a BEGIN/END block Copyright © 2012 – 2014 by Curt Hill

Display Select sends output to the same place as a query result We also have a print command This sends output the messages The messages usually show how many rows were affected or errors Print may only show one string –This may be the concatenation of several Copyright © 2012 – 2014 by Curt Hill

Example Program Most things that you can do in a program, you could also do with just queries –It may just be harder In this program we will look at all faculty older than 40 In this group we will compute the ratio of years of service to age Display the least and greatest Copyright © 2012 – 2014 by Curt Hill

Setup Copyright © 2012 – 2014 by Curt Hill use college declare fac_curse cursor for select f_name, f_age, f_years from faculty where f_age > 40 open fac_curse real = varchar(20)

The Loop – First Half Copyright © 2012 – 2014 by Curt Hill fetch next from @years while <> -1) Begin = if begin end if begin end

The Loop – Last Half Copyright © 2012 – 2014 by Curt Hill if begin fetch next from @years end

Finish Up Copyright © 2012 – 2014 by Curt Hill deallocate fac_curse print 'Least: ' + ' ratio: ‘ + print 'Most: ' + ' ratio: ' + as varchar(20))

Oracle Addendum All the features that are present in TransactSQL are available in Oracle’s PL/SQL The syntax is enough different that substantial conversion would be needed An example follows, but does not represent a working program with everything properly defined Copyright © 2012 – 2014 by Curt Hill

Oracle Examples Copyright © 2012 – 2014 by Curt Hill DECLARE part_number NUMBER(6); bonus int := 0; BEGIN part_number := 2135; IF part_number > special THEN bonus := (sales - quota)/4; ELSE bonus := 50; END IF; WHILE bonus < 100 LOOP bonus := bonus + 10; count := count + 1; END LOOP; END;

Finally A decent reference to Transact SQL is greater than 150 pages –This does not include pre-defined stored procedured Clearly there is more to know However, this shows many of the fundamentals and basic flavor Copyright © 2012 – 2014 by Curt Hill