SQL general efficient paging code (stored procedures include SQL injection) (1/2)

Source: Internet
Author: User
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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.