SQL generic efficient paging code (stored procedures include SQL injection)
SQL generic efficient paging code (stored procedures include SQL injection)
SQL generic efficient paging code (with SQL injection)
You can see that in the above stored procedure, an SQL string is finally spliced through some steps, and then the results are displayed by executing the string through exec.
Let's assume that we want to perform such a query and use username to fuzzy query users. For the sake of convenience and convenience, we only consider the first page, the serial number of the first page retrieved from the stored procedure is as follows:
For ease of Problem description, we can assume that @ pagesize is 20, @ strgetfields is '*', @ tblname is useraccount, @ strorder: 'order by id desc '. The preceding line can be written as follows:
Set @ strsql = 'select top 20 * from [useraccount] where' + @ strwhere + 'order by id desc'
We can assume that the fuzzy user name entered by the user is jim's dog.
We use sqlparameter to pass parameters to the paging Stored Procedure @ strwhere. The value is: 'username like ''' % jim ''dog % ''' (note that the single quotation marks in the string behind like have all been changed to two single quotation marks ), we will place this value into the above @ strsql value assignment statement, as follows:
Set @ strsql = 'select top 20 * from [useraccount] where username like ''% jim'' dog % ''order by id desc'
Let's write the Declaration variable and test it in the query analyzer. The Code is as follows:
Declare @ strsql varchar (8000)
Declare @ strwhere varchar (1000)
Set @ strwhere = 'username like ''' % jim ''dog % '''
Set @ strsql = 'select top 20 * from [useraccount] where' + @ strwhere + 'order by id desc'
Print @ strsql
Exec (@ strsql)