+ All Categories
Home > Documents > Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax...

Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax...

Date post: 11-Jan-2016
Category:
Upload: berniece-dorsey
View: 219 times
Download: 1 times
Share this document with a friend
26
Module 3: Changes to Transact-SQL
Transcript
Page 1: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Module 3: Changes to

Transact-SQL

Page 2: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Overview

Accessing Object Information

New Transact-SQL Syntax

Changes to Objects

Distributed Queries

Page 3: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Accessing Object Information

Referencing Objects

Viewing Metadata

Page 4: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Referencing Objects

Longer Object Names

Fully-Qualified Object Names

Bracketed Identifiers

Changing Object Ownership

northwind.dbo.[order details]northwind.dbo.[order details]

server.database.owner.objectserver.database.owner.object

EXEC sp_changedobjectowner 'products', 'Arturo'EXEC sp_changedobjectowner 'products', 'Arturo'

Page 5: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Viewing Metadata

Use System Stored Procedures

Use Information Schema Views

Use Metadata Functions

SELECT * FROM information_schema.tablesSELECT * FROM information_schema.tables

USE northwindIF OBJECTPROPERTY (object_id('products'), 'ISTABLE') IS NULL PRINT 'Products is not an object'

USE northwindIF OBJECTPROPERTY (object_id('products'), 'ISTABLE') IS NULL PRINT 'Products is not an object'

Page 6: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

New Transact-SQL Syntax

New Statements, Functions, and Options

Changes for ANSI Compliance

Page 7: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

New Statements, Functions, and Options

SET Statements and Options SET SHOWPLAN_ALL SET SHOWPLAN_TEXT SET @ local_variable = constant SET QUERY_GOVERNOR_COST_LIMIT

Functions @@LOCK_TIMEOUT, VAR, STDEV

SELECT Statement Options

TOP, PERCENT, WITH TIES

Page 8: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Changes for ANSI Compliance

ANSI Support

CAST

COMMIT WORK and ROLLBACK WORK

PRIVILEGES

Default ANSI Settings

OFF for DB-Library

ON for ODBC

Page 9: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Changes to Objects

Data Types

Tables

Stored Procedures

Views

Triggers

Transact-SQL Cursors

Page 10: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Data Types

Changes to Existing Data Types

decimal and numeric

char, varchar, and binary

sysname

text and image

New Types of Data

Unicode character Unicode text Global identifier Cursor

Page 11: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Tables

Changes to ALTER TABLE Statement

Can add a column with a default Can specify NOT NULL

Can drop a column with ALTER TABLE New Features for Columns

IDENTITY with NOT FOR REPLICATION uniqueidentifier with NEWID Computed columns

ALTER TABLE mytable ADD city varchar(20) NOT NULL DEFAULT “Tokyo”

ALTER TABLE mytable ADD city varchar(20) NOT NULL DEFAULT “Tokyo”

Page 12: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Stored Procedures

Delayed Name ResolutionAllows reference to objects created after stored procedureis created

ALTER PROCEDURE Modifies existing stored procedure without changing permissions

sp_procoptionSets and reports options that affect stored procedure execution

Page 13: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Views

Delayed Name Resolution

ALTER VIEW Maintains column permissions

USE northwindGOCREATE VIEW productview AS SELECT productname FROM products GO ALTER VIEW productview AS SELECT productname FROM products WHERE categoryid = 1GO

USE northwindGOCREATE VIEW productview AS SELECT productname FROM products GO ALTER VIEW productview AS SELECT productname FROM products WHERE categoryid = 1GO

Page 14: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Triggers

Syntax Changes

Delayed name resolution supported

Prevent trigger execution for replication

Can change trigger definition using ALTER TRIGGER

Multiple Triggers

Nested Triggers

Trigger Recursion

Page 15: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Transact-SQL Cursors

New cursor Data Type and Cursor Variables

New Options for DECLARE CURSOR Statement

LOCAL, GLOBAL, FORWARD_ONLY, STATIC,KEYSET, DYNAMIC, FAST_FORWARD,SCROLL_LOCKS, OPTIMISTIC, TYPE_WARNING

Retrieving Information About Cursors

CURSOR_STATUS Function

Page 16: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Upgrade Considerations for Transact-SQL

Remove References to System Tables from Scripts

Replace New Reserved Keywords in Scripts

Specify Object Owner Name in Scripts

Use Binary and Character Data Types for Data 8000 Bytes or Less

Check ANSI Settings

Alter Objects Rather Than Drop and Recreate Them

Page 17: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Lab 3.1: Changes to Transact-SQL and Objects

Page 18: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Distributed Queries

Linked Server Environments

Setting Up a Linked Server

Establishing Linked Server Security

Getting Information About Linked Servers

Executing Linked Server Queries

Executing Ad Hoc Queries

Page 19: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Linked Server Environments

Other Data Sources

Linked SQL Server

Linked SQL Server

SQL Server allowsaccess to otherdata sources

Linked serversmust be linked tothe local computerrunning SQL Server

Local SQL Server

Page 20: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Setting Up a Linked Server

Connecting to a Remote SQL Server

Connecting to an OLE DB Data Source

EXEC sp_addlinkedserver @server = 'AccountingServer', @product_name = 'SQL Server'

EXEC sp_addlinkedserver @server = 'AccountingServer', @product_name = 'SQL Server'

EXEC sp_addlinkedserver @server = 'OracleFinance', @product_name = 'Oracle',@provider_name = 'MSDORA', @data_source = 'OracleDB'-- 'OracleDB’ is the SQL*Net alias

EXEC sp_addlinkedserver @server = 'OracleFinance', @product_name = 'Oracle',@provider_name = 'MSDORA', @data_source = 'OracleDB'-- 'OracleDB’ is the SQL*Net alias

Page 21: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Establishing Linked Server Security

EXEC sp_addlinkedsrvlogin @rmtsrvname = AccountingServer, @useself = false,@locallogin = 'NULL',@rmtuser = 'allcustomers',

EXEC sp_addlinkedsrvlogin @rmtsrvname = AccountingServer, @useself = false,@locallogin = 'NULL',@rmtuser = 'allcustomers',

Local Server Logs In to Remote Server on User’s Behalf

To Establish Linked Server Security, You Can

Use the same user login accounts on both servers

Map login IDs between servers

Page 22: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Getting Information About Linked Servers

sp_linkedservers

sp_catalogs

sp_indexes

sp_primarykeys

sp_foreignkeys

sp_tables_ex

sp_columns_ex

USE masterEXEC sp_indexes 'CAIRO', 'employees', 'dbo', 'northwind', NULL, 0

USE masterEXEC sp_indexes 'CAIRO', 'employees', 'dbo', 'northwind', NULL, 0

Page 23: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Executing Linked Server Queries

Using Transact-SQL

Using Pass-through Queries

SELECT companyname FROM AccountingServer.northwindremote.dbo.suppliersSELECT companyname FROM AccountingServer.northwindremote.dbo.suppliers

SELECT * FROM OPENQUERY (AsiaServer, 'SELECT productid, FROM northwind.dbo.ProductInfo')

SELECT * FROM OPENQUERY (AsiaServer, 'SELECT productid, FROM northwind.dbo.ProductInfo')

Page 24: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Executing Ad Hoc Queries

Use the OPENROWSET Function:

To Access Remote Data Without Setting Up a Linked Server

When You Do Not Expect to Use the Data Source Repeatedly

SELECT a.* FROM OPENROWSET('SQLOLEDB', 'London1'; 'newcustomer';'mypassword', 'SELECT productid, unitprice FROM northwind.dbo.products ORDER BY unitprice')AS a

SELECT a.* FROM OPENROWSET('SQLOLEDB', 'London1'; 'newcustomer';'mypassword', 'SELECT productid, unitprice FROM northwind.dbo.products ORDER BY unitprice')AS a

Page 25: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Lab 3.2: Distributed Queries

Page 26: Module 3: Changes to Transact-SQL. Overview Accessing Object Information New Transact-SQL Syntax Changes to Objects Distributed Queries.

Review

Accessing Object Information

New Transact-SQL Syntax

Changes to Objects

Distributed Queries


Recommended