这是我第一次写的组合条件查询,很丑,仅此纪念。
(黑体总是显得跌跌撞撞没自信的赶脚有没有~~)
1 USE [exercise] 2 GO 3 /****** Object: StoredProcedure [dbo].[procstudent] Script Date: 11/10/2013 23:09:33 ******/ 4 SET ANSI_NULLS ON 5 GO 6 SET QUOTED_IDENTIFIER ON 7 GO 8 ALTER PROCEDURE [dbo].[procstudent] 9 @s int, 10 @c int 11 12 AS 13 BEGIN 14 SET NOCOUNT ON; 15 declare @sql varchar(300), 16 @sex nchar(10), 17 @class nchar(10) 18 set @sql=‘select number,name,sex,class from table1 where 1=1‘ 19 IF @s=1 20 begin 21 set @sex=‘男‘ 22 end 23 IF @s=2 24 begin 25 set @sex=‘女‘ 26 end 27 28 IF @s<>0 29 begin 30 set @sql=@sql+‘ and sex=‘+‘‘‘‘+@sex+‘‘‘‘ 31 end 32 IF @c=1 33 begin 34 set @class=‘1‘ 35 endm 36 IF @c=2 37 begin 38 set @class=‘2‘ 39 end 40 IF @c<>0 41 begin 42 set @sql=@sql+‘ and class=‘+@class 43 end 44 print @sql 45 exec(@sql) 46 END
原文:http://www.cnblogs.com/coderinprague/p/3785620.html