diff --git a/Cursores/Cursor_English.sql b/Cursores/Cursor_English.sql new file mode 100644 index 0000000..5fb9b7c --- /dev/null +++ b/Cursores/Cursor_English.sql @@ -0,0 +1,89 @@ +USE AdventureWorks2019; +GO + +-- Declare the variables to be used +DECLARE @c TINYINT = 1, @a TINYINT = 1 + , @businessEntityID INT + , @creditCardID INT + , @modifiedDate DATETIME + , @cardType VARCHAR(100) + , @cardNumber VARCHAR(25) + , @expMonth TINYINT + , @expYear SMALLINT + , @modifiedDate2 DATETIME; + +-- Declare the cursor for PersonCreditCard +DECLARE cPersonCreditCard CURSOR FOR +SELECT TOP 20 BusinessEntityID, CreditCardID, ModifiedDate +FROM Sales.PersonCreditCard; -- Ensure this table exists + +-- Open the PersonCreditCard cursor +OPEN cPersonCreditCard; + +-- Fetch the first row from the cursor +FETCH NEXT FROM cPersonCreditCard +INTO @businessEntityID, @creditCardID, @modifiedDate; + +-- Iterate through the rows of the PersonCreditCard cursor +WHILE @@FETCH_STATUS = 0 +BEGIN + -- Print the values for the current row + PRINT '< Cursor cPersonCreditCard: ' + CAST(@c AS VARCHAR); + PRINT '@businessEntityID: ' + CAST(@businessEntityID AS VARCHAR); + PRINT '@creditCardID: ' + CAST(@creditCardID AS VARCHAR); + PRINT '@modifiedDate: ' + CAST(@modifiedDate AS VARCHAR); + + SET @c = @c + 1; + SET @a = 1; + + -- Declare a nested cursor for CreditCard + DECLARE cCreditCard CURSOR FOR + SELECT CardType, CardNumber, ExpMonth, ExpYear, ModifiedDate + FROM Sales.CreditCard + WHERE CreditCardID = @creditCardID; -- Ensure this table exists + + -- Open the CreditCard cursor + OPEN cCreditCard; + + -- Fetch the first row from the CreditCard cursor + FETCH NEXT FROM cCreditCard + INTO @cardType, @cardNumber, @expMonth, @expYear, @modifiedDate2; + + -- Iterate through the rows of the CreditCard cursor + WHILE @@FETCH_STATUS = 0 + BEGIN + -- Print the values for the current row of the CreditCard cursor + PRINT ' <<< Cursor cCreditCard: ' + CAST(@a AS VARCHAR); + PRINT ' @cardType: ' + CAST(@cardType AS VARCHAR); + PRINT ' @cardNumber: ' + CAST(@cardNumber AS VARCHAR); + PRINT ' @expMonth: ' + CAST(@expMonth AS VARCHAR); + PRINT ' @expYear: ' + CAST(@expYear AS VARCHAR); + PRINT ' @modifiedDate2: ' + CAST(@modifiedDate2 AS VARCHAR); + PRINT ' >>>>>'; + + SET @a = @a + 1; + -- Fetch the next row from the CreditCard cursor + FETCH NEXT FROM cCreditCard + INTO @cardType, @cardNumber, @expMonth, @expYear, @modifiedDate2; + END -- End of the CreditCard cursor iteration + + CLOSE cCreditCard; -- Close the CreditCard cursor + DEALLOCATE cCreditCard; -- Deallocate the CreditCard cursor + + PRINT '>>>>>>>>'; + + -- Fetch the next row from the PersonCreditCard cursor + FETCH NEXT FROM cPersonCreditCard + INTO @businessEntityID, @creditCardID, @modifiedDate; +END -- End of the PersonCreditCard cursor iteration + +CLOSE cPersonCreditCard; -- Close the PersonCreditCard cursor +DEALLOCATE cPersonCreditCard; -- Deallocate the PersonCreditCard cursor + +Key Points: + + Cursor Declaration: The DECLARE cPersonCreditCard CURSOR FOR section defines the cursor to iterate through rows from the Sales.PersonCreditCard table. + Cursor Operations: The OPEN, FETCH NEXT, CLOSE, and DEALLOCATE commands manage the lifecycle of the cursor, fetching and processing rows as needed. + Nested Cursor: The nested cursor cCreditCard is used to fetch related card details based on CreditCardID. + +Make sure to validate that Sales.PersonCreditCard and Sales.CreditCard exist in your AdventureWorks 2019 database and that the column names match those used in the script. diff --git a/Indexacion/Indexation.sql b/Indexacion/Indexation.sql new file mode 100644 index 0000000..8677221 --- /dev/null +++ b/Indexacion/Indexation.sql @@ -0,0 +1,71 @@ +-- Use the AdventureWorks2019 database +USE AdventureWorks2019; +GO + +-- ================================================ +-- Indexing Example +-- ================================================ + +-- Create a non-clustered index on the 'Name' column of the 'Product' table +-- This will help improve performance for queries that filter or sort by 'Name' +CREATE NONCLUSTERED INDEX IX_Product_Name +ON Production.Product (Name); +GO + +-- Verify that the index has been created +-- This query retrieves information about the 'IX_Product_Name' index +SELECT * +FROM sys.indexes +WHERE object_id = OBJECT_ID('Production.Product') +AND name = 'IX_Product_Name'; +GO + +-- View index usage statistics +-- This query shows how often the index is used by looking at statistics +SELECT * +FROM sys.dm_db_index_usage_stats +WHERE object_id = OBJECT_ID('Production.Product'); +GO + +-- ================================================ +-- Blocking Example +-- ================================================ + +-- Display current blocking information +-- This query shows details about sessions that are blocking other sessions +SELECT + blocking_session_id AS BlockingSessionID, -- ID of the session causing the block + session_id AS BlockedSessionID, -- ID of the session being blocked + wait_type, -- Type of wait for the blocked session + wait_time, -- Time the blocked session has been waiting + wait_resource -- Resource causing the wait +FROM sys.dm_exec_requests +WHERE blocking_session_id <> 0; +GO + +-- Investigate details of blocking transactions +-- This query provides more information about sessions and transactions involved in blocking +SELECT + r.session_id, + r.blocking_session_id, + r.wait_type, + r.wait_time, + r.wait_resource, + s.login_name, + s.host_name, + t.transaction_state +FROM sys.dm_exec_requests r +JOIN sys.dm_exec_sessions s ON r.session_id = s.session_id +JOIN sys.dm_tran_session_transactions t ON r.session_id = t.session_id +WHERE r.blocking_session_id <> 0; +GO + +-- ================================================ +-- Terminate a blocking session (Use with caution!) +-- ================================================ + +-- To forcefully terminate a blocking session, replace with the actual session ID +-- This should be done carefully as it will terminate the session and rollback any uncommitted transactions +-- Example: KILL 55; +KILL ; +GO diff --git a/README.md b/README.md index 9890fb7..220b913 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,7 @@ - [Guia de estudio](#guia-de-estudio) - [Herramientas](#herramientas) + # Guia de estudio Publicaciones: @@ -53,6 +54,8 @@ Publicaciones: - Visual Studio Code - [Descarga](https://code.visualstudio.com/download) - Extensiones: [SQL Tools](https://marketplace.visualstudio.com/items?itemName=mtxr.sqltools), [Bicep](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-bicep), [Markdown](https://marketplace.visualstudio.com/items?itemName=yzhang.markdown-all-in-one), [Powershell](https://marketplace.visualstudio.com/items?itemName=ms-vscode.PowerShell) - [Teclas rĂ¡pidas](png/keyboard-shortcuts-windows.pdf), [teclas rĂ¡pidas (2)](https://www.sitepoint.com/visual-studio-code-keyboard-shortcuts/) +- SQL Server Basics - [SqlServer](https://www.sqlshack.com/sql-server-training/) + diff --git a/pdf/Introducing-Microsoft-SQL-Server-2019.pdf b/pdf/Introducing-Microsoft-SQL-Server-2019.pdf new file mode 100644 index 0000000..18e4518 Binary files /dev/null and b/pdf/Introducing-Microsoft-SQL-Server-2019.pdf differ