This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ----------------------------------------------------------------- | |
| -- Course: SQL Server: Optimizing Ad Hoc Statement Performance | |
| -- Module: Statement Caching | |
| -- Demo: Analyzing query_hash and query_plan_hash | |
| ----------------------------------------------------------------- | |
| SELECT TOP(10) [type] AS [Memory Clerk Type], | |
| SUM(pages_kb)/1024 AS [Memory Usage (MB)] | |
| FROM sys.dm_os_memory_clerks WITH (NOLOCK) | |
| GROUP BY [type] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ##example to extract substrings with grep | |
| grep -Poe '\[SWIFT BIC CODE\].*?\[|BIC\/SWIFT.*?\[|\(BIC\)\/SWIFT.*?\[|\(BIC\) \/ SWIFT.*?\[' list_full.csv | wc -l | |
| ##Grep everything before a specific character [duplicate] | |
| awk -F":" '{print $1}' < inputfile |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| EXEC sp_addtype complex, 'varchar(50)' | |
| CREATE rule complex_valid | |
| AS | |
| -- check if last character is 'i' | |
| RIGHT(RTRIM(@cx), 1) = 'i' AND | |
| -- check if the real part exists and is numeric | |
| IsNumeric(LEFT(LTRIM(@cx), | |
| PATINDEX('%_[-+]%', LTRIM(@cx)))) = 1 AND |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Listing 12-16: Creating a Temporary Table of Order Totals for a Customer | |
| SELECT IDENTITY(INT, 1, 1) AS Sequence, | |
| O.OrderID, | |
| SUM(OD.Quantity * OD.UnitPrice) AS Total | |
| INTO #Totals | |
| FROM Orders AS O | |
| JOIN [Order Details] AS OD | |
| ON OD.Orderid = o.OrderID | |
| WHERE O.CustomerID = 'SAVEA' | |
| GROUP BY |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| -- Listing 12-23: Removing the Duplicates | |
| DELETE d | |
| FROM Dupes AS D | |
| JOIN #Singles AS S | |
| ON S.ID = D.ID | |
| AND S.Txt = D.Txt | |
| -- Listing 12-24: Inserting the Former Duplicates | |
| INSERT Dupes | |
| SELECT * |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| select date, CurrentMeterSNID, | |
| [1] = case when rownum2 = 1 | |
| then reading | |
| else lead(reading) over(partition by CurrentMeterSNID order by date) | |
| end, | |
| [2] = case when rownum2 = 2 | |
| then reading | |
| else lead(reading) over(partition by CurrentMeterSNID order by date) | |
| end | |
| from INF_Facility_ElectricalRecord |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /************************************************************ | |
| * Code formatted by SoftTree SQL Assistant © v11.0.35 | |
| * Time: 8/30/2020 2:54:20 PM | |
| ************************************************************/ | |
| -- Listing 16-8: Schema Creation Script for the New Employees Table | |
| IF OBJECT_ID('Employees', 'U') IS NOT NULL | |
| DROP TABLE Employees; | |
| GO |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE PROC [dbo].[GetMemberInfoParam] | |
| ( | |
| @member_no INT = NULL, | |
| @Lastname VARCHAR(15) = NULL, | |
| @Firstname VARCHAR(15) = NULL, | |
| @MiddleInitial letter = NULL, | |
| @EmailAddress VARCHAR(128) = NULL, | |
| @Region_no numeric_id = NULL, | |
| @Member_code status_code = NULL | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| IF OBJECT_ID('[dbo].[ProcedurePlanState]') IS NOT NULL | |
| DROP PROCEDURE [dbo].[ProcedurePlanState]; | |
| GO | |
| CREATE PROCEDURE [dbo].[ProcedurePlanState] | |
| (@SchemaName sysname, | |
| @ObjectName sysname) | |
| AS | |
| SELECT | |
| ISNULL(db_name([ps].[database_id]), 'resourcedb') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| DECLARE @CurrDB SYSNAME = 'MyDBName' | |
| DECLARE @CMD NVARCHAR(MAX); | |
| SELECT @CMD = ISNULL(@CMD + N', | |
| ', N'') + N'(NAME = ' + QUOTENAME(name) + N' | |
| , FILENAME = ' + QUOTENAME(LEFT(physical_name, LEN(physical_name) - CHARINDEX('\', REVERSE(physical_name)) + 1) | |
| + DB_NAME(database_id) + '_' + REPLACE(NEWID(),'-','') + '.ss', N'''') | |
| + N')' |