SQL statement stitching in SQL Server

Source: Internet
Author: User

SQL statements are primarily manipulated for conditional additions, deletions, modifications, or queries.
However, in a variety of variable query conditions, the length of the SQL statement is not necessarily, it will require our SQL statement stitching.
Like what:
String sqlstr= "Select condition 1, Condition 2, Condition 3, Condition 4 from table 1 S1 INNER JOIN table 2 where";
When making multiple table queries in a program, because the length of the SQL statement is variable, we need string concatenation.

Sqlstr + = "(S1. s_id like @query or S1. S_name like @query or S1. S_mobile like @query or S1. S_address like @query or S1. S_crad like @query) and ";

Because I don't know how many SQL statements to splice, so
Add and after each SQL statement.

SqlParameter name = new SqlParameter ("@query", "%" + TextBox2.Text + "%");
Comm. Parameters.Add (name);

And then splicing the next SQL statement.

At the end of the final spliced SQL statement, write:

Comm.commandtext = Sqlstr. TrimEnd (new char[] {' A ', ' n ', ' d ', '});

Because TrimEnd is a trailing match for a set of characters specified in the array from the current System.String object,

So, we need to define an array new char[]{' a ', ' n ', ' d '} on top.

If you do not remove and, the program will error.

SQL statement stitching in SQL Server

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.