SQL查询问题 [英] SQL query issue

查看:90
本文介绍了SQL查询问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好
我是sql server中的新手.我想编写一个查询以检索有关学生的数据.就像我输入的名字一样.
我试过了,但是没用.

Hi all
I am new in sql server. I want to write a query that retrives data about student. It is like a piece of name that I enter.
I tried it but it does not work.

create proc select_name
(
@Name nvarchar(50)
)
as
begin
select *
from student
where
Name like '% @Name %'
end


拜托,有人可以帮我吗?

谢谢大家


Please, can anyone help me?

Thanks all

推荐答案

错误在这里:
Name like ''% @Name %''

您已在where子句中的%之前和之后给定了空格.因此,当您实际上想要带有"ant"的单词时,它会尝试找到类似"ant"的东西

只需删除空格,然后重试.
应该是:
Error lies here:
Name like ''% @Name %''

You have given space after and before % in the where clause. Thus, it tries to find something like '' ant '' when you actually want words that has just ''ant''

Just remove the spaces and re-try.
It should be:
WHERE Name like '%'+@Name+'%'



第二个错误是引号中包含的参数需要保留在外面.



Second mistake was parameter included inside the quotations which needs to be kept outside.


从应用程序mona%发送%.这意味着youe @Name参数将包含Mona%来查找以mona开头的名称.然后直接在SP中使用where Name like @Name.


您的程序
Send the % from your application that is mona% . That means youe @Name parameter will contain Mona% to find name started with mona. Then use where Name like @Name directly in your SP.


Your Procedure
create proc select_name
(
@Name nvarchar(50)
)
as
begin
select *
from student
where
Name like @Name
end



程序调用



Procedure Call

Exec select_name ''Mona%''


[END EDIT]


[END EDIT]


这篇关于SQL查询问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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