Saturday, September 2, 2017

Change stored procedure output column names and data types

SQL Server 2012 introduced “WITH RESULT SETS” option which can be used with EXECUTE command to redefine the metadata of result sets being received from stored procedures. This option can handle single as well as multiple result sets.
WITH RESULT SETS” option can be useful if we want to reuse an existing stored procedure’s result set(s) with different column name and / or data type. 

Using WITH RESULT SETS to redefine column names and data types of stored procedure’s result set

Using WITH RESULT SETS option, we can redefine the metadata of result set(s) of a stored procedure during execution. We can use the below code to change the column names and data types of result sets(s) of stored procedure without making any change in the existing code:

EXEC [dbo].[uspGetEmployeeManagers] @BusinessEntityID = 101
WITH RESULT SETS
(
 (
 ReportingLevel INT,
 BusinessEntityID INT,
 FirstName NVARCHAR(50),
 LastName NVARCHAR(50),
 OrganizationNode NVARCHAR(MAX),
 ManagerFirstName NVARCHAR(50),
 ManagerLastName NVARCHAR(50)
 )
)


Below is the syntax to define column names and data types in case we need to handle multiple result sets:

EXEC ProcedureName @Param = 'Value'
WITH RESULT SETS
(
--Result set 1
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 2
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 3
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
)
--And so on
)

WITH RESULT SETS option – Limitations

  1. We cannot redefine a subset of columns – Either all columns of a result set needs to be defined or none of them can be defined using WITH RESULT SETS option.
  2. Cannot redefine a subset of result sets – In case the stored procedure return multiple result sets, either all result sets (with all columns) need to be defined or none of them can be defined.
  3. Cannot change the order of the columns and result sets – We cannot change the sequence of columns and result sets.
  4. Cannot use calculation or type casting – We cannot use calculations or type casting with the columns.

Friday, September 1, 2017

Stored Procedure Metadata - 2012 onwards

Stored Procedure Metadata

In MS SQL Server v2012, Microsoft introduces a couple of new ways to gather meta from Stored Procedure.
The new functions are:
  1. sp_describe_first_result_set ( Stored Procedure )
  2. sys.dm_exec_describe_first_result_set_for_object ( table value function )

Usage

sys.dm_exec_describe_first_result_set_for_object

To review the result set returned by a Stored Prcoedure, please use sp_describe_first_result_set_for_object; a table value function.
The Stored Procedure’s Object ID should be passed in.
SELECT * FROM sys.dm_exec_describe_first_result_set_for_object (object_id('uspGetBillOfMaterials'),1) SELECT * FROM sys.dm_exec_describe_first_result_set ('EXEC uspGetBillOfMaterials',NULL,1) SELECT * FROM sys.dm_exec_describe_first_result_set ('SELECT * FROM Sales.vStoreWithContacts',NULL,1) select tblRS.column_ordinal , tblRS.name , tblRS.system_type_name , tblRS.max_length , tblRS.is_nullable , tblRS.is_identity_column , is_part_of_unique_key from sys.dm_exec_describe_first_result_set_for_object ( @objectID , 0 ) tblRS order by tblRS.column_ordinal


-- When @browse_information_mode=0, it will give you the meta data but no source data available in this option.
-- When @browse_information_mode=1, it will give you the meta data along with the source info but the source details will be based on this view's table.
-- When @browse_information_mode=2, it will give you the meta data along with the source info but the source details will be based on this view.

Encrypt/Decrypt the App.Config

Program.cs using System; using System.Diagnostics; using System.IO; namespace EncryptAppConfig {     internal class Program     {         pr...