存储过程中的参数问题

来源:百度知道 编辑:UC知道 时间:2024/05/25 11:52:33
我想在一个存储过程中设置两个参数,一个是列的名字(@colname),一个是列的值(@colvalue),在一个查询中列的名字不固定,该怎么写存储过程,谢谢
比如 学生表:student (code,name)
1.我想查code=1的记录
2.想查name=‘张三’的记录
这样列的名字和值都不固定,能不能将列的名字 做为参数加在 where 后面,
请高人指点。
CREATE PROCEDURE pp_myPro
@colname nvarchar(50),@colvalue nvarchar(50)
AS
BEGIN
declare @sql nvarchar(max)
set @sql='select * from [student] where '+@colname+'='''+@colvalue+''''
exec(@sql)
END

楼上的,谢谢帮助,可是这样也不对啊'+@colname+'这样写是不对的,虽然存储过程对,但执行传参数没有记录的

需要两个输入参数:
@CODE
@NAME
create proc uspSelcte
@code int,
@name varchar(20)
as
begin
select *
from student
where code=@code or name=@name
end
--------------------
使用方法:
EXEC uspSelcte '1',''
或者
EXEC uspSelcte '','张三'

CREATE PROCEDURE pp_myPro
@colname nvarchar(50),@colvalue nvarchar(50)
AS
BEGIN
declare @sql nvarchar(max)
set @sql='select * from [student] where '+@colname+'='''+@colvalue+''''
exec(@sql)
END

create proc procdemo

@colname varchar(100),
@colvalue varchar(200)
as
select * from students where @colname=@colvalue
go

我支持逐心追月的回答,或者可以把or改成and