DECLARE @over_time datetime
DECLARE @card_id intDECLARE @filePath VARCHAR(100)
declare @date VARCHAR(100)
--declare @sql varchar(1000)
set @over_time='2010-09-30'
set @card_id=1
set @date=replace(replace(replace(CONVERT(varchar, @over_time, 120 ),'-',''),' ',''),':','')
SET @filePath = 'd:\过期时间'+@date+'Card_id='+convert(VARCHAR,@card_id)+'.xls'
--set @sql='bcp "select card_id,card_code,card_pass,date_time,over_time,ispay from  game.dbo.stock_data_bak where card_id='+@card_id+' and over_time='+@over_time+'"'
select @filePath
EXEC('
    EXEC master..xp_cmdshell ''bcp "select card_id,card_code,card_pass,date_time,over_time,ispay from  game.dbo.stock_data_bak where card_id='+@card_id+' and over_time='+@over_time+'" queryout ' + @filePath + ' -c  -T''
    ')
提示错误
SQLState = 37000, NativeError = 170
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]第 1 行: '30' 附近有语法错误。
SQLState = 37000, NativeError = 8180
Error = [Microsoft][ODBC SQL Server Driver][SQL Server]未能准备语句。
NULL

解决方案 »

  1.   

    DECLARE @over_time datetime
    DECLARE @card_id intDECLARE @filePath VARCHAR(100)
    declare @date VARCHAR(100)
    --declare @sql varchar(1000)
    set @over_time='2010-09-30'
    set @card_id=1
    set @date=replace(replace(replace(CONVERT(varchar, @over_time, 120 ),'-',''),' ',''),':','')
    SET @filePath = 'd:\过期时间'+@date+convert(VARCHAR,@card_id)+'.xls'
    --set @sql='bcp "select card_id,card_code,card_pass,date_time,over_time,ispay from game.dbo.stock_data_bak where card_id='+@card_id+' and over_time='+@over_time+'"'
    select @filePath
    declare @cmd varchar(max)
    set @cmd ='bcp "select card_id,card_code,card_pass,date_time,over_time,ispay from game.dbo.stock_data_bak where card_id='
    +convert(varchar(20),@card_id)
    +' and over_time='''
    +convert(varchar(20),@over_time)
    +'''" queryout ' 
    + @filePath + ' -c -T''
      ' 
     EXEC master..xp_cmdshell  @cmd
      

  2.   

    DECLARE @over_time datetime
    DECLARE @card_id intDECLARE @filePath VARCHAR(100)
    declare @date VARCHAR(100)
    declare @date1 varchar(200)
    set @over_time='2010-09-30 12:10:45.000'
    set @card_id=2
    set @date1=CONVERT(varchar, @over_time, 120 )
    set @date=replace(replace(replace(CONVERT(varchar, @over_time, 120 ),'-',''),' ',''),':','')
    SET @filePath = 'd:\过期时间'+@date+'卡类型为'+convert(VARCHAR,@card_id)+'.xls'select @filePath
    select @date1EXEC('EXEC master..xp_cmdshell ''bcp "select card_id,card_code,card_pass,date_time,over_time,ispay from game.dbo.stock_data_bak where card_id='+@card_id+' and over_time='''''+@date1+'''''" queryout ' + @filePath + ' -c -T''')
    这样写就好了。
    感谢大家了。