今天遇到个问题:有个模糊查询,但是同时可能有多个条件,当然其中有的条件可能是空的.
开始的时候很自以为是的直接将几个查询条件用and连接了起来,结果在程序中一调用,根本查不到结果...
于是开始想到了空值的问题.
如果有的条件为空,怎么办呢?如何修改最终的查询语句?
1.对条件进行空值判断;
2.进行查询语句的字符串拼接.
基本信息表结构如下:
create
table
T_INFOCODE_NATURAL
(
CDATE datetime not null , -- 出厂时间
FAC_NAME varchar ( 50 ) not null , -- 发货厂家
BATTERY_TYPE varchar ( 20 ), -- 电池型号
SEND_TYPE varchar ( 20 ), -- 发货类型
SEND_NUM int , -- 检测数量
MAKE_MODE varchar ( 20 ), -- 制作方式
PRODUCE_MODE varchar ( 20 ), -- 生产方式
BATTERY_NUM int , -- 电池组数量
[ YEAR ] char ( 4 ), -- 年
[ MONTH ] char ( 2 ), -- 月
TDATE datetime , -- 日期
SEND_RULE varchar ( 50 ), -- 发货规则
SEND_CODE varchar ( 12 ), -- 发货组编号
BATTERY_SORT varchar ( 20 ), -- 电池类别(新添加)
PAGENUM varchar ( 12 ) not null , -- 条码号
FITTING_CODE varchar ( 12 ), -- 装配号
LINE_CODE varchar ( 12 ), -- 生产机号
FLAG char ( 1 ), -- 是否放行
REMARK varchar ( 200 ), -- 备注
OTHER varchar ( 50 ), -- 其他
BLANK_COL varchar ( 50 ), -- 空白列
)
(
CDATE datetime not null , -- 出厂时间
FAC_NAME varchar ( 50 ) not null , -- 发货厂家
BATTERY_TYPE varchar ( 20 ), -- 电池型号
SEND_TYPE varchar ( 20 ), -- 发货类型
SEND_NUM int , -- 检测数量
MAKE_MODE varchar ( 20 ), -- 制作方式
PRODUCE_MODE varchar ( 20 ), -- 生产方式
BATTERY_NUM int , -- 电池组数量
[ YEAR ] char ( 4 ), -- 年
[ MONTH ] char ( 2 ), -- 月
TDATE datetime , -- 日期
SEND_RULE varchar ( 50 ), -- 发货规则
SEND_CODE varchar ( 12 ), -- 发货组编号
BATTERY_SORT varchar ( 20 ), -- 电池类别(新添加)
PAGENUM varchar ( 12 ) not null , -- 条码号
FITTING_CODE varchar ( 12 ), -- 装配号
LINE_CODE varchar ( 12 ), -- 生产机号
FLAG char ( 1 ), -- 是否放行
REMARK varchar ( 200 ), -- 备注
OTHER varchar ( 50 ), -- 其他
BLANK_COL varchar ( 50 ), -- 空白列
)
存储过程语句:
-- 存储过程,根据CDATE,FAC_NAME,BATTERY_TYPE,MAKE_MODE,PRODUCE_MODE,
-- YEAR,MONTH,TDATE,PAGENUM几个字段来查询
create proc pr_select_Natural
(
@CDATE datetime,
@FAC_NAME varchar( 50 ),
@BATTERY_TYPE varchar( 20 ),
@MAKE_MODE varchar( 20 ),
@PRODUCE_MODE varchar( 20 ),
@YEAR char ( 4 ),
@MONTH char ( 2 ),
@TDATE datetime,
@PAGENUM varchar( 12 )
)
as
declare @strCDATE varchar( 100 )
declare @strFAC_NAME varchar( 100 )
declare @strBATTERY_TYPE varchar( 100 )
declare @strMAKE_MODE varchar( 100 )
declare @strPRODUCE_MODE varchar( 100 )
declare @strYEAR varchar( 100 )
declare @strMONTH varchar( 100 )
declare @strTDATE varchar( 100 )
declare @strPAGENUM varchar( 100 )
declare @strMAIN varchar( 500 )
set @strCDATE = ' CDATE = @CDATE and '
set @strFAC_NAME = ' FAC_NAME like '' % '' +@FAC_NAME+ '' % '' and '
set @strBATTERY_TYPE = ' BATTERY_TYPE like '' % '' +@BATTERY_TYPE+ '' % '' and '
set @strMAKE_MODE = ' MAKE_MODE like '' % '' +@MAKE_MODE+ '' % '' and '
set @strPRODUCE_MODE = ' PRODUCE_MODE like '' % '' +@PRODUCE_MODE+ '' % '' and '
set @strYEAR = ' YEAR like '' % '' +@YEAR+ '' % '' and '
set @strMONTH = ' MONTH like '' % '' +@MONTH+ '' % '' and '
set @strTDATE = ' TDATE = @TDATE and '
set @strPAGENUM = ' PAGENUM like '' % '' +@PAGENUM+ '' % '' and '
if ( @CDATE = null ) set @strCDATE = ''
if ( @FAC_NAME = null ) set @strFAC_NAME = ''
if ( @BATTERY_TYPE = null ) set @strBATTERY_TYPE = ''
if ( @MAKE_MODE = null ) set @strMAKE_MODE = ''
if ( @PRODUCE_MODE = null ) set @strPRODUCE_MODE = ''
if ( @YEAR = null ) set @strYEAR = ''
if ( @MONTH = null ) set @strMONTH = ''
if ( @TDATE = null ) set @strTDATE = ''
if ( @PAGENUM = null ) set @strPAGENUM = ''
set @strMAIN = ' select
CDATE as 出厂时间,
FAC_NAME as 发货厂家,
BATTERY_TYPE as 电池型号,
SEND_TYPE as 发货类型,
SEND_NUM as 检测数量,
MAKE_MODE as 制作方式,
PRODUCE_MODE as 生产方式,
BATTERY_NUM as 电池组数量,
[YEAR] as 年,
[MONTH] as 月,
TDATE as 日期,
SEND_RULE as 发货规则,
SEND_CODE as 发货组编号,
BATTERY_SORT as 电池类别,
PAGENUM as 条码号,
FITTING_CODE as 装配号,
LINE_CODE as 生产机号,
FLAG as 是否放行,
REMARK as 备注,
OTHER as 其他,
BLANK_COL as 空白列
from T_INFOCODE_NATURAL
where ' + @strCDATE + @strFAC_NAME + @strBATTERY_TYPE + @strMAKE_MODE + @strPRODUCE_MODE
+ @strYEAR + @strMONTH + @strTDATE + @strPAGENUM
exec(@strMAIN)
后又想到更加简短的语句:
create proc pr_select_Natural
(
@CDATE datetime ,
@FAC_NAME varchar ( 50 ),
@BATTERY_TYPE varchar ( 20 ),
@MAKE_MODE varchar ( 20 ),
@PRODUCE_MODE varchar ( 20 ),
@YEAR char ( 4 ),
@MONTH char ( 2 ),
@TDATE datetime ,
@PAGENUM varchar ( 12 )
)
as
declare @strWhere varchar ( 500 )
if ( @CDATE = null )
set @strWhere = @strWhere + ' and CDATE = ' + @CDATE
if ( @FAC_NAME = null )
set @strWhere = @strWhere + ' and FAC_NAME like '' % '' +@FAC_NAME+ '' % '' '
if ( @BATTERY_TYPE = null )
set @strWhere = @strWhere + ' and BATTERY_TYPE like '' % '' +@BATTERY_TYPE+ '' % '''
if ( @MAKE_MODE = null )
set @strWhere = @strWhere + ' and MAKE_MODE like '' % '' +@MAKE_MODE+ '' % '''
if ( @PRODUCE_MODE = null )
set @strWhere = @strWhere + ' and PRODUCE_MODE like '' % '' +@PRODUCE_MODE+ '' % '''
if ( @YEAR = null )
set @strWhere = @strWhere + ' and YEAR like '' % '' +@YEAR+ '' % '''
if ( @MONTH = null )
set @strWhere = @strWhere + ' and MONTH like '' % '' +@MONTH+ '' % '''
if ( @TDATE = null )
set @strWhere = @strWhere + ' and TDATE = @TDATE '
if ( @PAGENUM = null )
set @strWhere = @strWhere + ' and PAGENUM like '' % '' +@PAGENUM+ '' % '''
set @strMAIN = ' select
CDATE as 出厂时间,
FAC_NAME as 发货厂家,
BATTERY_TYPE as 电池型号,
SEND_TYPE as 发货类型,
SEND_NUM as 检测数量,
MAKE_MODE as 制作方式,
PRODUCE_MODE as 生产方式,
BATTERY_NUM as 电池组数量,
[YEAR] as 年,
[MONTH] as 月,
TDATE as 日期,
SEND_RULE as 发货规则,
SEND_CODE as 发货组编号,
BATTERY_SORT as 电池类别,
PAGENUM as 条码号,
FITTING_CODE as 装配号,
LINE_CODE as 生产机号,
FLAG as 是否放行,
REMARK as 备注,
OTHER as 其他,
BLANK_COL as 空白列
from T_INFOCODE_NATURAL
where 1=1 ' + @strWhere
exec ( @strMAIN )
学习路漫漫,继续成长ing.....