Overview

Stored Procedure Template

IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE SPECIFIC_SCHEMA = N'dbo' AND SPECIFIC_NAME = N'MyProcedureName')
   DROP PROCEDURE [dbo].[MyProcedureName];
GO

CREATE PROCEDURE [dbo].[MyProcedureName]
	@p1 int = 0,
	@p2 int = 0
WITH 
/* Obfuscating the Procedure Definition */
ENCRYPTION,
/* It is helpful when the parameters supplied to the procedure will not be typical, 
and when a new execution plan should not be cached or stored in memory. */
RECOMPILE
AS
BEGIN
	SET NOCOUNT ON;
	SELECT @p1 + @p2;
END

Using OUTPUT parameters

IF OBJECT_ID ( 'Production.uspGetList', 'P' ) IS NOT NULL 
    DROP PROCEDURE Production.uspGetList;
GO

CREATE PROCEDURE Production.uspGetList 
	@Product varchar(40),
	@MaxPrice money,
	@ComparePrice money OUTPUT,
	@ListPrice money OUTPUT
AS
BEGIN
	SET NOCOUNT ON;

	SELECT p.[Name] AS Product, p.ListPrice AS 'List Price'
	FROM Production.Product AS p
	INNER JOIN Production.ProductSubcategory AS s 
		ON p.ProductSubcategoryID = s.ProductSubcategoryID
  WHERE s.[Name] LIKE @Product AND p.ListPrice < @MaxPrice;

	SET @ListPrice = (SELECT MAX(p.ListPrice)
	  FROM Production.Product AS p
    INNER JOIN  Production.ProductSubcategory AS s 
	    ON p.ProductSubcategoryID = s.ProductSubcategoryID
		WHERE s.[Name] LIKE @Product AND p.ListPrice < @MaxPrice);

	SET @ComparePrice = @MaxPrice;
END

Execution

DECLARE @ComparePrice MONEY, @Cost MONEY;
EXECUTE Production.uspGetList '%Bikes%', 700, @ComparePrice OUTPUT, @Cost OUTPUT

Insert Stored Procedure Results Into a Temporary Table

-- Before we do this task, we have to do to enable ad-hoc distributed queries.
sp_configure 'Show Advanced Options', 1
GO
RECONFIGURE
GO
sp_configure 'Ad Hoc Distributed Queries', 1
GO
RECONFIGURE
GO

-- Insert into Temp Table
SELECT * 
INTO #TempTable
FROM OPENROWSET('SQLNCLI', 'Server=(local)\\SS14;Trusted_Connection=yes;','EXEC [DatabaseName].[dbo].[Sp_Name]')

Best Practices