Friday 14 July 2017

Variable to SQL Query

Sometimes there is a need to store a SQL query output to a variable in store procedure. This feature gives programming flexibility to apply more action while generating output.
For example, you can apply more group option in a simple way which was complicated in a single query or if you want to concatenate the 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






No comments:

Post a Comment