先判断参数的类型,你写的那个不是datetime类型,用encodedate()函数

解决方案 »

  1.   

    我有一个存储过程,参数是datetime类型的,
    参数不要用datetime类型,直接用varchar类型。省了转换。
    execute prc_name '2002-01-02 00:00'
      

  2.   

    我的存储过程如下:
    CREATE proc  prc_zwcl_cs
     
    @shijian datetime
    as  
    set nocount ondeclare @buildsql nvarchar(4000),
            @name varchar(9)-- create new table
    select @name=year(@shijian)
    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[t_zj_' + @name + ']') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
    goto  calculate
    else
    begin
    select @buildsql=N'CREATE TABLE [dbo].[t_zj_' + @name +'] (
    [lsh] [numeric](18, 0) IDENTITY (1, 1) NOT NULL ,
    [period] [datetime] NOT NULL ,
    [kpbh] [numeric](18, 0) NOT NULL ,
    [gdzcbh] [char] (11) COLLATE Chinese_PRC_CI_AS NOT NULL ,
    [gdzcmc] [char] (40) COLLATE Chinese_PRC_CI_AS NULL ,
    [yz] [numeric](18, 0) NULL ,
    [byzj] [numeric](18, 0) NULL ,
    [ljzj] [numeric](18, 0) NULL ,
    [jcz] [numeric](18, 0) NULL ,
    [zjl] [numeric](18, 0) NULL 
    ) ON [PRIMARY]'
    execute sp_executesql @buildsql
    goto calculate
    endcalculate:
    update t_gdzc_main
       set yzjl=(1-jczl)/(synx*12),
           yzje = 0 
    update t_gdzc_main
       set yzjl=(1-jczl)/(synx*12),
           yzje= yz*(1-jczl)/(synx*12),
           ljzj=ljzj + yz*(1-jczl)/(synx*12),
           jz=yz-ljzj,
           jcz=yz*jczl,
           ytzjyf=ytzjyf + 1
           where (year(kssyrq)<year(@shijian) or 
            (year(kssyrq)=year(@shijian) and month(kssyrq)< month(@shijian) ) )
            and ( (act_time is null )  or year(act_time)>year(@shijian)  or 
            (year(act_time)=year(@shijian) and month(act_time)>month(@shijian) ) );
    select @buildsql=N'insert into t_zj_'+ @name+'(period,kpbh,gdzcbh,gdzcmc,yz,byzj,ljzj,jcz,zjl)
    select '+@shijian+',kpbh,gdzcbh,gdzcmc,yz,yzje,ljzj,jcz,yzjl from t_gdzc_main
      where yzje>0'execute sp_executesql @buildsql
    GO
      

  3.   

    declare @date datetime
    set @date=convert(datetime,'2002-01-02')
    execute prc_name @date
      

  4.   

    cast('2002-01-02 00:00' as datetime)
      

  5.   

    建议参数不要用datetime类型,用varchar类型。
      

  6.   

    LoveSQL(ligg) 你说的是在sql中,我用到pb中就不能用了,提示我类型出错。
      

  7.   

    --不需要使用convert,去掉就行了.数据类型会自动转换的.execute prc_name '2002-01-02 00:00'
      

  8.   

    不要使用转还,sql会对第一次使用的数据自动转换
      

  9.   

    execute prc_name convert(datetime,'2002-01-02 00:00',20)20是按照中国的习惯取值方法。
      

  10.   


        不是“不需要使用convert转换”,而是不能这样使用。    因为你在调用时,输入的参数如果是一个计算表达式,不是常量的话,是在前台的语言中进行计算的,也就是说,如果你前台用的VB,那么convert(datetime,'2002-01-02 00:00')这个计算式要在VB中进行计算,而VB是没有convert函数的,所以会出错。
      

  11.   

    传日期到sql中,还是使用 varchar类型比较好处理 ,在需要时,sql中可以随时进行转换。
      

  12.   

    SQLSERVER会隐式进行字符到日期的转换,只要你输入的字符串式符合日期的格式。