Professional Documents
Culture Documents
Manage Linked Server Definitions Sp_adlinkedserver / sp_dropserver Can also be done in SSMS
Sys.servers System catalog views used to see servers defined in a specific instance of SQL Server. Ad Hoc Names used for infrequent queries against OLE DB data sources not defined as linked servers. By default ad hoc names are not supported. The DisallowAdhocAccess provider option must be set to 0 and the AD Hoc Distributed Queries advanced configuration option must be enabled. o OPENROWSET and OPENDATASOURCE are referred to as functions, but are truly macros that do not support T-SQL statements as arguments. o OPENROWSET can be used with any OLE DB provider that returns a rowset, and can be used anywhere a table or view reference is used in a T-SQL statement. o OPENDATASOURCE provides connection information as part of a four-part object name. This function supports only OLE DB providers that expose multiple rowsets by using the catalog.schema.object notation.
Entity Data Model ( EDM )- abstracts the conceptual model from the physical database schema. An EDM model contains a conceptual model, a logical storage model, and mappings between the two models, which are each stored in special XML files. o The EDM model consists entities and relationships Entity can be exposed as objects, have properties and be instantiated in application code. Relationships relate entities in the connectional model and support inheritance between entities. o The Entity Framework uses the special XML files representing the model to translate the operations performed on the data model to operations against the respective tables in the underlying data source. Entity SQL Similar to SQL, but with different syntax. Developers can write data access code against the conceptual model with it. Entity Data Model Inheritance o Table-per-hierarchy inheritance model Can create an EDM that supports inheritance within a single database table. A Base Entity represents all elements within the table; other entities inherit different properties from the Base Entity. Steps: 1. Create a base entity (e.g. Products) 2. Create additional entities that inherit from it (e.g. Virtual Products and Physical Products) o Table-per-Type Inheritance Supports inheritance with multiple tables. A Base Entity contains all of the elements for a group of entities, where properties of each Entity are inherited from tables with a one-one relationship with the primary table.
Flexible Database Roles are user created and can be modified. Application Roles Users can only access the database through a custom application. User Authentication is handled by the application. The role has its own user-like permissions. o No Members - Application Roles do not contain users, only a password. o Sp_setapprole Stored procedure to enable Application Roles. o Database Level Principal Cannot see other databases or server level meta-data by default. o Application Role connection steps: User executes a client application The client application connects to an instance of SQL Server as the user. The application executes the sp_setapprole stored procedure with a password known only to the application. The Application Role is authenticated by the database. The connection switches to the permission of the Application Role DB Ownership o Only accounts in the db_owner role can create objects owned by dbo. o When a user is owner of one or more objects, the user cannot be deleted until either the objects are deleted or ownership is transferred. Ownership Chaining Referenced objects permissions are not checked when the calling object has the same owners as the object being called. Security Levels
Thesaurus Files XML files that can be modified to contain synonyms of pattern replacement, initially the default file contains the basic structure and a commented out sample thesaurus. o Sys.sp_fulltext_load_thesaurus_file
Must be used after modifying a thesaurus file before the changes are effective. Defining Mappings (Synonyms) Specific for each language file. Include all synonyms.
Diacritics Setting Each thesaurus is either sensitive or insensitive to diacritical marks, such as tilde (~), acute accent mark (), etc. Expansion Set Contains a group of synonyms such as Writer and Journalist. Queries that match any synonym are expanded to include all synonyms. Replacement Set Contains a text pattern to be replaced by a substitute set.
1) Modify the Thesarus File to include all words with the same meaning. 2) Reload the Thesaurus file using the sys.sp_fulltext_load_thesaurus_file system stored procedure. 3) Run a full text search using either FREETEXT or CONTAINS using FORMSOF.
Design Tables
SPARSE Columns Useful to optimize the storage of NULL values, at the cost of more overhead to retrieve non-null values. o The columns must be nullable and cannot have either the IDENTITY property or FILESTREAM attribute. o Syntax for using the SPARSE Keyword: ColumnName DataType SPARSE NULL Char and Varchar Both allow up to 8,000 characters. o Char is fixed length, whereas Varchar can vary. MAX Specifier Allows storage of values up to 2 GB. o Previously text and ntext were used for lengths of this magnitude, however text should be avoided as it cannot be indexed. o Data Types using the max specifier Varchar(max) Nvarchar(max) Varbinary(max) Numeric Data Types o Bigint Range 9,223,372,036,854,775,808, Storage = 8 Bytes o Int Range +/- 2 billion, Storage = 4 Bytes o Smallint Range +/- 37.2 K, Storage = 2 Bytes o Tinyint Range 0 255, Storage = 1 Byte Decimal and Numeric data types Numeric is functionally equivalent to Decimal. o Syntax: decimal[ (p[ , s] )] and numeric[ (p[ , s] )] o (P) Precision, the maximum number of digits that can be stored on either side of the decimal point. Max Precision is 38 and consumes 17 Bytes o (S) Scale, the maximum number of digits that can be stored on the right side of the decimal point.
Sp_tableoption Sets option values for tables containing large user-defined type columns, such as varbinary(max), nvarchar(max), varchar(max), image, text, etc. o Large Value Types Out of Row option when turned on, large value data types will be stored outside of the row, but still in the database. FILESTREAM attribute for column values to be stored in the file system rather than the database. Non-deterministic Views and Computed Columns Cannot be indexed, and PERSISTED cannot be used. PERSISTED Keyword with Computed Columns the value is recalculated every time a column referenced in the calculation changes. Sp_tableoption Sets optional values for user defined tables Syntax Sp_tableoption [@TableNamePattern = ] table , [@OptionName = ] option_name , [ @OptionValue = ] value
Value
table lock on bulk load
Description
When disabled (the default), it causes the bulk load process on user-defined tables to obtain row locks. When enabled, it causes the bulk load processes on user-defined tables to obtain a bulk update lock.
Not supported in SQL Server 2008. The locking strategy of SQL Server is row locking with possible promotion to page or table locking. This option has no effect on the locking behavior of SQL Server and is included only for compatibility of existing scripts and procedures.
text in row
When OFF or 0 (disabled, the default), it does not change current behavior, and there is no BLOB in row. When specified and @OptionValue is ON (enabled) or an integer value from 24 through 7000, new text, ntext, or image strings are stored directly in the data row. All existing BLOB (binary large object: text, ntext, or image data) will be changed to text in row format when the BLOB value is updated. For more information, see Remarks.
large value
types out of row columns in the table are stored out of row, with a 16-byte pointer to the root. 0 = varchar(max), nvarchar(max), varbinary(max), xml and large UDT values are stored
directly in the data row, up to a limit of 8000 bytes and as long as the value can fit in the record. If the value does not fit in the record, a pointer is stored in-row and the rest is stored out of row in the LOB storage space. 0 is the default value.
When TRUE, ON, or 1, the designated table is enabled for vardecimal storage format. When FALSE, OFF, or 0, the table is not enabled for vardecimal storage format. Vardecimal storage format can be enabled only when the database has been enabled for vardecimal storage format by using sp_db_vardecimal_storage_format. For information about vardecimal storage format, see Storing Decimal Data As Variable Length. This option requires SQL Server 2005 Service Pack 2. Vardecimal storage format is available only in SQL Server Enterprise, Developer, and Evaluation editions. In SQL Server 2008 and later, all user databases are enabled for the vardecimal storage format. In SQL Server 2008 and later, vardecimal storage format is deprecated. Use ROW compression instead. For more information, see Creating Compressed Tables and Indexes. 0 is the default value.
Design Views
CREATE VIEW [schema name].ViewName [ (Column1 [,.n)] [WITH <View_Attribute> [,.n] AS SELECT Statement [WITH CHECK OPTION] Partitioned Views Implemented using the UNION ALL operator. o Query Optimizer can use CHECK constraints on the individual tables to optimize SELECT statements on the partitioned view. o The preferred method for using partitioned data local to one server is with partitioned tables. However, Partitioned Views can access data spread across multiple Federated Database Servers. View Attributes o ENCRYPTION Used to encrypt the CREATE VIEW statement that is stored in the sys.syscomments table. o SCHEMABINDING Prevents the base table(s) from being modified in a way that would affect the view definition. The SELECT statement must use the two part name when this option is specified (e.g. Schema.Object) o VIEW_METADATA Various APIs will be able to access metadata about the view instead of the base table or views, when browse mode metadata is being requested for a query that references the view.
DECLARE @IntVariable int, @SQLString nvarchar(500) DECLARE @ParmDefinition nvarchar(500) SET @SQLString = NSELECT Col1, Col2, n FROM Dbname.SchemaName.Table WHERE Col1 = @Var SET @ParmDefinition = N@Var int SET @IntVariable = 123 EXECUTE sp_executesql @SQLString, @ParmDefinition, @Var = @IntVariable
SQL Injection Attack Malicious code is inserted into strings that are later passed to an instance of SQL Server for processing and execution. o How: Prematurely terminates one text string & Appending another, then terminating with a comment mark - - o String concatenation is the primary point of entry for script injection. Ownership Chain Created when a database object, such as a stored procedure, accesses another database object, such as an underlying table. o For example, if an underlying table is owned by the creator of a stored procedure, someone with permission to execute the stored procedure does not require permission to access the underlying table in order see the result of the Stored Procedure. o Ownership chaining does not occur when the Stored Procedure contains DDL. EXECUTE AS Clause Specifies the security context under which a stored procedure should execute. o SELF Eauilivant to the EXECUTE AS User_Name where User_Name is the person creating or modifying the stored procedure. o OWNER The owner of the module, if the module does not have a specified owner, the owner of the schema model is used. o CALLER (Default Option) The user must have access not only to the module itself, but also any underlying modules that are accessed. o User_Name The statements in the module execute under the context of the specified user. Must be a singleton account (e.g. cannot be a group, role, certificate key, or built in account such as NTAUTHORITY\Local System Cannot be specified for DDL triggers with server scope or logon triggers. Use login_name instead. Application Roles Used to authenticate applications, and can be setup with either authentication mode. These are database-level connections. To access another database, the Guest profile would have to be enabled.
Sp_setuprole Command to establish an application role connection, requires a password. Data Reader (ADO.NET) broad category of objects used to sequentially read data from a data source. o There is no DataReader class in ADO.NET but, there are a number of classes that implement the IDataReader interface. o Classes employing IDataReader (Selection) System.Data.SqlClient.SqlDataReader System.OLEDB.OLEDBDataReader Oracle.OracleClient.OracleDataReader o
Function Properties and Limitations o Functions can contain multiple or no parameters. o Cannot use IFELSE construct with the RETURN clause It causes an error, that states that the last statement of a function must be a RETURN clause, even when the last statement is a RETURN clause Inline Table-Valued Functions Returns a result set as a table data type, like the multistatement table-valued function. o Like creating a parameterized view, with parameters specified in the select statement. o Does not have to specify the structure of the table being returned.
CLR Table Valued Function Syntax CLR Table Valued Function Syntax Example CREATE FUNCTION Example (@parm data_type ) RETURNS TABLE (col1 datatype, n) AS EXTERNAL NAME mydotnetassembly.mynamespace.myclassname CLR Function results are Streaming When a CLR Table valued function is used the results are streamed from the CLR table, therefore, using the ORDER clause in the Function definition rather than in SPs consuming the results is more efficient.
CLR User Defined Types Especially beneficial for situations where the data type is employed for a global variable with a definition limiting the domain. o The CREATE ASSEMBLY and CREATE TYPE statements are used to create a CLR UDT. o Specific and limited to the database where it is created in. The UDT must be recreated in every database in which it is employed.
I/O and Isolation The isolation level only determines the level of protection for read operations. Transaction Isolation Levels do not affect the locks acquired to protect data modifications. A Transaction always gets an exclusive lock on any data it modifies. Read Phenomenon ANSI/ISO standards for the three types of Concurrency Side Effects. o Dirty Reads Transaction can read data from another running transaction that has not committed the data. o Non-Repeatable Reads The data read by one transaction could be altered by a second transaction before the first transaction has committed. o Phantom Reads Two identical queries executing at the same time return different results. This could occur if, for example, another transaction commits [inserts] (an) additional row(s) into a table being referenced. Isolation Levels Described Ordered from the least to most restrictive. o Read Uncommitted Ensures that physically corrupt data is not read. Dirty Reads Non-Repeatable Phantom o Read Committed The Default Database Engine Level Non-Repeatable Phantom o Repeatable Read Ensures that no other transactions can update or delete rows in the results set of each SELECT statement in the current transaction until the current transaction has been committed or rolled-back. Phantom o Serializable The highest level, transactions are completely isolated from one another. Lock_Escalation Process of converting many fine-grain locks into fewer coarse-grain locks. o Reduces overhead at the expense of increasing the likelihood of concurrency contention. o The Lock_Escalation settings for a table can be determined from querying sys.tables o Lock Escalation Settings: Table The default option Auto Lock escalation is performed to the table level for non-partitioned tables, and to the partitioned level for partitioned tables. Disable Lock escalation is generally disabled and only occurs in specific situations where it is absolutely necessary to endure data integrity. Rules for Efficient Transactions o Transactions should be as short as possible Do not require input from users during transactions o Reduce versioning when appropriate with row versioning-based isolation level for readonly queries. Deadlocking Two or more tasks block each other by each having a lock on a resource which the other tasks are trying to lock o The system resolves these situations by choosing one task to be the Victim, and killing it. o
Methods of Recovery with Marked Transactions o STOPATMARK = mark_name Rolls the recovery forward to the mark and includes the marked transaction. o STOPBEFOREMARK = mark_name Does not include the marked transaction.
Example of Restoration to a marked transaction USE AdventureWorks2008R2; GO BEGIN TRANSACTION ListPriceUpdate WITH MARK 'UPDATE Product list prices'; GO UPDATE Production.Product SET ListPrice = ListPrice * 1.10 WHERE ProductNumber LIKE 'BK-%'; GO COMMIT TRANSACTION ListPriceUpdate; GO -- Time passes. Regular database and log backups are taken.
-- An error occurs in the database. USE master GO RESTORE DATABASE AdventureWorks2008R2 FROM AdventureWorksBackups WITH FILE = 3, NORECOVERY; GO RESTORE LOG AdventureWorks2008R2 FROM AdventureWorksBackups WITH FILE = 4, RECOVERY, STOPATMARK = 'ListPriceUpdate'; Run-Time Errors and Transactions o A Run-Time Error occurs when the codes syntax is correct, but the operation is still not able to complete. Ex1: Program cannot open a corrupt file Ex2: Statement violates a Check Constraint. XACT_ABORT Option Controls how SQL Server handles a transaction when a run-time error occurs. o When OFF, only the T-SQL statement that raised the error is rolled back o Default value is OFF.
Allows Dirty Reads TABLOCKX Exclusive lock is taken on the table. o Only Locks during a query XLOCK Exclusive locks are taken on the appropriate granularity level when specified with ROWLOCK, TABLOCK, or PAGLOCK. o The lock is held until the transaction completes. READPAST Skips rows that are locked by other transactions rather than waiting for the lock to be released. UPDLOCK Update locks are to be taken and held until the transaction completes o Locks at row or page level only.
sp_create_plan_guide [ @name = ] N'plan_guide_name' , [ @stmt = ] N'statement_text' , [ @type = ] N'{ OBJECT | SQL | TEMPLATE }' , [ @module_or_batch = ] { N'[ schema_name. ] object_name' | N'batch_text' | NULL } , [ @params = ] { N'@parameter_name data_type [ ,...n ]' | NULL } , [ @hints = ] { N'OPTION ( query_hint [ ,...n ] )' | N'XML_showplan' | NULL }
Types of Plan Guides: Object Stored Procedures, scalar UDFs, Multi-Statement table-valued UDFs, DML triggers. SQL Stand-alone T-SQL, batches that arent part of an object. Template Applies to any query that parameterized to the form indicated in statement_text. If TEMPLATE is specified, only the PARAMETERAZATION {FORCED | SIMPLE} query hint can be specified in the @hints parameter. Sp_Control_Plan_Guide Used to control Plan Guides in the following ways. o Drop or disable a single plan guide. o Drop or disable all plan guides in the current database Sp_Get_Query_Template Used to create a template plan guide by returning the parameterized version of a query. o Syntax for creating a Template Plan Guide EXEC Sp_Create_Plan_Guide NMyTemplatePlanGuide, @stmt, NTemplate, NULL, @Params, NOPTION(PARAMETERIZATION FORCED), o PARAMETERIZATION Reusing execution plans for syntactically similar queries with different parameters. o Databases can be set to either simple or forced parameterization. Simple Mode (Default option): The relational engine determines which queries can be parameterized. Forced Mode: Any literal value in a DML statement is converted to a parameter during query compilation with some exceptions (EX: NO ANSI PADDING, RECOMPILE Query hint, etc.)
REBUILD PARTITION = 1 WITH (DATA_COMPRESSION=ROW); Row Compression Uses variable length storage for numeric and fixed length text fields to save space. o Columns with variable length fields are not affected (i.e. Varchar and Varbinary) Page Level Compression o Compressing the leaf level of tables and indexes with page compression consists of three operations in the following order:
1. Row compression 2. Prefix compression A value is identified for each column that is repeated in the data. The value is stored in the Compression Information structure (CI) and replaced from the data with a reference to the CI. 3. Dictionary compression Dictionary Compression searches for repeated data anywhere on the page (not just in the specific column), stores the data in CI and replaces the occurrences with a reference to CI. Sp_estimate_data_compression_savings measures the space savings from using compression. Syntax: sp_estimate_data_compression_savings [ @schema_name = ] 'schema_name' , [ @object_name = ] 'object_name' , [@index_id = ] index_id , [@partition_number = ] partition_number , [@data_compression = ] 'data_compression' [;]
Direct Attached Storage (DAS) A digital Storage System directly attached to a server or workstation without a storage network in between. o The primary characteristic is that it is directly connected through a Host Bus Adapter (HBA) rather than via a network device such as a hub, switch, or router. o DAS is a retronym devised after the advent of storage sharing. Storage Sharing OSs maintain their own file systems on their own dedicated (nonshared) LUNs. o LUN (Logical Unit Number) A number used to identify a logical unit. Logical Unit A device addressed by the SCISI protocol or similar protocols such as Fibre Channel or iSCISI. Common Example: A disk array has multiple physical SCISI ports, each one with a SCISI target address. The array is formatted as a RAID with multiple partitions. The SCISI target is configured to provide multiple LUs (one for each partition). A computer accesses a volume with the LUN assigned to it. Types of Shared Storage: o Network Attached Storage (NAS) A computer connected to a network which only provides file based data storage services to other devices on the network.
Designed as an easy and self-contained solution for sharing files over a network. Contrast with a SAN which only provides block-based storage, leaving file-system concerns to the client side. o Storage Area Network (SAN) Dedicated network that provides access to consolidated block level data storage. Block Devices Support s random access and generally use buffered I/O routines. The OS allocates a single data buffer to hold a single block for each input and output. y I/O requests initiate the storage of data in the appropriate buffer. When all information is received or the buffer fills up, the appropriate action is performed (e.g. writing the data to disk) SAN Benefits: y Servers can boot directly from the SAN. y Allows the quick and easy replacement of faulty servers by reconfiguring the SAN to give the LUN of the faulty device to the replacement server. y Can enable a more effective Disaster Recovery process. A SAN can span great distances which could contain a secondary storage array.
Federated Database Servers Share the database processing load across a group of servers by horizontally partitioning the data in a database. o Servers are managed independently, but cooperate to process the database requests from applications. Distributed Partitioned Views A partitioned view where the partitions are stored on multiple servers. o Linked Server definitions must be added. o Optimize Performance by setting the Lazy Schema Validation option by using the sp_serveroption for each linked server definition that employs a Distributed Partitioned View. This prevents the query processor from requesting meta-data for any of the linked tables until the data is needed. Database Mirroring Primary benefits are that it is easier to administer than log shipping, and it has failover capability. o The mirroring schema consists of one Principal (production) server, and one Minor (mirror) server. All actions performed on the Principal are replicated on the minor. The minor re-does every insert, delete, etc. All modes only support one minor and one principal. o Operating Modes Asynchronous Transactions commit on the Principal before the mirror. Synchronous increases transaction latency. High Safety (Automatic Failover) Requires a third server known as a Witness, which supports automatic failover verifying whether the principal server is up and functioning.
2. Create Partition Schema based on the partition function. The filegroups can all reside on the same disk set: Whats important is to maximize I/O performance by using as many underlying disk spindles as possible. a. The partition schema maps particular partitions to filegroups. i. Using multiple filegroups is necessary to take advantage if filegroup backups or piecemeal restore are needed. ii. With multiple filegroups, multiple LUNs can be mapped on a SAN. b. ALL partition scheme is mapped to a single dedicated file group. Syntax: CREATE PARTITION SCHEME partition_scheme_name AS PARTITION partition_function_name [ ALL ] TO ( { file_group_name | [ PRIMARY ] } [ ,...n ] ) [;]
3. Create the table using the partition schema. a. The Partition Column must be a part of: i. The clustered index key ii. The primary key. iii. Unique index and uniqueness constraint keys.
Altering a Partition Function partition functions are not static, and changes to them will propagate through the partition scheme to the partitioned table. o $PARTITION Function used to determine which partition a particular value would be placed in. SELECT $PARTITION.partition_function_name (test value) y Could be used to measure the distribution of values within partitions if, for example, a column name was used instead of a static value.
o SWITCH operation o Three ways to use the SWITCH operation: Switch from a partition of a partitioned table to an empty non partitioned staging table. Switch from a staging table to an empty partition of the partitioned table. Switch a partition from one partitioned table TO an empty partition in another partitioned table. o Requirements for Indexes during a SWITCH operation All indexes must be aligned. No foreign keys can reference the partitioned table. o Syntax: ALTER TABLE table_name SWITCH PARTITION1 TO tbl_2_name PARTITION4 y Will fail if PARTITION4 contained data.
information should be displayed. Syntax: GROUP BY GROUPING SETS (groupingset1[.,groupingsetn]) Adding () into the grouping sets also displays a grand total grouping. o GROUPING_ID Function that computes the level of grouping. GROUPING_ID can only be used in the SELECT list, HAVING, or ORDER BY clauses when GROUP BY is specified GROUPING_ID increases as the level of aggregation increases
SELECT D.Name ,E.Title ,GROUPING_ID(D.Name, E.Title) AS 'Grouping Level' ,COUNT(E.EmployeeID) AS N'Employee Count' FROM HumanResources.Employee E INNER JOIN HumanResources.EmployeeDepartmentHistory DH ON E.EmployeeID = DH.EmployeeID INNER JOIN HumanResources.Department D ON D.DepartmentID = DH.DepartmentID WHERE DH.EndDate IS NULL AND D.DepartmentID IN (12,14) GROUP BY ROLLUP(D.Name, E.Title) --HAVING GROUPING_ID(D.Name, E.Title) = 0; --All titles
APPLY Used to join the results of a table valued function by an outer table expression of a query. o OUTER APPLY Returns the results that produce a result, as well as those that dont o CROSS APPLY - Returns rows from the outer table only when they produce a result set from the table-valued function. Syntax Example: APPLY SELECT D.deptid, D.deptname, D.deptmgrid ,ST.empid, ST.empname, ST.mgrid FROM Departments AS D CROSS APPLY fn_getsubtree(D.deptmgrid) AS ST; Table Hints o INDEX ( 0 ) Forces a clustered index scan if one is present, otherwise a full table scan is completed. o INDEX( index_value [,n] ) Specifies the indexes that will be used by the query optimizer, by using the name or ID of the indexes. o FORCE ORDER The query optimizer does not affect the join order specified in the query. o FORCESEEK Specifies that the query optimizer use only an index seek operation as the access path to the data in the table or view.
FORCESEEK [ (index_value(index_column_name [ ,... n ] )) ]
Hash Joins have two inputs the Build Input and the Probe Input. The Build input is the smaller of the two inputs. Three types of Hash Joins: In-Memory Hash Join The hash table is built in memory. After a table scan of the build input the results are put into hash buckets. Then the probe table is scanned one row at a time. The rows hash key is created and the appropriate bucket searched. Grace Hash Join The build and probe are partitioned into smaller segments based on the hash function, and processed in batches. o Occurs when the input does not fit into memory. o The Hash join is applied to each pair of partitioned files after the partitioning. Recursive Hash Join Employs large asynchronous I/O operations and additional partitioning steps for the larger partitions. o Occurs when the input is so large that it would require multiple partitioning steps, merge levels, and partitioning levels. Useful for intermediate results queries because: Intermediate results arent generally indexed or sorted for the next join operation. Hash Joins reduce the need for de-normalization because Logical and Physical Operators o Tempdb stores temporary objects created during SQL server operations. Hash Joins, Hash Aggregates, and Sort operations all create intermediate results stored in tempdb.
2. Execute T-SQL to populate cursor. 3. Retrieve Rows This operation which can retrieve one or multiple rows is called a FETCH. a. A series of Fetches to retrieve rows in either a forward or backward direction is called scrolling. 4. [Optional] Perform modification operations on the row at the current position in the cursor. 5. Close the cursor.
Transact-SQL Extended Cursor Syntax: DECLARE cursor_name CURSOR [ LOCAL | GLOBAL ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] [ READ_ONLY | SCROLL_LOCKS | OPTIMISTIC ] [ TYPE_WARNING ] FOR select_statement [ FOR UPDATE [ OF column_name [ ,...n ] ] ] [;]
FOR UPDATE Identifies that all or some of the columns can be updated unless the READ_ONLY option is specified. When column_name is supplied only the listed columns can be updated. o Cursor Types The four cursor types are available either through the APIs or via TSQL. Static Detect few or no changes and consume few resources when scrolling. Keyset-Driven Detect some changes but less than dynamic. Dynamic Detect all changes, but consume the most amounts of resources. Forward-Only The APIs see this as a type, T-SQL sees as an option.
SELECT ID AS @ID, CompanyName AS @Name, Type AS Detail/Type, Rating AS Detail/Rating FROM Prospect FOR XML PATH(Prospect), Root (ProspectData); TYPE directive used when the output of a query using FOR XML requires the output returned in the xml data type. SELECT BusinessEntityID, FirstName, LastName FROM Person.Person ORDER BY BusinessEntityID FOR XML AUTO, TYPE;
o Best way to move data from an xml data type column to a relational table, because it requires minimal effort and memory use. o Simple Example: Create a Stored Procedure using nodes to retrieve multiple lines for each Purchase Order line. The value() method is used with the table alias to return the details in SQL server data types. CREATE PROCEDURE usp_name (@XML, xml) AS BEGIN SELECT t.c.value(@var1, int), t.c.value(@var2,varchar(50)) FROM @XML.nodes(/POlines/POline AS t(c) END y t. Is optional y Original Format: <POlines> <POline var1=### var2 = ### . /> <POline var1=### var2 = ### . /> .. </POlines> o Apply Nodes to a Column requires the use of the CROSS APPLY clause. Sample XML from AdventureWorks Db
insert into #temp values('<root> Adventure Works CyclesFR-210B Instructions for Manufacturing HL Touring Frame. Summary: This document contains manufacturing instructions for manufacturing the HL Touring Frame, Product Model 7. Instructions are work center specific and are identified by Work Center ID. These instructions must be followed in the order presented. Deviation from the instructions is not permitted unless an authorized Change Order detailing the deviation is provided by the Engineering Manager.<Location LaborHours="2.5" LotSize="100" MachineHours="3" SetupHours="0.5" LocationID="10"> Work Center 10 - Frame Forming. The following instructions pertain to Work Center 10. (Setup hours = .5, Labor Hours = 2.5, Machine Hours = 3, Lot Sizing = 100)<step> Insert <material>aluminum sheet MS-2341</material> into the <tool>T-85A framing tool</tool>. </step><step> Attach <tool>Trim Jig TJ-26</tool> to the upper and lower right corners of the aluminum sheet. </step><step> Using a <tool>router with a carbide tip 15</tool>, route the aluminum sheet following the jig carefully. </step><step> Insert the frame into <tool>Forming Tool FT-15</tool>
and press Start. </step><step> When finished, inspect the forms for defects per Inspection Specification <specs>INFS-111</specs>. </step><step>Remove the frames from the tool and place them in the Completed or Rejected bin as appropriate.</step></Location><Location LaborHours="1.75" LotSize="1" MachineHours="2" SetupHours="0.15" LocationID="20"> Work Center 20 - Frame Welding. The following instructions pertain to Work Center 20. (Setup hours = .15, Labor Hours = 1.75, Machine Hours = 2, Lot Sizing = 1)<step> Assemble all frame components following blueprint <blueprint>1299</blueprint>. </step><step> Weld all frame components together as shown in illustration <diag>3</diag></step><step> Inspect all weld joints per Adventure Works Cycles Inspection Specification <specs>INFS-208</specs>. </step></Location><Location LaborHours="1" LotSize="1" LocationID="30"> Work Center 30 - Debur and Polish. The following instructions pertain to Work Center 30. (Setup hours = 0, Labor Hours = 1, Machine Hours = 0, Lot Sizing = 1)<step> Using the <tool>standard debur tool</tool>, remove all excess material from weld areas. </step><step> Using <material>Acme Polish Cream</material>, polish all weld areas. </step></Location><Location LaborHours="0.5" LotSize="20" MachineHours="0.65" LocationID="45"> Work Center 45 - Specialized Paint. The following instructions pertain to Work Center 45. (Setup hours = 0, Labor Hours = .5, Machine Hours = .65, Lot Sizing = 20)<step> Attach <material>a maximum of 20 frames</material> to <tool>paint harness</tool> ensuring frames are not touching. </step><step> Mix <material>primer PA-529S</material>. Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-501</specs>. </step><step>Apply thin coat of primer to all surfaces. </step><step>After 30 minutes, touch test for dryness. If dry to touch, lightly sand all surfaces. Remove all surface debris with compressed air. </step><step> Mix <material>paint</material> per manufacturer instructions. </step><step> Test spray pattern on sample area and correct flow and pattern as required per engineering spec <specs>AWC-509</specs>. </step><step>Apply thin coat of paint to all surfaces. </step><step>After 60 minutes, touch test for dryness. If dry to touch, reapply second coat. </step><step> Allow paint to cure for 24 hours and inspect per <specs>AWC-5015</specs>. </step></Location><Location LaborHours="3" LotSize="1" SetupHours="0.25" LocationID="50"> Work Center 50 - SubAssembly. The following instructions pertain to Work Center 50. (Setup hours = .25, Labor Hours = 3, Machine Hours
= 0, Lot Sizing = 1)<step>Add Seat Assembly. </step><step>Add Brake assembly. </step><step>Add Wheel Assembly. </step><step>Inspect Front Derailleur. </step><step>Inspect Rear Derailleur. </step></Location><Location LaborHours="4" LotSize="1" LocationID="60"> Work Center 60 - Final Assembly. The following instructions pertain to Work Center 60. (Setup hours = 0, Labor Hours = 4, Machine Hours = 0, Lot Sizing = 1)<step> Perform final inspection per engineering specification <specs>AWC-915</specs>. </step><step>Complete all required certification forms.</step><step>Move to shipping.</step></Location></root>');
T(c)