创建一个存储过程,该存储过程使用varchar(2)字符作为输入并返回匹配的结果 [英] creating a stored procedure which takes a varchar(2) character as input and return a matching results

查看:96
本文介绍了创建一个存储过程,该存储过程使用varchar(2)字符作为输入并返回匹配的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个过程,该过程接受一个输入参数,并返回表中具有与提供的输入相同的起始字符的所有结果.为此,我写这个查询:

更改proc returnmatchingrecords
@inputstring varchar(2)

开始
从返回记录中选择*,例如%@ inputstring%"之类的城市
结束
.如何纠正呢?谢谢.

解决方案

u甚至可以用这种方式

 创建 过程 getdetails( @inputstring   varchar ( 2 ))

选择 * 返回记录其中子字符串(城市, 1  2 )= @ inputstring - 


完成了!这是我所做的:

 更改  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆