SqlServer 多條件查詢 [預存程序] 經典例子

來源:互聯網
上載者:User
★首先要感謝 網友[來自Sql Server 技術空間群]簡單愛的大力支援...
 需要注意的問題 NULL,  '      ' 的區別
 SELECT *FROM  表 WHERE  Fdate > ' 2007-6-7'  字元之間的比較,還是時間之間的比較..........
------------表中的欄位---------------
CREATE TABLE [dbo].[stuInfo] (
 [FNumber] [int] IDENTITY(1,1) NOT NULL ,
 [FName] [nvarchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [FNameen] [varchar] (35) COLLATE Chinese_PRC_CI_AS  ,
 [FSex] [char] (1) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [FEducation] [char] (1) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [FCardID] [char] (18) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [FBirDate] [datetime] NOT NULL ,
 [FTech] [nvarchar](50) COLLATE Chinese_PRC_CI_AS ,
 [FSubject] [nvarchar](50) COLLATE Chinese_PRC_CI_AS,
 [FJiF] [nvarchar](100)COLLATE Chinese_PRC_CI_AS,
 [FJobAdd][nvarchar](100)COLLATE Chinese_PRC_CI_AS,
 [FExamNum][varchar](30)COLLATE Chinese_PRC_CI_AS NOT NULL,
 [FServerNum] [varchar] (30) COLLATE Chinese_PRC_CI_AS NOT NULL ,
 [FExamDate] [datetime] NOT NULL ,
 [FIsAE] [char] (1) COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY]
GO
----------預存程序-------------------------
IF EXISTS(SELECT *FROM SYSOBJECTS WHERE NAME='PROC_Look')
DROP PROC  PROC_Look
GO
CREATE PROC PROC_Look
 @FName  NVARCHAR(30)=null,
 @FNameen VARCHAR(35)=null,
 @FCardID CHAR(18)=null,
 @FExamNum NVARCHAR(30)=null,
 @FServerNum NVARCHAR(30)=null,
 @FSex  CHAR(1)=null,
 @FEducation NVARCHAR(20)=null,
 @FIsAE  CHAR(1)=null,
 @FTech  NVARCHAR(50)=null,
 @FSubject NVARCHAR(50)=null,
 @FJiF  NVARCHAR(100)=null,
 @FJobAdd NVARCHAR(100)=null,
 @FStartTime VARCHAR(20)=null,
 @FEndTime VARCHAR(20)=null
 
AS
 declare @sqlStr varchar(500)
        declare @man char(2)
        declare @wom char(2)
        declare @is char(2)
        declare @no char(2)
        set  @man ='男'
        set  @wom ='女'
        set  @is ='是'
        set  @no ='否' if  @FName <>''
 begin
   set @sqlStr=' where FName='+''''+@FName+''''
 end
       
 if @FNameen <>''
 begin
    if @sqlStr <>''
        set @sqlStr=@sqlStr+' and FNameen='+''''+@FNameen+''''
    else
   set @sqlStr=' where FNameen='+''''+@FNameen+''''
 end
   
 if @FCardID <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FCardID='+''''+@FCardID+''''
  else
   set @sqlStr=' where FCardID='+''''+@FCardID+''''
 end
 
 if @FExamNum <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FExamNum='+''''+@FExamNum+''''
  else
   set @sqlStr=' where FExamNum='+''''+@FExamNum +''''
 end
 
 
 if @FServerNum <>''
  begin
   if @sqlStr <>''
    set @sqlStr=@sqlStr+' and FServerNum='+''''+@FServerNum+''''
   else
    set @sqlStr=' where FServerNum='+''''+@FServerNum+''''
  end if @FSex <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FSex='+''''+@FSex+''''
  else
   set @sqlStr=' where FSex='+''''+@FSex+''''
 end if @FEducation <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FEducation='+''''+@FEducation+''''
                       
                else
   set @sqlStr=' where FEducation='+''''+@FEducation+''''
  
 end if @FIsAE <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FIsAE= '+''''+@FIsAE+''''
                       
  else
   set @sqlStr=' where FIsAE= '+''''+@FIsAE+''''
                       
 end if @FTech <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FTech='+''''+@FTech+''''
  else
   set @sqlStr=' where FTech='+''''+@FTech+''''
 end
 
 if @FSubject <>''
   begin
    if @sqlStr <>''
     set @sqlStr=@sqlStr+' and FSubject='+''''+@FSubject+''''
    else
     set @sqlStr=' where FSubject='+''''+@FSubject +''''
   end
 
 if @FJiF <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FJiF='+''''+@FJiF+''''
  else
   set @sqlStr=' where FJiF='+''''+@FJiF+''''
 end if @FJobAdd <>''
 begin
  if @sqlStr <>''
   set @sqlStr=@sqlStr+' and FJobAdd='+''''+@FJobAdd +''''
  else
   set @sqlStr=' where FJobAdd='+''''+@FJobAdd +''''
 end
 
 if @FStartTime <>''
                begin
 
  if @sqlStr <>''
                      
                        set @sqlStr=@sqlStr+' and FExamDate>= '+''''+@FStartTime+''''  else
   set @sqlStr=' where FExamDate>= '+''''+@FStartTime+''''
   end
 
 if @FEndTime <>''
   begin
    if @sqlStr <>''
     set @sqlStr=@sqlStr+' and FExamDate<= '+''''+@FEndTime+''''
    else
     set @sqlStr=' where FExamDate<= '+''''+@FEndTime+''''
   end     
       EXEC('select FNumber,FName,FNameen,FSex=CASE WHEN FSex=0 THEN ' +''''+@man+''''+ ' ELSE '+''''+ @wom+''''+ ' END,FEducation,FCardID,FBirDate,FTech,FSubject,FJiF,FJobAdd,FExamNum,FServerNum,FExamDate,FIsAE=CASE WHEN FIsAE=0 THEN'+ ''''+@is+''''+' ELSE '+''''+@no+''''+' END from stuInfo'+@sqlStr)GO
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.