Home >Database >Mysql Tutorial >在sp_executesql中使用like字句的方法

在sp_executesql中使用like字句的方法

WBOY
WBOYOriginal
2016-06-07 17:58:231090browse

在sp_executesql中可以执行参数化的sql,但是如果sql中有like,那么一般都要拼字符串,下面的例子把两者结合了起来,既有拼sql的灵活性,又有参数化sql的优点。

declare @LikeSql nvarchar(32);--定义一个like变量,如果是存储过程,此处可以存储过程的参数

set @LikeSql = ‘someword%';--把LikeSql变量赋值,可以赋值成'%xxxxxxxxxxxx%'

declare @SelectSql nvarchar(128)--定义Sql语句,开始拼Sql

set @SelectSql = ‘select * from Table where [Column] like @LikePar';—使用@LikePar变量进行参数化

exec sp_executesql @SelectSql ,N'@LikePar as varchar(32)',@LikePar = @LikeSql;--使用sp_executesql来执行
Statement:
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
Previous article:常用SQL功能语句Next article:table 行转列的sql详解