存储过程中,根据条件动态查询数据

1230阅读 0评论2013-02-25 lwyyuan
分类:C#/.net

USE [MuseumDB]
GO
/****** 对象:  StoredProcedure [dbo].[GetOperBySelect_sp]    脚本日期: 02/25/2013 14:20:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO




CREATE PROCEDURE [dbo].[GetOperBySelect_sp]


@OperatorName varchar(50),
@RoleID int


AS
BEGIN
declare @sql nvarchar(1000)
set @sql = 'select OperatorID,OperatorName,OperatorPwd,o.RoleID,r.RoleName,o.memo from tb_operator o left join tb_role r on o.RoleID = r.RoleID where 1= 1'


if(isnull( @OperatorName,'') <> '')
set @sql = @sql + ' and OperatorName like ''%'+ @OperatorName+'%'''


if( isnull( @RoleID,0) <> 0)
set @sql = @sql + ' and o.RoleID =  '+  @RoleID + ''


exec(@sql)
END

上一篇:sql查询语句中遇到问题
下一篇:asp.net中生成验证图片方法