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的信息
这样的存储过程该怎么写啊 ??
不要分两个存储过程

解决方案 »

  1.   

    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
    goexec selectByMCOrSCCJ 'china',''drop proc selectByMCOrSCCJ
    drop table SBLX