使用参数列出表中的所有数据 [英] List all data in the table with paremeter
本文介绍了使用参数列出表中的所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想列出学生表中的所有学生详细信息,但是有一个参数@Name,我们必须传递学生的姓名。但我想要所有学生,我必须通过什么作为参数列出所有学生? ,这里我无法更改存储过程..
创建 < span class =code-keyword> proc ListStudents @ Name varchar ( 20 )
as
begin
选择 * 来自 StudentDet 其中名称= @姓名
结束
解决方案
试试这个:
创建 PROC ListStudents
@ Name varchar ( 20 )= ' '
AS
BEGIN
SELECT * < span class =code-keyword> FROM StudentDet WHERE 名称 LIKE ' ' + @ Name + ' %'
END
当您需要详细信息时特别的学生,你可以传递这个名字。如果你没有传递名字,那么它将列出所有学生的详细信息。
EXEC ListStudents ' Amit' - 将列出Amit的详细信息
EXEC ListStudents - 将列出所有学生的详细信息
- Amit
创建 < span class =code-keyword> PROC ListStudents
@ Name varchar ( 20 )= ' '
AS
BEGIN
SELECT * FROM StudentDet WHERE 名称= 案例 当 @ Name = ' ' 名称其他姓名结束
END
EXEC ListStudents ' Anudeep' - 将返回学生详细信息名称= Anudeep
EXEC ListStudents ' ' - - 将返回所有学生详细信息'您想要的输出'
I want to list all student details from the student table, but there is a parameter @Name where we have to pass the name of the student. But i want all students , what i have to pass as the parameter to list all students? , here i cant change the stored procedure..
create proc ListStudents @Name varchar(20)
as
begin
select * from StudentDet where Name=@Name
end
解决方案
Try this:
CREATE PROC ListStudents @Name varchar(20)='' AS BEGIN SELECT * FROM StudentDet WHERE Name LIKE ''+@Name+'%' END
When you need the details of particular student you can pass the name. If you'll not pass name then it'll list the details of all students.
EXEC ListStudents 'Amit'--Will list the details of Amit EXEC ListStudents -- Will list the details of all students
--Amit
CREATE PROC ListStudents @Name varchar(20)='' AS BEGIN SELECT * FROM StudentDet WHERE Name = Case When @Name = '' then Name Else Name End END EXEC ListStudents 'Anudeep' -- Will Return Student Detail Name = Anudeep EXEC ListStudents '' -- Will Return All Student Detail 'You Desired Output'
这篇关于使用参数列出表中的所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文