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
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'
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'
New Transact-SQL Syntax
New Statements, Functions, and Options
Changes for ANSI Compliance
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
Changes for ANSI Compliance
ANSI Support
CAST
COMMIT WORK and ROLLBACK WORK
PRIVILEGES
Default ANSI Settings
OFF for DB-Library
ON for ODBC
Changes to Objects
Data Types
Tables
Stored Procedures
Views
Triggers
Transact-SQL Cursors
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
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”
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
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
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
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
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
Lab 3.1: Changes to Transact-SQL and Objects
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
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
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
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
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
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')
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
Lab 3.2: Distributed Queries
Review
Accessing Object Information
New Transact-SQL Syntax
Changes to Objects
Distributed Queries