2 December 2011

Dynamic Stored Procedure example in Sql Server


Dynamic Stored Procedure example in Sql Server
/*
EXEC Proc_get_gridCount 'MUser','StatusId=1'
--Dynamic Stored Procedure example , to get the table and name and Condition and get the result
*/


Create Procedure Proc_get_gridCount(
@Tablename as varchar(50),
@Condition as varchar(300)
)

as

begin

set nocount on

declare @StrSelectSQL as nvarchar(500)  --It should be nvarchar, if you declare varchar error will come.
SELECT @StrSelectSQL = ''                                

SELECT @StrSelectSQL = @StrSelectSQL + ' Select Count(*) From '    
SELECT @StrSelectSQL = @StrSelectSQL +  @TableName                  
SELECT @StrSelectSQL = @StrSelectSQL + ' Where '
SELECT @StrSelectSQL = @StrSelectSQL + @Condition

 -- print @StrSelectSQL
     
EXEC SP_EXECUTESQL @StrSelectSQL  


END

No comments:

Post a Comment

Comments Welcome

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...