Mssql in multi-value parameter passing Query

Source: Internet
Author: User

Because IN queries in mssql are multi-value queries, one of your parameters may contain many values, but they are only processed as one string during MSSQL parsing, rather than processing multiple strings, IN multi-value processing is actually processing multiple string values.

Therefore, IN order to achieve the desired IN multi-value parameterized query, dynamic SQL statements must be spliced, because dynamic SQL is actually a dynamic SQL statement consisting of many strings, only execute sp_executesql @ SQL to get the desired result. First, the Northwind database is used for demonstration. The Code is as follows:

 DECLARE @ preName nvarchar (4000)
SET @ preName = '@ Davolio @, @ Fuller @, @ Leverling @'

Declare @ lastName nvarchar (4000)

Set @ lastName = replace (@ preName ,'@','''')
Declare @ SQL nvarchar (2000)
Set @ SQL = n' SELECT * FROM Employees where lastName IN ('+ @ lastName + ')'
Execute sp_executesql @ SQL

The dynamic SQL statement is as follows.

You can also write stored procedures.

Create proc lOOKINFO (@ prename nvarchar (1000 ))

As

.....

 

 

 

 

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.