当前位置: 代码迷 >> Sql Server >> 不确定查询解决方法
  详细解决方案

不确定查询解决方法

热度:170   发布时间:2016-04-27 19:33:45.0
不确定查询
SQLServer2000数据库
假设一个表SBLX
包含字段MC,SCCJ
建立如下存储过程
CREATE procedure SBLX_selectByMCOrSCCJ
@MC varchar(30),
@SCCJ varchar(30) 
as
select ID,MC,SCCJ,BZ
from SBLX
where MC = @MC 
or SCCJ = @SCCJ
GO

运行存储过程
SBLX_selectByMCOrSCCJ ‘aa’,‘bb’
会显示字段MC下包含aa,或者字段下SCCJ包含bb的信息
但是如果运行SBLX_selectByMCOrSCCJ 'aa',''
会显示全部的信息

现在我想
如果运行SBLX_selectByMCOrSCCJ 'aa',''
只显示字段MC下包含aa的信息


这样的存储过程该怎么写啊 ??
不要分两个存储过程


------解决方案--------------------
SQL code
if object_id('SBLX ') is not null    drop table SBLX goif object_id('selectByMCOrSCCJ') is not null    drop proc selectByMCOrSCCJgocreate table SBLX(MC varchar(30),SCCJ varchar(30))insert SBLX select 'china','guangdong'union all select 'china','beijing'union all select 'usa','newyork'union all select 'france','paris'gocreate proc selectByMCOrSCCJ     @MC varchar(30)='',     @SCCJ varchar(30)=''ASdeclare @sql nvarchar(1000)if @MC<>'' and @SCCJ<>''     set @sql=N'select MC,SCCJ from SBLX where [email protected]+N''' and [email protected]+N''''else if @MC<>'' and @SCCJ=''     set @sql=N'select MC,SCCJ from SBLX where [email protected]+N''''else     set @sql=N'select MC,SCCJ from SBLX where [email protected]+N''''--print @sqlexec sp_executesql @sqlgoexec selectByMCOrSCCJ 'china',''drop proc selectByMCOrSCCJdrop table SBLX
  相关解决方案