Skip to content

Instantly share code, notes, and snippets.

@LitKnd
Created January 23, 2018 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LitKnd/0af4c569ae12b4b66d1994cfa7995af0 to your computer and use it in GitHub Desktop.
Save LitKnd/0af4c569ae12b4b66d1994cfa7995af0 to your computer and use it in GitHub Desktop.
/*****************************************************************************
Copyright (c) 2017 SQL Workbooks LLC
MIT License, http://www.opensource.org/licenses/mit-license.php
Contact: help@sqlworkbooks.com
Setup:
Get WideWorldImporters-Full.bak from Microsoft at:
https://github.com/Microsoft/sql-server-samples/releases/tag/wide-world-importers-v1.0
******************************************************************************/
USE master;
GO
IF DB_ID('WideWorldImporters') IS NOT NULL
BEGIN
ALTER DATABASE WideWorldImporters SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
END
GO
RESTORE DATABASE WideWorldImporters FROM DISK=
'S:\MSSQL\Backup\WideWorldImporters-Full.bak'
WITH REPLACE,
MOVE 'WWI_Primary' to 'S:\MSSQL\Data\WideWorldImporters.mdf',
MOVE 'WWI_UserData' to 'S:\MSSQL\Data\WideWorldImporters_UserData.ndf',
MOVE 'WWI_Log' to 'S:\MSSQL\Data\WideWorldImporters.ldf',
MOVE 'WWI_InMemory_Data_1' to 'S:\MSSQL\Data\WideWorldImporters_InMemory_Data_1';
GO
USE WideWorldImporters;
GO
EXEC sp_helpstats 'Warehouse.StockItemTransactions', 'ALL'
GO
DBCC SHOW_STATISTICS ('Warehouse.StockItemTransactions', 'FK_Warehouse_StockItemTransactions_CustomerID');
GO
SELECT MAX(InvoiceID)
FROM Warehouse.StockItemTransactions as sit
WHERE sit.CustomerID = 35
GO
DECLARE @CustomerID INT = 35
SELECT MAX(InvoiceID)
FROM Warehouse.StockItemTransactions as sit
WHERE sit.CustomerID = @CustomerID
GO
CREATE OR ALTER PROCEDURE #temp
@CustomerID INT
AS
SELECT MAX(InvoiceID)
FROM Warehouse.StockItemTransactions as sit
WHERE sit.CustomerID = @CustomerID
GO
EXEC #temp @CustomerID = 35;
GO
CREATE OR ALTER PROCEDURE #temp
@CustomerID INT
AS
DECLARE @CustomerID2 INT = ISNULL(@CustomerID, 1)
SELECT MAX(InvoiceID)
FROM Warehouse.StockItemTransactions as sit
WHERE sit.CustomerID = @CustomerID2
GO
EXEC #temp @CustomerID = 35;
GO
CREATE OR ALTER PROCEDURE #temp
@CustomerID INT
AS
SELECT MAX(InvoiceID)
FROM Warehouse.StockItemTransactions as sit
WHERE sit.CustomerID = @CustomerID
OPTION (OPTIMIZE FOR UNKNOWN)
GO
EXEC #temp @CustomerID = 35;
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment