读书人

不确定查询解决方法

发布时间: 2012-02-09 18:22:27 作者: rapoo

不确定查询
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 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 @sqlexec sp_executesql @sqlgoexec selectByMCOrSCCJ 'china',''drop proc selectByMCOrSCCJdrop table SBLX 

读书人网 >SQL Server

热点推荐