Home Database Mysql Tutorial sql存储过程in参数如何传递及问题解决

sql存储过程in参数如何传递及问题解决

Jun 07, 2016 pm 05:49 PM
stored procedure

下面来介绍在sql存储过程in参数如何传递及问题解决,有需要的朋友可参考参考。

--1. 构造使用IN子句的动态Transact-SQL方法进行编号查询

--a. 要查询的字段类型是数字型

 代码如下 复制代码

--查询的值列表
DECLARE @idlist varchar(100)
SET @idlist='1,2,3'

--拼接并执行动态Transact-SQL语句
EXEC('SELECT * FROM tbname WHERE fdname IN('+@idlist+')')
GO

--b. 要查询的字段类型是字符型
--查询的值列表已经加上了字符串边界符
DECLARE @idlist varchar(100)
SET @idlist='''a'',''b''''a'',''c'''

--拼接并执行动态Transact-SQL语句
EXEC('SELECT * FROM tbname WHERE fdname IN('+@idlist+')')
GO

--查询的值列表没有字符串边界符
DECLARE @idlist varchar(100)
SET @idlist='a,b''a,c'

--由于是字段类型是,所以在拼接时,必须为其加上字符串边界符(')
DECLARE @s varchar(1000)
SET @s=''''
    +REPLACE(REPLACE(@idlist,'''',''''''),',',''',''')
    +''''

--拼接并执行动态Transact-SQL语句
EXEC('SELECT * FROM tbname WHERE fdname IN('+@s+')')
GO

/*=====================================================*/


--2. 使用LIKE或者PATINDEX进行编号查询
--查询的值列表
DECLARE @idlist varchar(100)
SET @idlist='1,2,3'

--查询
SELECT * FROM tbname WHERE CHARINDEX(','+RTRIM(fdname)+',',','+@idlist+',')>0
SELECT * FROM tbname WHERE PATINDEX('%,'+RTRIM(fdname)+',%',','+@idlist+',')>0
SELECT * FROM tbname WHERE ','+@idlist+',' LIKE '%,'+RTRIM(fdname)+',%'
GO

/*=====================================================*/


--3. 编号查询中常见的错误
--a. 最容易犯的错误:表达式充当表达式列表。
DECLARE @s varchar(100)
SET @s='1'
SELECT id,name FROM sysobjects WHERE id IN(@s)
/*--结果
id          name
---------------- ------------
1           sysobjects
--*/

SET @s='1,2,3'
SELECT id,name FROM sysobjects WHERE id IN(@s)
/*--结果
服务器: 消息 245,级别 16,状态 1,行 3
将 varchar 值 '1,2,3' 转换为数据类型为 int 的列时发生语法错误。
--*/
GO

--b. 生成动态Transact-SQL语句时忽略了数据类型。
DECLARE @s varchar(100)
SET @s='U,S'
EXEC('SELECT id,name FROM sysobjects WHERE id IN('+@s+')')
/*--结果:
服务器: 消息 207,级别 16,状态 3,行 1
列名 'S' 无效。
服务器: 消息 207,级别 16,状态 1,行 1
列名 'U' 无效。
--*/
GO

--c. 忽略了比较的精确性问题。
--要查询的数据
DECLARE @t TABLE(col varchar(10))
INSERT @t SELECT '1'
UNION ALL SELECT '11'
UNION ALL SELECT '111'
UNION ALL SELECT '22'

--查询
DECLARE @s varchar(100)
SET @s='111,22'
SELECT * FROM @t WHERE CHARINDEX(col,@s)>0
/*--结果
col       
----------
1
11
111
22
-*/
GO

在SQL Server Management Studio里执行这个(我使用的是SQL Server 2005如果是SQL 2000这个函数要改些内容如:VARCHAR(MAX)),会生成一个表值函数,然后使用时呢,比如你想实现:In(@ids)这时可以换成 In(select * from Split(@ids , ','))

这个函数的作用呢,就是把你输入的字符按一定的分隔符分开,并放在一个表里的一列里,然后返回。

 代码如下 复制代码

--引用 Select * From Split('1,2,3' , ',')
--引用 Select * From Split('我,是,www.111cn.net' , ',')
CREATE   FUNCTION [dbo].[Split]
(
@c VARCHAR(MAX) ,
@split VARCHAR(50)
)
RETURNS @t TABLE ( col VARCHAR(50) )
AS
BEGIN
    WHILE ( CHARINDEX(@split, @c) 0 )
        BEGIN
            INSERT  @t( col )
            VALUES  ( SUBSTRING(@c, 1, CHARINDEX(@split, @c) - 1) )
            SET @c = STUFF(@c, 1, CHARINDEX(@split, @c), '')
        END
    INSERT  @t( col ) VALUES  ( @c )
    RETURN
END

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Steps and precautions for implementing batch updates using Oracle stored procedures Steps and precautions for implementing batch updates using Oracle stored procedures Mar 08, 2024 pm 04:12 PM

Title: Steps and Precautions for Implementing Batch Updates by Oracle Stored Procedures In Oracle database, stored procedures are a set of SQL statements designed to improve database performance, reuse code, and enhance security. Stored procedures can be used to update data in batches. This article will introduce how to use Oracle stored procedures to implement batch updates and provide specific code examples. Step 1: Create a stored procedure First, we need to create a stored procedure to implement batch update operations. The following is how to create a stored procedure

Oracle stored procedure: Implementation method to determine whether a table exists Oracle stored procedure: Implementation method to determine whether a table exists Mar 08, 2024 pm 09:18 PM

Stored procedures in Oracle database are a specific type of stored procedures used to execute a series of SQL statements and data operations in the database. In actual database development work, sometimes we need to determine whether a certain table exists in the database, so that we can do some judgment and logical processing in the storage process. Below we will introduce how to implement the method of determining whether a table exists in Oracle database, and provide specific code examples. First, we can use the system table user_tables or all_t

How to delete stored procedure in MySQL How to delete stored procedure in MySQL Sep 05, 2023 am 10:25 AM

MySQL methods for deleting stored procedures include using the DROP PROCEDURE statement, using MySQL Workbench, and using command line tools. Detailed introduction: 1. Use the DROP PROCEDURE statement. The steps are to first open the MySQL client or use any tool that supports MySQL, then connect to your MySQL database, and finally execute the following SQL statement to delete the stored procedure; 2. Use MySQL Workbench to delete Stored procedures and so on.

Implementation principles and applications of Golang stored procedures Implementation principles and applications of Golang stored procedures Feb 22, 2024 pm 04:57 PM

Implementation Principles and Applications of Golang Stored Procedures Stored procedures are precompiled programs that are stored in relational databases and can be called by applications. They can effectively reduce the cost of network transmission of data and improve the execution efficiency of the database. Although Golang does not directly support stored procedures, you can simulate the functions of stored procedures by using SQL statements. This article will introduce the principles and applications of implementing stored procedures in Golang, and provide specific code examples. 1. The implementation principle of Golang stored procedure is in Gol

Performance Optimization Strategy for Oracle Stored Procedure Batch Update Performance Optimization Strategy for Oracle Stored Procedure Batch Update Mar 08, 2024 pm 09:36 PM

Performance Optimization Strategies for Batch Updates of Oracle Stored Procedures In Oracle database, a stored procedure is a database object used to process data logic or perform specific tasks. It can provide certain performance optimization strategies, especially when updating data in batches. Updating data in batches usually involves a large number of row-level operations. In order to improve performance and efficiency, we can adopt some strategies and techniques to optimize the performance of stored procedures. The following will introduce some performance optimization strategies for batch updates of Oracle stored procedures and provide specific code examples.

Detailed comparison and advantage analysis of Oracle stored procedures and functions Detailed comparison and advantage analysis of Oracle stored procedures and functions Mar 03, 2024 am 10:24 AM

Title: Detailed comparison and advantage analysis of Oracle stored procedures and functions. In Oracle database, stored procedures and functions are two important database objects. They can both be used to encapsulate a series of SQL statements and logic to improve the efficiency and complexity of data operations. Usability. This article will compare the characteristics of Oracle stored procedures and functions in detail, as well as their respective advantages, and provide specific code examples. Stored procedure A stored procedure is a set of SQL statements and PL/SQL code logic that are pre-written and stored in the database.

How to write efficient stored procedures using Golang How to write efficient stored procedures using Golang Mar 22, 2023 pm 02:24 PM

Golang is a powerful programming language that can easily implement stored procedures. In this article, we will introduce how to write efficient stored procedures using Golang and the benefits of using them in your projects.

How to write custom stored procedures and functions in MySQL using C# How to write custom stored procedures and functions in MySQL using C# Sep 22, 2023 am 09:42 AM

How to write custom stored procedures and functions in MySQL using C# Introduction: MySQL is a widely used open source database management system, and C# is a commonly used object-oriented programming language. During the development process, we often need to use database stored procedures and functions to improve code reusability and performance. This article will introduce how to use C# to write custom stored procedures and functions in a MySQL database, and provide specific code examples. 1. Stored procedures A stored procedure is a set of SQL statements that perform specific tasks.

See all articles