SQL Server multi-condition Query [Stored Procedure] classic example
Source: Internet
Author: User
★First of all, I would like to thank the Netizens [from the SQL server technology space group] for their simple support...
Note the difference between null and ''.
Select * from table where fdate> comparison between '2007-6-7' or between time ..........
------------ Table field ---------------
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,
[Fj'if] [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
---------- Stored Procedure -------------------------
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 = 'male'
Set @ wom = 'female'
Set @ is = 'yes'
Set @ No = '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 + ''''
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.