try
exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where logtime > ''2008-12-8 12:00:00'' and logtime < ''2008-12-8 12:15:00'' and resultcode not in (''12209,''407'',''12202'')" queryout c:\temp\test.csv -c -Usa -PPassword -t,'

解决方案 »

  1.   

    exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where logtime > ''2008-12-8 12:00:00'' and logtime < ''2008-12-8 12:15:00'' and resultcode not in (''2209'',''407'',''2202'')" queryout c:\temp\test.csv -c -Usa -PPassword -t,' 
      

  2.   

    --TRY
    exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where logtime > ''2008-12-8 12:00:00'' and logtime < ''2008-12-8 12:15:00'' and resultcode not in (''12209'',''407'',''12202'')" queryout c:\temp\test.csv -c -Usa -PPassword -t' 
      

  3.   

    sql语句基本语法 
    1 :普通SQL语句可以用Exec执行 eg:   Select * from tableName 
             Exec('select * from tableName') 
             Exec sp_executesql N'select * from tableName'    -- 请注意字符串前一定要加N 2:字段名,表名,数据库名之类作为变量时,必须用动态SQL eg:   
    declare @fname varchar(20) 
    set @fname = 'FiledName' 
    Select @fname from tableName              -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。 
    Exec('select ' + @fname + ' from tableName')     -- 请注意 加号前后的 单引号的边上加空格 当然将字符串改成变量的形式也可 
    declare @fname varchar(20) 
    set @fname = 'FiledName' --设置字段名 declare @s varchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s)                -- 成功 
    exec sp_executesql @s   -- 此句会报错 declare @s Nvarchar(1000)  -- 注意此处改为nvarchar(1000) 
    set @s = 'select ' + @fname + ' from tableName' 
    Exec(@s)                -- 成功     
    exec sp_executesql @s   -- 此句正确 3. 输出参数 
    declare @num int, 
            @sqls nvarchar(4000) 
    set @sqls='select count(*) from tableName' 
    exec(@sqls) 
    --如何将exec执行结果放入变量中? declare @num int, 
                   @sqls nvarchar(4000) 
    set @sqls='select @a=count(*) from tableName ' 
    exec sp_executesql @sqls,N'@a int output',@num output 
    select @num 
      

  4.   

    在下还有一问题请教,望各位大侠不吝赐教:
    我想把从前天到今天3天之内8点~18点的数据导出,但执行时出错。
    SQL Code
    declare @start        datetime
    declare @end          datetimeset @start = dateadd(Hour,1,CONVERT(char(10),   GETDATE(),   120))
    set @end = dateadd(Hour,2,CONVERT(char(10),   GETDATE(),   120))exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where ((logtime > @start and logtime < @end) or (logtime > dateadd(day,-1,@start) and logtime<dateadd(day,-1,@end)) or (logtime>dateadd(day,-2,@start) and logtime<dateadd(day,-2,@end)))and resultcode not in (''12209'',''407'',''12202'')" queryout c:\temp\test.csv -c -Usa -PPassword** -t,'
    Results:
    SQLState = 42000, NativeError = 137
    Error = [Microsoft][SQL Native Client][SQL Server]Must declare the scalar variable "@start".
    SQLState = 42000, NativeError = 8180
    Error = [Microsoft][SQL Native Client][SQL Server]Statement(s) could not be prepared.
    NULL
      

  5.   

    本帖最后由 fcuandy 于 2008-12-08 15:25:56 编辑
      

  6.   

    ---try
    exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where ((logtime > '+@start+' and logtime < '+@end+') or (logtime > dateadd(day,-1,'+@start+') and logtime <dateadd(day,-1,'+@end+')) or (logtime>dateadd(day,-2,'+@start+') and logtime <dateadd(day,-2,'+@end+')))and resultcode not in (''12209'',''407'',''12202'')" queryout c:\temp\test.csv -c -Usa -PPassword** -t,' 
      

  7.   

    1,先拼好变量再执行:declare @s varchar(1000)
    declare @s1 varchar(50),@e1 varchar(50)
    select @s1=convert(varchar(10),@start,120) + ' ' + convert(varchar(10),@start,114),
    @e1=convert(varchar(10),@end,120) + ' ' + convert(varchar(10),@end,114),
    set @s='bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where ((logtime > ''' + @s1 ''' and logtime < ''' + @s1 + ''') or (logtime > dateadd(day,-1,''' + @s1 + ''') and logtime <dateadd(day,-1,''' + @e1+ ''')) or (logtime>dateadd(day,-2,@start) and logtime <dateadd(day,-2,''' + @e1 + ''')))and resultcode not in (''12209'',''407'',''12202'')" queryout c:\temp\test.csv -c -Usa -PPassword** -t,'exec xp_cmdshell @s
      

  8.   

    --这个应该没有问题
    exec master..xp_cmdshell 'bcp "select ClientIP,logTime,processingtime,protocol,
    operation,resultcode,CacheInfo from isalog..webproxylog 
    where logtime > ''2008-12-8 12:00:00'' and logtime < ''2008-12-8 12:15:00'' and resultcode not in (''12209'',''407'',''12202'')" 
    queryout "c:\temp\test.csv" /T /C' 
      

  9.   

    2,双重exec嵌套 
    declare @s1 varchar(50),@e1 varchar(50)
    select @s1=convert(varchar(10),@start,120) + ' ' + convert(varchar(10),@start,114),
        @e1=convert(varchar(10),@end,120) + ' ' + convert(varchar(10),@end,114)exec ('
    exec master..xp_cmdshell ''bcp "select ClientIP,logTime,processingtime,protocol,operation,resultcode,CacheInfo from isalog..webproxylog where ((logtime > ''''' + @s1 + ''''' and logtime < ''''' + @e1 + ''''') or (logtime > dateadd(day,-1,''''' + @s1 + ''''') and logtime <dateadd(day,-1,''''' + @e1 + ''''')) or (logtime>dateadd(day,-2,''''' + @s1 + ''''') and logtime <dateadd(day,-2,''''' + @e1 + ''''')))and resultcode not in (''''12209'''',''''407'''',''''12202'''')" queryout c:\temp\test.csv -c -Usa -PPassword** -t,'' 
    ')当然,也可以用sp_executesql注入参数。可能会有手误,你自己调一下。