SQL Server in implementation parameterized query XML TYPE SOLUTION

Source: Internet
Author: User
1: If the parameter is of the int type, declare @ axmlset @ arowid1idrowrowid5idrowrowid4idrowrowid3idrowrowid2idrowselect * fromproductwhereidin (selectd. x. value (. id [1], int) from@a.nodes (*) asd (x) 2: If the parameter is var

1: If the parameter is of the int type: declare @ a xml set @ a = 'rowid1/id/row rowid5/id/row rowid4/id/row rowid3/id/row rowid2/id/row' select * from product where id in (select d. x. value ('. /id [1] ', 'int') from @. nodes ('/*') as d (x) 2: If the parameter is var

1: IfParametersYes intType:

Declare @ a xml
Set @ a ='
1
5
4
3
2 '
Select * from product where id in (
Select d. x. value ('./id [1]', 'int') from @ a. nodes ('/*') as d (x ))

2: IfParametersYes varcharType:

Declare @ a xml
Set @ a ='
A
B5
C4
D3
E2 '
Select * from product where pname in (
Select d. x. value ('./name [1]', 'varchar (100) ') from @ a. nodes ('/* ') as d (x ))

C # simple method written in the first example

Public DataSet GetData (List
 
  
IdList) {System. Text. StringBuilder idXML = new System. Text. StringBuilder (); // converts IdList to idxml (the xml to be used later)
  Parameters) Foreach (var item in idList) {idXML. AppendFormat ("
  
   
    
{0}
   
  ", Item);} System. text. stringBuilder strSql = new System. text. stringBuilder (); strSql. append ("select * from product where id in ("); // Parse xml
  Parameters@ A extracts the ID. Here we can think of @ a as a special table strSql. append ("select d. x. value ('. /id [1] ', 'int') from @. nodes ('/*') as d (x) "); strSql. append (")"); SqlConnection con = new SqlConnection ("database connection string"); SqlCommand cmd = new SqlCommand (strSql. toString (), con );//
  ParametersAssign SqlParameter [] para = new SqlParameter [] {new SqlParameter ("@ a", SqlDbType. xml) {Value = idXML. toString () }}; cmd. parameters = para ;//
  QuerySqlDataAdapter sda = new SqlDataAdapter (cmd); DataSet ds = new DataSet (); sda. Fill (ds); return ds ;}
 

If you think it is a good article, please do not mean your suggestion. It is not easy to write an 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.