这个我知道的我是写@tempdate,不是这个问题

解决方案 »

  1.   

    declare @tempdate datetime,@tempdate1 varchar(256),@s varchar(8000)
    select @tempdate='2004-7-23 12:00:00'
    ,@tempdate1='20040723.txt'set @s='bcp "select * from sms..table_name where datediff(hour,rec_time,'''+@tempdate+''')>0 and  datediff(hour,rec_time,'''+@tempdate+''')<=24 " queryout c:\ftptemp\cjtxt\xdcj'+@tempdate1+'.txt -c -S数据库名 -U用户名 -P密码'
    EXEC master..xp_cmdshell @s
      

  2.   

    --上面的文件名多写了.txt,要去掉--或者(文件名根据指定的日期自动生成):declare @tempdate datetime,@s varchar(8000)
    select @tempdate='2004-7-23 12:00:00'set @s='bcp "select * from sms..table_name where datediff(hour,rec_time,'''+@tempdate+''')>0 and  datediff(hour,rec_time,'''+@tempdate+''')<=24 " queryout c:\ftptemp\cjtxt\xdcj'+convert(varchar,@tempdate,112)+'.txt -c -S数据库名 -U用户名 -P密码'
    EXEC master..xp_cmdshell @s
      

  3.   

    这样才行吧:declare @tempdate datetime,@temps varchar(19),@s varchar(8000)
    select @tempdate='2004-7-23 12:00:00',@temps=convert(varchar,@tempdate,120)set @s='bcp "select * from sms..table_name where datediff(hour,rec_time,'''+@temps+''')>0 and  datediff(hour,rec_time,'''
    +@temps+''')<=24 " queryout c:\ftptemp\cjtxt\xdcj'+convert(varchar,@tempdate,112)+'.txt -c -S数据库名 -U用户名 -P密码'
    EXEC master..xp_cmdshell @s