我常要执行这样一类sql语句
select * from tableA  where date='20003-6-30'
union all
select * from tableB where date='20003-6-30'
union all
select * from tableC where date='20003-6-30'
union all
select * from tableD where date='20003-6-30'可不可以将其中的'2003-6-30'设成一个变量代替,这样在执行sql语名前我只需更改变量的值即可
谢谢

解决方案 »

  1.   

    在MFC中有个CTime::CurrentTime()函数可以这么负值
    在嵌入式sql中应该也是可以的
    找找date的格式
      

  2.   

    当然可以了,这是最基本的!
    首先说明你给的那几条语句应该说不完全正确。在SQL查询语句中使用变量有些不同!
    如果是数值开变量!那么直接使用!字符型就是就得加单引号,那么日期型变量就得
    加#,你给的那几条语句如果可以正常查询到你想得到的内容,那么只能说你的那个
    字段是字符型的!而不是真正的日期型!因为日期型查询时必须加#!select * from tableD where date=#20003-6-30#
    例:
    dim SearchDate as date
    SearchDate=  '自己写
    select * from tableD where date=#" & SearchDate & "#"
      

  3.   

    dim sqlcmd,ss as string
    ss=data()
    sqlcmd="select * from table where data=' + ss + '"
      

  4.   

    DECLARE @theDate datetime
    SELECT @theDate = '20003-6-30'select * from tableA  where date = @theDate
    union all
    select * from tableB where date = @theDate
    union all
    select * from tableC where date = @theDate
    union all
    select * from tableD where date = @theDate
      

  5.   

    结了分,发现问题:DECLARE @theDate datetime
    SELECT @theDate = '20003-6-30'select * from orderin   where 入库日期 = @theDate执行后提示:
    服务器: 消息 241,级别 16,状态 1,行 2
    从字符串转换为 datetime 时发生语法错误。所以不得不再发贴请教了
      

  6.   

    对不起,发现错误是我造成的,日期输入成'20003-6-30',应改成 '2003-6-30'但发现变量为字符串时查询不出数据啊,怎么回事?(我知道肯定是数据库中肯定有符合条件的数据)DECLARE @e1 char
    SELECT @e1 = '上海av'
    select * from korder   where 办事处 = @e1执行后返回空集(无数据)
      

  7.   

    自己捉摸出来了,呵呵DECLARE @e1 char(10)
    SELECT @e1 = '上海av'select * from korder   where 办事处 = @e1