34,575
社区成员
发帖
与我相关
我的任务
分享
create proc myproc @tid varchar(50)
as
declare @sql varchar(1000)
begin
set @sql='select * from mytable where tid='''+@tid+''';
exec(@sql)
end
create proc myproc @tid varchar(50)
as
begin
select * from mytable where tid=@tid;
end
create proc myproc
@strwhere varchar(200)
as
declare @sql varchar(1000)
begin
set @sql='select * from mytable where 1=1 '+@strwhere;
exec(@sql)
end
如果查询的条件不固定,就可以动态去追加条件,
比如要查所有的数据,@strwhere=‘’
比如要查性别为男的数据,@strwhere=‘ and sex=1’
主要就是处理一些查询条件不固定的业务,如果查询条件比较固定,还是直接运行语句比较好。