关于like在存储过程中的使用

fanqie514 2010-07-22 09:09:07
create procfeduer GetAll
@id varchar(20)='%20%'
as
select * from name where num like @id
go


execute GetAll调用这个存储过程的时候怎么动态使用@id啊?,这个不是在变量中写死了是20的吗?要怎么做啊?


求教存储过程中使用like 的方法
...全文
143 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
王向飞 2010-07-23
  • 打赏
  • 举报
回复
每天回帖即可获得10分可用分!
ChinaITOldMan 2010-07-23
  • 打赏
  • 举报
回复
create procfeduer GetAll
@id varchar(20)='20'
as
select * from name where num like '%'+@id+'%'
go
liujingkun 2010-07-23
  • 打赏
  • 举报
回复
create procfeduer GetAll
@id varchar(20)
as
select * from name where num like @id
go

execute GetAll '%20%'
kuxuan21 2010-07-23
  • 打赏
  • 举报
回复
create procfeduer GetAll 楼主关键字错了!

if exists(select 1 from sysobjects where name='GetAll')
drop proc GetAll
go

create proc GetAll
@id varchar(20)
as
select * from name where num like @id
go

exec GetAll '%20%'
go


htl258_Tony 2010-07-23
  • 打赏
  • 举报
回复
--法一:
create procfeduer GetAll
@id varchar(20)='%20%'
as
select * from name where num like @id
go
--默认值方式:
execute GetAll
--指定参数值方式:
execute GetAll '%120%'

--法二:
create procfeduer GetAll
@id varchar(20)='20'
as
select * from name where num like '%'+@id+'%'
go
--默认值方式:
execute GetAll
--指定参数值方式:
execute GetAll '120'
CXZ84 2010-07-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 sql77 的回复:]
SQL code
create procfeduer GetAll
@id varchar(20)='20'
as
select * from name where num like '%'+@id+'%'
go
[/Quote]

这里的20是id的默认值,你还可以传入其他的值
sunrisehy2003 2010-07-23
  • 打赏
  • 举报
回复
支持四楼
rart2008 2010-07-23
  • 打赏
  • 举报
回复

create procfeduer GetAll
@id varchar(20) --这里不能直接赋值,声明一个变量就行了
as
select * from name where num like '%'+@id+'%' --用like要加上%,这里是找出包含@id的字段
go

--执行存储过程
execute GetAll 20 --存储过程后面跟变量@id的值
wanglingzhong 2010-07-22
  • 打赏
  • 举报
回复
不明白楼主的动态使用ID。
正如大家的做法,执行存储过程的时候传参数即可
hhucxyb 2010-07-22
  • 打赏
  • 举报
回复

create proc GetAll
@id varchar(20)
as
declare @sql nvarchar(1000)
set @sql='select * from name where num like ''%'+@id+'%'''
exec(@sql)
go
永生天地 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 fanqie514 的回复:]
create procfeduer GetAll
@id varchar(20)='%20%'
as
select * from name where num like @id
go


execute GetAll调用这个存储过程的时候怎么动态使用@id啊?,这个不是在变量中写死了是20的吗?要怎么做啊?


求教存储过程中使用like 的方法
[/Quote]

这个不是在变量中写死了是20的吗?
------------->这样定义叫做参数的默认值。
有默认值时,如果在调用时没有赋值,就是用其默认值,
如果赋值,就使用指定的值,比如

execute GetAll '%102%'
fanqie514 2010-07-22
  • 打赏
  • 举报
回复
额,,早上程序也这么弄得貌似查询来不对,现在在数据库中查倒是对了,明天在去试试
也好像听别人说要把%%写变量中什么的不是很懂啊。
SQL77 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 obuntu 的回复:]
引用 2 楼 sql77 的回复:

SQL code
DECLARE @id varchar(20)
SET @id='''%20%'''

PRINT('select * from name where num like '+@id)

/*select * from name where num like '%20%'


77的SQL 写的真是牛逼。。
[/Quote]
过奖了,小虾而已
SQL77 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fanqie514 的回复:]
SQL code

create procfeduer GetAll
@id varchar(20)
as
select * from name where num like '%'+@id+'%'
go


execute GetAll '20'
这样是不是就可以了? 20这个变量随便变?
[/Quote]
楼主试一下就知道了
fanqie514 2010-07-22
  • 打赏
  • 举报
回复

create procfeduer GetAll
@id varchar(20)
as
select * from name where num like '%'+@id+'%'
go


execute GetAll '20'
这样是不是就可以了? 20这个变量随便变?
obuntu 2010-07-22
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 sql77 的回复:]

SQL code
DECLARE @id varchar(20)
SET @id='''%20%'''

PRINT('select * from name where num like '+@id)

/*select * from name where num like '%20%'
[/Quote]

77的SQL 写的真是牛逼。。

SQL77 2010-07-22
  • 打赏
  • 举报
回复
DECLARE @id varchar(20)
SET @id='''%20%'''

PRINT('select * from name where num like '+@id)

/*select * from name where num like '%20%'
SQL77 2010-07-22
  • 打赏
  • 举报
回复
create procfeduer GetAll
@id varchar(20)='20'
as
select * from name where num like '%'+@id+'%'
go

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧