日期:2014-05-18  浏览次数:20410 次

不确定查询
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 
go
if object_id('selectByMCOrSCCJ') is not null
    drop proc selectByMCOrSCCJ
go
create 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'

go
create proc selectByMCOrSCCJ
     @MC varchar(30)='',
     @SCCJ varchar(30)=''
AS
declare @sql nvarchar(1000)
if @MC<>'' and @SCCJ<>''
     set @sql=N'select MC,SCCJ from SBLX where MC='''+@MC+N''' and SCCJ='''+@SCCJ+N''''
else if @MC<>'' and @SCCJ=''
     set @sql=N'select MC,SCCJ from SBLX where MC='''+@MC+N''''
else
     set @sql=N'select MC,SCCJ from SBLX where SCCJ='''+@SCCJ+N''''
--print @sql
exec sp_executesql @sql
go

exec selectByMCOrSCCJ 'china',''

drop proc selectByMCOrSCCJ
drop table SBLX