這是一個不錯的例子,特轉載分享之。
------------表中的欄位---------------
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