编写存储过程:
查询点播资源,要求能按照资源类别名称、资源编号、资源名称、上载日期(区间)查询;
我的解答: CREATE PROCEDURE uer_select_v_sources @type_name varchar(15)='%',@source_id char(9)='%',@source_name varchar(50)='%',@up_date1 datetime='%',@up_date2  datetime='%'
ASselect a.source_id as 资源编号,a.source_name as 资源名称,a.source_price as 价格,a.up_date as 上载日期,b.type_name as 资源类别
from tsource as a,ttype as b
where a.type_id=b.type_id or b.type_name=@type_name or a.source_id=@source_id or a.source_name=@source_name or  a.up_date between @up_date1  and  @up_date2  
GOexec uer_select_v_sources @type_name='动画片'有动画片这个类型啊,但是这样得不出结果,出现了‘服务器: 消息 241,级别 16,状态 1,过程 uer_select_v_sources,行 0
从字符串转换为 datetime 时发生语法错误。

我没有给@up_date1 datetime='%',@up_date2  datetime='%'这两个变量赋值,为什么还要出现这个语法错误啊

解决方案 »

  1.   

    --这样试试?
    CREATE PROCEDURE uer_select_v_sources 
     @type_name varchar(15)='%',
     @source_id char(9)='%',
     @source_name varchar(50)='%',
     @up_date1 datetime=null,
     @up_date2  datetime=null
    ASif @up_date1 is NULL and @up_date2 is NULL
     select 
      a.source_id as 资源编号,
      a.source_name as 资源名称,
      a.source_price as 价格,
      a.up_date as 上载日期,
      b.[type_name] as 资源类别
     from 
      tsource as a,
      ttype as b
     where 
      a.type_id=b.type_id or 
      b.[type_name]=@type_name or 
      a.source_id=@source_id or 
      a.source_name=@source_name
    else
     select 
      a.source_id as 资源编号,
      a.source_name as 资源名称,
      a.source_price as 价格,
      a.up_date as 上载日期,
      b.[type_name] as 资源类别
     from 
      tsource as a,
      ttype as b
     where 
      a.type_id=b.type_id or 
      b.[type_name]=@type_name or 
      a.source_id=@source_id or 
      a.source_name=@source_name or
      a.up_date between @up_date1 and @up_date2  
    GO
      

  2.   

    exec uer_select_v_sources @type_name='动画片'
    服务器: 消息 241,级别 16,状态 1,过程 uer_select_v_sources,行 0
    从字符串转换为 datetime 时发生语法错误。
      

  3.   

    --没有测试数据,随便写了几句
    create table tsource(type_id varchar(10),source_id varchar(10),source_name varchar(10),source_price int,up_date  datetime)
    insert into  tsource select '1','1','aa',11,'2004-01-01'
    insert into  tsource select '2','2','bb',22,'2005-01-01'create table ttype([type_name] varchar(10),type_id varchar(10))
    insert into ttype select '动画片','1'
    insert into ttype select 'hhhh','2'
    goCREATE PROCEDURE uer_select_v_sources 
     @type_name varchar(15)='%',
     @source_id char(9)='%',
     @source_name varchar(50)='%',
     @up_date1 datetime=null,
     @up_date2  datetime=null
    ASif @up_date1 is NULL and @up_date2 is NULL
     select 
      a.source_id as 资源编号,
      a.source_name as 资源名称,
      a.source_price as 价格,
      a.up_date as 上载日期,
      b.[type_name] as 资源类别
     from 
      tsource as a,
      ttype as b
     where 
      a.type_id=b.type_id and
      (b.[type_name]=@type_name or 
      a.source_id=@source_id or 
      a.source_name=@source_name)
    else
     select 
      a.source_id as 资源编号,
      a.source_name as 资源名称,
      a.source_price as 价格,
      a.up_date as 上载日期,
      b.[type_name] as 资源类别
     from 
      tsource as a,
      ttype as b
     where 
      a.type_id=b.type_id and
      (b.[type_name] like @type_name or
      a.source_id=@source_id or
      a.source_name like @source_name or
      a.up_date between @up_date1 and @up_date2)
    GOexec uer_select_v_sources @type_name='动画片'exec uer_select_v_sources @type_name='动画片',@up_date1='2004-01-01',@up_date2='2005-01-01'
    drop proc uer_select_v_sources
    drop table tsource,ttype
      

  4.   

    同意一楼楼主可能没改参数定义的地方CREATE PROCEDURE uer_select_v_sources 
     @type_name varchar(15)='%',
     @source_id char(9)='%',
     @source_name varchar(50)='%',
     @up_date1 datetime=null,                    ----改了
     @up_date2  datetime=null                    ----改了
    AS
      

  5.   

    谢谢,现在得到结果了,我上次改成null就没有用的啊,而且刷新过好几次啊,像这种情况是不是要新停止服务重新启动才可以正确的反应啊!