Methods for passing and using in parameters in SQL during stored procedures

Source: Internet
Author: User
Tags string methods

Background:

1. Using Stored Procedures

2. The stored procedure has in

3, in parentheses inside the contents as parameters passed

Solution: 1, directly splicing SQL

You can stitch strings in a stored procedure, and then execute this string, similar to the eval in JS

PROCEDURE [dbo]. [Inselect]    @P_0 NVARCHAR (+)        as  DECLARE @A VARCHAR (6000)  SET @a= ' SELECT * from TESTTB WHERE zip in (' [Email protected]_0 + ') '

Then use the system stored procedure sp_executesql execute @a can!

2. Define a method to cut a character into a collection

Cutting string methods

Create function  func_split_hongfei0416j (@SourceSql   varchar (8000), @StrSeprate   varchar (2))     Returns   @temp   table (col   varchar) as       begin     Declare   @ch as   varchar (     set   @[email protected][email protected] while       (@SourceSql <> ")                     begin                     Set   @ch =left (@SourceSql, CHARINDEX (@StrSeprate, @SourceSql, 1)-1)     insert   @temp   values (@ch)     Set   @SourceSql =stuff (@SourceSql, 1,charindex (@StrSeprate, @SourceSql, 1), ")                     end     return     

Stored Procedure Writing Demo

CREATE PROC ps_spit_test      (     @lettertype VARCHAR) as      BEGIN        select * from Y_paper   where Lettertype in (select Col as Lettertype from dbo.func_split_hongfei0416j (@lettertype, ', ')) Endexec Ps_spit_ Test ' 118,148,163 '

If it is simple SQL directly using the first method,

The second method can be used for complex writes.

You can also use the temporary table method,

However, you still need to cut the string first.

Methods for passing and using in parameters in SQL during stored procedures

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.