declare @begin_date int,@end_date int
select @begin_date = 20090101,@end_date = 20090619
select b.date_id
from table1 as a
join table2 as b
  on a.id= b.id
where (b.date_id between @begin_date and @end_date )执行的时候会报错:当前命令发生了严重错误。应放弃任何可能产生的结果
如果改成
declare @begin_date int,@end_date int
select @begin_date = 20090101,@end_date = 20090619
select b.date_id
from table1 as a
join table2 as b
  on a.id= b.id
where (b.date_id between @begin_date and 20090619)就执行正常了 请教是什么原因???急!

解决方案 »

  1.   

    select @begin_date = 20090101,@end_date = 20090619 
    改成select @begin_date = '20090101',@end_date = '20090619' 
    试试
      

  2.   

    看定义的变量的数据类型啊到底应该是int 还是varchar啊
      

  3.   

    declare @begin_date int,@end_date int 
    set @begin_date = 20090101
    set @end_date = 20090619 
    select b.date_id 
    from table1 as a 
    join table2 as b 
      on a.id= b.id 
    where (b.date_id between @begin_date and @end_date) 
      

  4.   

    有没有在sql查询的时候遇到过 
    如果用变量查询报错  把这个变量替换成某个常量后 就没有问题了这个应该也是这样的问题 
    不知道是什么原因引起的 以及该如何解决
      

  5.   

    declare @begin_date int,@end_date int 
    select @begin_date = 20090101,@end_date = 20090619 
    select b.date_id 
    from table1 as a 
    join table2 as b 
      on a.id= b.id 
    where (b.date_id between @begin_date and @end_date ) where 后面不要括号试试
      

  6.   

    declare @begin_date int,@end_date int 
    select @begin_date = 20090101
    select @end_date = 20090619 
    select b.date_id 
    from table1 as a 
    join table2 as b 
      on a.id= b.id 
    where b.date_id between @begin_date and @end_date 个人感觉 不会错~也许版本问题 
      

  7.   

    DECLARE @T1 TABLE(id int,name char)
    insert @T1
    SELECT 1,'t' UNION ALL
    SELECT 2,'r' UNION ALL 
    SELECT 3,'c'DECLARE @T2 TABLE(id int,date_id int)
    insert @T2
    SELECT 1,20090101 UNION ALL
    SELECT 3,20080101 UNION ALL 
    SELECT 2,20091101 UNION ALL
    SELECT 3,20090701 UNION ALL 
    SELECT 6,20090101 UNION ALL 
    SELECT 2,20090201--经测试,这种表达没问题
    declare @begin_date int,@end_date int 
    select @begin_date = 20090101,@end_date = 20090619 select b.date_id 
    from @T1 as a 
    join @T2 as b 
      on a.id= b.id 
    where (b.date_id between @begin_date and @end_date )
    /* 
    date_id
    -----------
    20090101
    20090201(2 行受影响)*/
    ----------------------------------------------
    ----------------------------------------------
    declare @begin_date int,@end_date int 
    select @begin_date = 20090101,@end_date = 20090619 select b.date_id 
    from @T1 as a 
    join @T2 as b 
      on a.id= b.id 
    where (b.date_id between @begin_date and 20090619) 
    /*
    date_id
    -----------
    20090101
    20090201(2 行受影响)*/
      

  8.   

    我喜欢这样写declare @begin_date int,@end_date int 
    set @begin_date=20090101
    set @end_date=20090619 
    select b.date_id from table1 a join table2 b on a.id= b.id 
    where b.date_id between @begin_date and @end_date
      

  9.   

    date_id
    在数据库表中是什么类型,是字符型吗?
    为何不加引号呢?
      

  10.   

    declare @begin_date int,@end_date int 
    set @begin_date=20090101
    set @end_date=20090619 
    select b.date_id from table1 a join table2 b on a.id= b.id 
    where b.date_id between @begin_date and @end_date
      

  11.   

    declare @begin_date int,@end_date int 
    set @begin_date=20090101
    set @end_date=20090619 
    select b.date_id from table1 a join table2 b on a.id= b.id 
    where b.date_id between @begin_date and @end_date个人觉得这样没问题
      

  12.   

    SQL code  如何才能发类似的帖子,。求指教~是不是直接从2005查询分析器复制过来的?
      

  13.   


    要错都错。要对都对呀。不可能一句错,一句对。
    如果20090619 加了引号,楼主那句也是对的。楼主那一句改成这样就OK了。
    between cast(@begin_date as varchar) and cast(@end_date  as varchar)