[英]Parameter Passing for Stored Procedure
I need help in fetching records using a stored procedure, where I need to pass a single parameter from the stored procedure, but I need to fetch both semester records 我需要使用存储过程来获取记录的帮助,我需要从存储过程中传递一个参数,但是我需要获取两个学期的记录
Declare @sem varchar(10)
set @sem ='1sem'
Ex: select * from student where TestType = '1sem' 例如:从TestType ='1sem'的学生中选择*
set @sem ='2sem'
select * from student where TestType = '2sem'
set @sem ='dual'
If I need to select both 1st sem and 2 sem, I have only one parameter but how to handle it with the stored procedure. 如果我需要同时选择第一学期和第二学期,那么我只有一个参数,但是如何使用存储过程来处理它。
Select *
from student
where TestType where?
How can I achieve 我该如何实现
select *
from student
where TestType = '1sem' or TestType = '2sem'
you can try to separate the testtype value with "," (comma) 您可以尝试使用“,”(逗号)分隔testtype值
then change your sql: 然后更改您的sql:
set @sem = '1sem, 2sem'
set @sql = 'select * from student where testype in ('+@sem+')'
execute sp_executesql @sql
you can use something like this 你可以用这样的东西
DECLARE @sem VARCHAR(10) = '1sem';
select * from student where testype = @sem OR (@sem = 'dual' AND testype IN '1sem','2sem');
You must declare @sem as table and insert record 您必须将@sem声明为表并插入记录
DECLARE @sem TABLE
(
Testype VARCHAR(50),
)
INSERT INTO @sem
select Testype from student where TestType = '1sem' or TestType = '2sem'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.