CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  基础类

小弟存储过程问题

楼主boboox(伟人西门庆)2004-09-04 09:39:38 在 MS-SQL Server / 基础类 提问

有两个问题  
  1     第一个条件if   @diskid   is   not   null,赋值的话正常,不赋值的话变为0。我要的效果是忽略这个条件  
  2     时间参数如何搜索呢?  
   
   
   
  if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[pt_disksearch]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)  
  drop   procedure   [dbo].[pt_disksearch]  
  GO  
   
  SET   QUOTED_IDENTIFIER   OFF    
  GO  
  SET   ANSI_NULLS   OFF    
  GO  
   
  CREATE   PROCEDURE   pt_disksearch    
  @diskid   int=null,  
  @writedate   datetime=null,  
  @detail   varchar(5000)=null,  
  @disktype   varchar(50)=null,  
  @gametype   varchar(50)=null,  
  @singer   varchar(500)=null,  
  @starring   varchar(500)=null,  
  @movietype   varchar(100)=null,  
  @moviecountry   varchar(100)=null  
    AS  
  declare   @sql   as   varchar(8000)  
  set   @sql=''  
  if   @diskid   is   not   null  
  begin  
  set   @sql=@sql+'and   diskid='+cast(@diskid   as   varchar(20))+''  
  end  
  if   @writedate   is   not   null  
  begin  
  set   @sql=@sql+'and   writedate='''+cast(@writedate   as   varchar(20))+''''  
  end  
  if   @detail   is   not   null  
  begin  
  set   @sql=@sql+'and   detail   like   ''%'''+@detail+'''%'''  
  end  
  if   @disktype   is   not   null  
  begin  
  set   @sql=@sql+'   and   disktype='''+cast(@disktype     as   varchar(20))+''''  
  end  
  if   @gametype   is   not   null  
  begin  
  set   @sql=@sql+'and   gametype='''+@gametype+''''  
  end  
  if   @gs   is   not   null  
  begin  
  set   @sql=@sql+'and   gs='''+@gs+''''  
  end  
  if   @yy   is   not   null  
  begin  
  set   @sql=@sql+'and   yy='''+   @yy+''''  
  end  
  if   @movietype   is   not   null  
  begin  
  set   @sql=@sql+'and   movietype='''+   @movietype+''''  
  end  
  if   @moviecountry   is   not   null  
  begin  
  set   @sql=@sql+'and   moviecountry='''+   @moviecountry+''''  
  end  
  if   @sql<>''  
  begin  
  print   @sql  
  exec(@sql)  
  end  
  GO  
  SET   QUOTED_IDENTIFIER   OFF    
  GO  
  SET   ANSI_NULLS   ON    
  GO  
   
   
  问题点数:0、回复次数:4Top

1 楼James_ht(顺风飞扬)回复于 2004-09-04 09:45:29 得分 0

@diskid   int   =0Top

2 楼haoK(haoK.Y)回复于 2004-09-04 09:56:00 得分 0

1  
  这样使用@diskid  
  ISNULL(@diskid,   0)  
   
  或者在开头先处理一下:SET   @diskid   =   ISNULL(@diskid,   0)  
   
  2  
  时间可以直接用>   <   =比较,between也可  
  CONVERT(CHAR(10),   @datetime,   120)转换成'2004-09-04'形式,  
  别忘了前后加引号  
  Top

3 楼boboox(伟人西门庆)回复于 2004-09-12 16:55:21 得分 0

@diskid的值这样处理后还是0啊,还是要查找出diskid=0的记录。  
  我要的效果是完全忽略这个条件。请赐教。。。Top

4 楼zjcxc(邹建)回复于 2004-09-12 17:23:25 得分 0

if   exists   (select   *   from   dbo.sysobjects   where   id   =   object_id(N'[dbo].[pt_disksearch]')   and   OBJECTPROPERTY(id,   N'IsProcedure')   =   1)  
  drop   procedure   [dbo].[pt_disksearch]  
  GO  
   
  SET   QUOTED_IDENTIFIER   OFF    
  GO  
  SET   ANSI_NULLS   OFF    
  GO  
   
  CREATE   PROCEDURE   pt_disksearch    
  @diskid   int=null,  
  @writedate   datetime=null,  
  @detail   varchar(5000)=null,  
  @disktype   varchar(50)=null,  
  @gametype   varchar(50)=null,  
  @singer   varchar(500)=null,  
  @starring   varchar(500)=null,  
  @movietype   varchar(100)=null,  
  @moviecountry   varchar(100)=null  
    AS  
  declare   @sql   as   Nvarchar(4000)   --改为类型  
  select   @sql  
  =case    
  when   @diskid   is   null   then   ''  
  else   '   and   diskid=@diskid'   end  
  +case  
  when   @writedate   is   null   then   ''  
  else   '   and   writedate=@writedate'   end  
  +case  
  when   @detail   is   null   then   ''  
  else   '   and   detail   like   ''%''+@detail+''%'''   end  
  +case    
  when   @disktype   is   null   then   ''  
  else   '   and   disktype=@disktype'   end  
  +case  
  when   @gametype   is   null   then   ''  
  else   '   and   gametype=@gametype'   end  
  /*--这里的变量没有定义,不知道是否楼主搞错了  
  +case  
  when   @gs   is   null   then   ''  
  else   '   and   gs=@gs'   end  
  +case  
  when   @yy   is   null   then   ''  
  else   '   and   yy=@yy'   end  
  --*/  
  +case  
  when   @movietype   is   null   then   ''  
  else   'and   movietype=@movietype'   end  
  +case  
  when   @moviecountry   is   null   then   ''  
  else   '   and   moviecountry=@moviecountry'   end  
  ,@sql='select   *   from   表'     --至少有查询语句  
  +case   @sql   when   ''   then   ''  
  else   '   where   '+stuff(@sql,1,5,'')   end  
  exec   sp_executesql   @sql  
  ,N'@diskid   int=null, --存储过程中定义的参数帖一遍  
  @writedate   datetime=null,  
  @detail   varchar(5000)=null,  
  @disktype   varchar(50)=null,  
  @gametype   varchar(50)=null,  
  @singer   varchar(500)=null,  
  @starring   varchar(500)=null,  
  @movietype   varchar(100)=null,  
  @moviecountry   varchar(100)=null'  
  ,@diskid,@writedate,@detail,@disktype,@gametype,@singer,@starring,@movietype,@moviecountry  
  GO  
  SET   QUOTED_IDENTIFIER   OFF    
  GO  
  SET   ANSI_NULLS   ON    
  GO  
  Top

相关问题

  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程
  • 存储过程

关键词

  • sql
  • diskid
  • disktype
  • writedate
  • disksearch
  • goset
  • varchar
  • detail
  • dbo
  • cast

得分解答快速导航

  • 帖主:boboox

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo