SQL Server - Bài
Stored Procedures &User-Defined FunctionsVu Tuyet Trinhtrinhvt-fit@mail.hut.edu.vnHanoi University of Technology1 MicrosoftMicrosoftIntroduction to SQL Batch ProcessingBatchIndividual SQL commandsGrouped to form a batchCompiled as single execution plan MicrosoftMicrosoftExampleUse PubsSelect * from authorsUpdate authors set phone= '890 451-7366‘where au_lname= 'White'Go MicrosoftMicrosoftOutline√Understanding the concepts of batch and batch processingT-SQL ProgrammingDefine and assign variablesCursors for data retrievalControl statementsWrite SQL statements using SQL Server basic functionsUse basic functions in a queryImplementing Stored ProceduresImplementing User-Defined Functions 4 MicrosoftMicrosoftAssigning values to variablesSET statement SET @local_variable name = value SELECT statement SELECT @local_variable name = valueExampleSET @CUST=’FRANK’SELECT CUSTOMERID,COMPANYNAME FROM CUSTOMERS WHERE CUSTOMERID = @CUST MicrosoftMicrosoftVariablesSQL Server supports two types of variables in T-SQL @@global_variable@local_variable MicrosoftMicrosoftList of Global variables @@CONNECTIONS Number of connections made to the server since it was last started.@@CPU_BUSY Number of milliseconds the system has been processing since SQL Server was started@@CURSOR_ROWS Number of rows in the most recently opened cursor.@@ERROR Error number of the last T-SQL error@@FETCH_STATUS 0 if the last fetch status was successful. -1 if there was an error MicrosoftMicrosoftList of Global variables (2)@@IDENTITY Last inserted identity value@@LANGUAGE Name of the language currently in use.@@MAX_CONNECTIONS Maximum number of concurrent connections that can be made.@@ROWCOUNT Number of rows affected by most recent SQL Statement@@SERVERNAME Name of local server@@SERVICENAME Name of the SQL Service on this computer@@TIMETICKS Number of microseconds per tick on the current computer@@TRANSCOUNT Number of transaction open on the current connection@@VERSION Version information of SQL Server MicrosoftMicrosoftCursors to Retrieve DataAllowing positioning at specific rows of the result set Retrieving one row or block of rows from the current position in the result set Supporting data modifications to the rows at the current position in the result set Supporting different levels of visibility for changes made by other users to the data in the result set Providing access to the data in a result set for T-SQL statements in scripts, stored procedures, and triggers MicrosoftMicrosoftCursor ImplementationsTransact-SQL Server CursorsUsed in scripts, stored procedures, and triggersImplemented on the serverAPI Server CursorsImplemented on the server but managed by API cursor functions (OLE DB, ODBC, DB-Library)Client CursorsEntire result set is cached on client and all cursor operations are performed against this cached set [...]... SUSER_SID([‘login_name’]) SUSER_ID([‘login_name’]) SUSER_SNAME( [server_ user_id]) SUSER_NAME( [server_ user_id]) USER_ID([‘user_name’]) Microsoft USER_NAME([user_id]) Outline √ √ Understanding the concepts of batch and batch processing T -SQL Programming Define and assign variables Cursors for data retrieval Control statements Write SQL statements using SQL Server basic functions Use basic functions in... Remote stored procedures Execute or OpenQuery(linked server name,‘exec stored procedure’) Example Execute dbo.GetSubjects Microsoft Outline √ √ Understanding the concepts of batch and batch processing T -SQL Programming √ Define and assign variables Cursors for data retrieval Control statements Write SQL statements using SQL Server basic functions Use basic...Working with T -SQL Server Cursors Declare the cursor Populate the cursor Retrieve (fetch) the result set Microsoft First, Next, Prior, Last, Absolute n, Relative n Optional: update or delete a row Close the cursor Free resources allocated to the cursor Control Statements Microsoft Control Statements Contd… Microsoft BEGIN END A set of TSQL statements to be executed can... BEGIN { statement | statement_block} END Microsoft IF ELSE We can execute different sets of SQL statements based on specified conditions Syntax: IF Boolean_expression { sql_ statement | statement_block } [ ELSE { sql_ statement | statement_block } ] Microsoft Example Microsoft WHILE construct We can execute a SQL statement or a block of statements based on some condition Syntax: WHILE Boolean_expression... Stored Procedures Implementing User-Defined Functions Microsoft 27 Stored Procedures Main ideas move the processing as close to the data as possible Move into a batch that has been stored with a name so it can be pre-compiled Advantages Microsoft better performance higher productivity ease of use increased scalability Creating a Stored Procedure USE SQLProject; go CREATE PROCEDURE GetSubjects... variables Cursors for data retrieval Control statements Write SQL statements using SQL Server basic functions Use basic functions in a query Implementing Stored Procedures Implementing User-Defined Functions Microsoft 33 User-Defined Function A function returning value Advantages: better performance, higher productivity, ease of use, increased scalability for database application embed complex... logic within a query create new functions for complex expressions Three distinct types Microsoft Scalar functions that return a single value Updateable inline table functions similar to views Multi-statement table functions that build a result set with code Creating a Scalar Function Microsoft Creating a Inline Table Function Microsoft . '890 45 1-7 366 ‘where au_lname= 'White'Go MicrosoftMicrosoftOutline√Understanding the concepts of batch and batch processingT -SQL ProgrammingDefine. a result set for T -SQL statements in scripts, stored procedures, and triggers MicrosoftMicrosoftCursor ImplementationsTransact -SQL Server CursorsUsed