菜鸟请教语法问题

kinki1980 2005-09-23 09:56:35
CREATE PROCEDURE up_select
@checked boolean,
@type varchar(50),
@edit varchar(50)
AS
if (@checked)
select * from customer where @type like' %'@edit% 'order by custid
else
select * from customer where @type=@edit order by custid

我是新手。。看了半天也不知道怎么回事。。请赐教!!
...全文
175 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
filebat 2005-09-27
  • 打赏
  • 举报
回复
楼主,你那个不行的@type是用来表示列名的。
exec('select * from customer where @type=+'''+'@edit'+'''+ order by custid')
中就不是列名啊。

wgsasd311(自强不息) 就可以,你试过了没?
kinki1980 2005-09-27
  • 打赏
  • 举报
回复
顶一顶!
churchatp1 2005-09-23
  • 打赏
  • 举报
回复
用动态sql
因为你的@type,@edit 都是字符串,假设分别为‘a’,‘b’
select * from customer where @type=@edit order by custid
就相当于select * from customer where 'a'='b' order by custid
当然不对了
kinki1980 2005-09-23
  • 打赏
  • 举报
回复
谢谢。。最好能讲解下。。不好意思。。我太菜了。。!!
wgsasd311 2005-09-23
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_select
@checked bit,
@type varchar(50),
@edit varchar(50)
AS
if @checked=1
exec('select * from customer where '+ @type+' like ' +'''%'+@edit+'%'''+'order by custid ')
else
exec('select * from customer where '+@type+'='+@edit+' order by custid')
kinki1980 2005-09-23
  • 打赏
  • 举报
回复
服务器: 消息 156,级别 15,状态 1,过程 up_select,行 7
在关键字 'select' 附近有语法错误。
服务器: 消息 156,级别 15,状态 1,过程 up_select,行 8
在关键字 'else' 附近有语法错误。
kinki1980 2005-09-23
  • 打赏
  • 举报
回复
exec('select * from customer where @type=+'''+'@edit'+'''+ order by custid')
???????????
kinki1980 2005-09-23
  • 打赏
  • 举报
回复
原来还有这个问题啊。。那要怎么解决啊。。详细点好吗?
这样行吗?
exec('select * from customer where @type='+@edit+' order by custid')

27,581

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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