关于存储过程的问题
-- =============================================-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: 根据条件查询信息
-- =============================================
ALTER PROCEDURE [dbo].[selectUsers]
-- Add the parameters for the stored procedure here
@Id int='',
@NickName varchar(50)='',
@Sex varchar(50)='',
@Age varchar(50)=''
AS
declare @strsql varchar(500)
select @strsql='select Id as QQ号码,NickName as 昵称,Sex as 性别,Age as 年龄 from Users where 1=1 '
BEGIN
SET NOCOUNT ON;
-- Insert statements for procedure here
if @Id<>''
select @strsql='select Id as QQ号码,NickName as 昵称,Sex as 性别,Age as 年龄 from Users where 1=1 and Id='+@Id+''
if @NickName<>''
select @strsql=@strsql+'and NickName like'''+@NickName+''''
if @Sex<>''
select @strsql
=case
when @Sex='不限' then @strsql
else @strsql+'and Sex like''%'+@Sex+'%'''
end
if @Age<>''
select @strsql
=case
when @Age='不限' then @strsql
when @Age='0-15岁' then @strsql+'and Age between 0 and 15'
when @Age='16-22岁' then @strsql+'and Age between 16 and 22'
when @Age='23-30岁' then @strsql+'and Age between 23 and 30'
when @Age='31-40岁' then @strsql+'and Age between 31 and 40'
when @Age='41-无限' then @strsql+'and Age >41'
end
exec(@strsql)
END
cunchuguocheng.rar
(5.42 KB)