create  procedure test
  (
   @dtsdate smalldatetime,
   @chra varchar(50)
   )
 as 
declare @sql nvarchar
set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' +  @dtsdate  + ', 102)'exec sp_executesql @sql

解决方案 »

  1.   

    to: xluzhong(打麻将一缺三,咋办?) 
    运行上面的代码,提示这样的错误:服务器: 消息 295,级别 16,状态 3,过程 test,行 8
    [Microsoft][ODBC SQL Server Driver][SQL Server]从字符串转换为 smalldatetime 数据类型时发生语法错误。
      

  2.   


    create  procedure test
      (
       @dtsdate smalldatetime,
       @chra varchar(50)
       )
     as 
    declare @sql nvarchar(4000)
    set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' +  @dtsdate  + ', 102)'
    exec sp_executesql @sql
      

  3.   

    如果这样不行则
    create  procedure test
      (
       @dtsdate smalldatetime,
       @chra varchar(50)
       )
     as 
    declare @sql nvarchar(4000)
    set @sql='select * from table where chra=' + @chra + ' and Date =CONVERT(DATETIME, ' +  convert(nvarchar(30),@dtsdate,112)  + ', 102)'
    exec sp_executesql @sql
      

  4.   

    to: xluzhong(打麻将一缺三,咋办?) 
    改过后提示:
    服务器: 消息 8115,级别 16,状态 2,行 1
    [Microsoft][ODBC SQL Server Driver][SQL Server]将 expression 转换为数据类型 datetime 时发生算术溢出错误。
      

  5.   

    ---test
    declare
       @dtsdate smalldatetime,
       @chra varchar(50),
       @sql nvarchar(4000)
    set @dtsdate=getdate()
    set @chra = 'test'
    set @sql='select * from table where chra=' + @chra + ' and Date = CONVERT(DATETIME,'+convert(nvarchar(30),@dtsdate ,112)+ ',102)'
    print  @sql
    ---result
    select * from table where chra=test and Date = CONVERT(DATETIME,20050123,102)
      

  6.   

    create  procedure test
       @dtsdate smalldatetime,
       @chra varchar(50)
     as 
    exec ('select * from table where chra=''' + @chra + ''' and Date =CONVERT(DATETIME, ' +  @dtsdate  + ', 102)' )
      

  7.   


     访问和更改关系数据  
    转换 datetime 和 smalldatetime 数据
    转换为 datetime 时,Microsoft® SQL Server™ 2000 将拒绝所有无法识别为日期的值(包括 1753 年 1 月 1 日以前的日期)。当日期在适当的范围内(1900 年 1 月 1 日到 2079 年 6 月 6 日)时,可将 datetime 值转换为 smalldatetime。时间值被四舍五入为最接近的分钟数。此示例分别将 smalldatetime 和 datetime 值转换为 varchar 和 binary 数据类型。DECLARE @mydate_sm  SMALLDATETIME
    SET  @mydate_sm  = '4/05/98'SELECT  CAST(@mydate_sm AS VARCHAR) AS SM_DATE_VARCHAR
    GODECLARE @mydate  DATETIME
    SET @mydate     = '4/05/98'SELECT  CAST(@mydate AS BINARY) AS DATE_BINARY
    GO下面是结果集:(1 row(s) affected)SM_DATE_VARCHAR                
    ------------------------------ 
    Apr  5 1998 12:00AM            (1 row(s) affected)DATE_BINARY                                                    
    -------------------------------------------------------------- 
    0x0000000000000000000000000000000000000000000000008c3000000000 (1 row(s) affected)
    请参见CAST 和 CONVERT 数据类型©1988-2000 Microsoft Corporation。保留所有权利。
      

  8.   

    晕,还是不行
    @dtsdate我赋值了,
    set @dtsdate ='2005-01-01 00:00:00'
      

  9.   

    这句我就想从表中筛选出date=@dtsdate的数据,直接写可以,一动态的用字符串的形式写就出错,不知道为什么,试了大半天了,郁闷!!!!
      

  10.   

    我的意思写成下面的查没问题,但是一做成上面的动态查就出错,不知道为什么!!!!create  procedure test
      (
       @dtsdate smalltime,
       @chra varchar(50)
       )
     as 
       declare @dtsdate smalldatetime
    select * from table where chra= @chra and Date =@dtsdate
    GO
      

  11.   

    结果:
    id          dtsdate                                            chra                                               
    ----------- ------------------------------------------------------ -------------------------------------------------- 
    1           2005-01-01 00:00:00                                    aaa
    @RETURN_VALUE = 0
      

  12.   

    字段名:id,dtsdate,chra
    表:table1
    存储过程:test
    create  procedure test
      (
       @dtsdate smalldatetime,
       @chra varchar(50)
       )
     as 
    select * from table1 where chra= @chra and dtsDate =@dtsdate
    ----------------------------------------------------------
     传值:
     @dtsdate='2005-01-01 00:00:00'
     @chra='aaa'
    ---------------------------------------------------
      

  13.   

    create table test0523(id int,dtsdate datetime,chra nvarchar(50))
    insert into test0523 select 1,'2005-1-1','test'
    union all select 2,'2005-1-2','test'
    union all select 3,'2005-1-3','aaa'
    union all select 4,'2005-1-4','bbb'
    declare
    @dtsdate smalldatetime,
        @chra varchar(50),
        @sql nvarchar(4000)set @dtsdate='2005-1-1'
    set @chra = 'test'
    set @sql='select * from test0523 where chra=' +''''+ @chra+''''+ ' and dtsdate = CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120)'
    print @sql
    exec sp_executesql @sql
    drop table test0523
      

  14.   

    可以了^-^
    我想问一下
    set @sql='select * from test0523 where chra=' +''''+ @chra+''''+ ' and dtsdate = CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120)'
    CONVERT(DATETIME,'+''''+convert(nvarchar(30),@dtsdate ,112)+''''+',120) 这部分为什么不能直接写成@dtsdate  呢?@dtsdate它的类型不是已经声名成@dtsdate smalldatetime 这个了么?
      

  15.   

    比较
    declare @test datetimeset @test=2005-1-23
    print @test
    set @test='2005-1-23'
    print @test再看看 datetime和smalldatetime 的范围