创建一个存储过程,该存储过程使用varchar(2)字符作为输入并返回匹配的结果 [英] creating a stored procedure which takes a varchar(2) character as input and return a matching results
本文介绍了创建一个存储过程,该存储过程使用varchar(2)字符作为输入并返回匹配的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
更改proc returnmatchingrecords
@inputstring varchar(2)
如
开始
从返回记录中选择*,例如%@ inputstring%"之类的城市
结束
.如何纠正呢?谢谢.
解决方案
u甚至可以用这种方式
创建 过程 getdetails( @inputstring varchar ( 2 )) 为 选择 * 从返回记录其中子字符串(城市, 1 , 2 )= @ inputstring -由于输入字符串的数据类型为varchar(2),因此我假设您需要获取所有记录,其中城市名称的前两个字母为城市名称.认为这是您的要求
完成了!这是我所做的:
更改 proc 返回匹配记录 @ inputstring varchar ( 2 ) 为 开始 选择 * 来自返回记录所在位置城市喜欢 ' %' + @ inputstring + ' %' 结束
并起作用!
声明 @ inputstring varchar ( 3 )= ' span> 201' 选择 * 来自 TableName 位置 PATINDEX(' %,' + CAST(ColumnName AS VARCHAR ( 30 ) )+ ' ,%',' ,' + @ inputstring + ' ,')> 0
i want to create a procedure which takes a input parameter and returns all results from table which have starting character same as input provided. For this i write this query :
alter proc returnmatchingrecords
@inputstring varchar(2)
as
begin
select * from returnrecords where city like ''%@inputstring%''
end
. how to correct it? Thanks.
解决方案
u can even do it this way
create procedure getdetails(@inputstring varchar(2)) as select * from returnrecords where substring(city,1,2)=@inputstring --since the input string is having varchar(2) as the data type i presume that u need to get all the records where the city name has first two letters as the city name.I thought that was your requirement
done it! here what i have done:
alter proc returnmatchingrecords @inputstring varchar(2) as begin select * from returnrecords where city like '%'+ @inputstring+ '%' end
and its working!
Declare @inputstring varchar(3) = '201' Select * From TableName Where PATINDEX('%,' + CAST(ColumnName AS VARCHAR(30))+',%', ',' +@inputstring + ',') > 0
这篇关于创建一个存储过程,该存储过程使用varchar(2)字符作为输入并返回匹配的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文