Friday, November 23, 2012

Dynamic Stored Procedure for Select

Dynamic Stored Procedure for Select Query
this Genic Stored Procedure for all Tables


USE [itechgal_lkms]
GO
/****** Object:  StoredProcedure [dbo].[sp_Generic]    Script Date: 11/24/2012 10:40:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*
Procedure Name : sp_Generic
Created by : Chiru
Created On : 22-10-2010
Modified By :  
Description :
               
*/
 
 
ALTER procedure [dbo].[sp_Generic]                  
   (
   
    @colName varchar(2000),
    @tableName varchar(2000),
    @whereCond varchar(2000)
    )
   
           
As

declare @sql varchar(4000)

if @whereCond <>''
    begin
        select @sql = 'select '+ @colName +' from ' + @tableName + ' Where ' + @whereCond
    end
else
    begin
        select @sql = 'select '+ @colName +' from ' + @tableName
    end
   
    exec (@sql)

No comments:

Post a Comment