Friday, 14 July 2017

Variable to SQL Query

Some time there is a need to store a SQL query output to a variable in store procedure. This feature gives a programmer flexibility to apply more action while generating output.
For example you can apply more group option in a simple way which was complicated in single query or if you want concatenate output of some query etc.

USE [Girfa_Portal]
GO
SET ANSI_NULLS ON

GO
SET QUOTED_IDENTIFIER ON
GO


alter PROCEDURE [dbo].[Girfa]

AS
BEGIN
SET NOCOUNT ON;
;With MyVar AS
(
   SELECT * FROM [dbo].[Student]

)
SELECT firstName+lastname as name
       FROM
       Myvar WHERE LastName='Yadav'
      
SET NOCOUNT OFF;
END