Stored Procedures with search criteria paging and stored procedures with search criteria

Source: Internet
Author: User

Stored Procedures with search criteria paging and stored procedures with search criteria

This storage process mainly involves two aspects:

1. dynamically splice the paging of query Conditions

2. Dynamically Retrieve the total number of rows for the current search Condition

Note: If you use this join query condition to pass in, you must first filter the special strings of the form to make the system more secure.

Alter procedure [dbo]. [OrdersListByCondition] @ pagesize int, @ pageindex int, @ condition nvarchar (1000), @ totalcount int outputASBEGINdeclare @ SQL nvarchar (3000) declare @ countSql nvarchar (3000) if @ condition = ''begin
-- Set @ SQL = 'select * from (select top '+ cast (@ pagesize as nvarchar (30 )) + '* from E_Orders where OrderID not in (select top (' + cast (@ pagesize as nvarchar (30 )) + '* (' + cast (@ pageindex as nvarchar (30) + '-1) OrderID from E_Orders order by OrderID desc) o left join E_OrdersCommodity oc on o. orderID = oc. orderID order by OrderID desc 'set @ countSql = 'select @ a = count (1) from E_Orders 'endelsebegin set @ SQL = 'select * from (select top '+ cast (@ pagesize as nvarchar (30 )) + '* from E_Orders where OrderID not in (select top (' + cast (@ pagesize as nvarchar (30 )) + '* (' + cast (@ pageindex as nvarchar (30) + '-1) OrderID from E_Orders where' + @ condition + 'order by OrderID desc) and '+ @ condition + 'order by OrderID desc) o left join E_OrdersCommodity oc on o. orderID = oc. orderID order by OrderID desc 'set @ countSql = 'select @ a = count (1) from E_Orders where '+ @ condition + ''endexec (@ SQL) execute sp_executesql @ countSql, n' @ a INT output', @ totalcount outputEND

The format of the background merging parameters must be noted: field name = 'value', condition such as OrderID = '000000' and Contacts = '000000'

/// <Summary> /// concatenate SQL query conditions /// </summary> /// <param name = "strOrderStatus"> </param> /// <param name = "strOrderID"> </param> // <param name = "strMallName"> </param> // <param name = "strContacts"> </param> /// <returns> </returns> private string [] GetConditionArray (string strOrderStatus, string strOrderID, string strMallName, string strContacts) {List <string> list = new List <string> (); if (strOrderStatu S! = "") {List. Add ("OrderStatus = '" + strOrderStatus + "'");} if (strOrderID! = "") {List. Add ("OrderID = '" + strOrderID + "'");} if (strMallName! = "") {List. Add ("MallName = '" + strMallName + "'");} if (strContacts! = "") {List. Add ("Contacts = '" + strContacts + "'");} return list. ToArray ();}

 

Related Article

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.