Friday, November 25, 2011

Quick Data Selection

 Create Procedure S
(
    @Table    VarChar(Max),
    @TopN    VarChar(18)     = '999999999999999999',
    @fld    VarChar(Max) = ''
)
As
Begin
    Set NoCount On
    Declare @s VarChar(Max)

    Set @s = 'Select Top('+ @TopN +') * From ' + @Table

    If @fld <> ''
        Set @s = @s + ' Where ' + @fld
      
    Exec (@s)
End


Mainly the above sp used for quick selection from table or view data at develeping.

Suppose we have a table named Employee. We want show all data from that table.Normaly we just type "Select * from Employee" and it will run.We can simply execute this as " S 'Employee' "
Another , we want to show only the top ten record then execute as " S 'Employee',10 " and finally we need to filter record with Department field. How to do?

just run " S 'Employee',10,"Department = 100"

Sorry for my bad English.

No comments:

Post a Comment