Sql不使用cmdshell创建.xls文件 [英] Sql don't create the .xls file with cmdshell

查看:74
本文介绍了Sql不使用cmdshell创建.xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码并没有标记我的错误,但在运行时我没有创建任何文件,任何解决方案?



 声明  @ Fecha   date  @ FechaIni   date  @ FechaFin  日期 
选择 @ Fecha = getdate()
set @ FechaIni = cast( convert char 6 ),dateadd(MM,-1, @ Fecha ), 112 )+ ' 01' as date
set @ FechaFin = dateadd(DD,-1,cast( convert char 6 ), @ Fecha 112 )+ ' 01' as date ))


声明 @ sql Nvarchar (max)
set @sql = N ' xp_cmdshell''BCPselect posc(distinct(till_no))as cajas from poshist..dbo_register_header
其中proc_date介于''''
+ CONVERT NV ARCHAR 30 ), @ FechaIni )+ ' ''''和'''' + CONVERT NVARCHAR 30 ), @ FechaFin )+ ' ''''
和till_no not in(61,62)QUERYOUTC:file.xls - T -c'''



exec @sql





我尝试过:



这是输出



用法:BCP {dbtable |查询} {in |出| queryout |格式}数据文件

[-m maxerrors] [-f formatfile] [-e errfile]

[-F firstrow] [-L lastrow] [-b batchsize]

[-n native type] [-c character type] [-w wide character type]

[-N保持非文本原生] [-V file format version] [-q引用标识符]

[-C代码页说明符] [-t字段终止符] [-r行终止符]

[-i inputfile] [-o outfile ] [-a packetsize]

[-S服务器名称] [-U用户名] [-P密码]

[-T可信连接] [-v版本] [ -R区域启用]

[-k保持空值] [-E保持标识值]

[-h加载提示] [-x生成xml格式文件]

[-d数据库名称]

NULL

解决方案

 < span class =code-keyword> DECLARE   @ Fe cha   date  @ FechaIni   date  @ FechaFin   date ; 
SELECT @ Fecha = GETDATE();
SET @ FechaIni = CAST( CONVERT CHAR 6 ),DATEADD(MM,-1, @ Fecha ), 112 )+ ' 01' AS DATE );
SET @ FechaFin = DATEADD(DD,-1,CAST( CONVERT CHAR 6 ), @ Fecha 112 )+ ' 01' AS DATE ));


DECLARE @ sql NVARCHAR 4000 );
set @ sql = N ' BCPselect count(distinct(till_no))as cajas from
poshist..dbo_register_header
where proc_date in'''
+ CONVERT NVARCHAR 30 ), @ FechaIni )+ ' ''
和'''
+ CONVERT NVARCHAR 30 ), @ FechaFin )+ ' ''和until_no not in
(61,62)QUERYOUTC:file.xls-S'
+ @@ SERVERNAME + ' <跨度class =code-string> -T -c';
EXEC master..xp_cmdshell @ SQL ;


尝试或检查的东西很少



  • 不要立即执行命令,而是尝试打印它。用 print @sql 替换

    exec(@sql),这样你就可以看到输出
  • 复制输出并尝试在命令提示符下手动运行生成的命令以查看它在操作系统级别上是否正常工作
  • 不确定但看起来你有
  • 确保您已启用 xp_cmdshell 。请参阅 xp_cmdshell服务器配置选项| Microsoft Docs [ ^ ]
  • 在驱动器之后,您似乎错过了反斜杠的文件名。所以你应该有 c:\ file.xls
  • c:file.xls >还要确保SQL Server进程对您正在使用的目录具有写权限。根据操作系统级别,可能不允许您在目标文件夹中创建文件。
  • 尝试将输出重定向到文件,以便您可以调查执行期间发生的情况。您可以使用 -o 或命令行重定向与> 2>

I have the following code which does not mark me error but at the time of running it I do not create any file, any solution?

declare @Fecha date, @FechaIni date, @FechaFin date
select @Fecha = getdate()
set @FechaIni = cast(convert(char(6), dateadd(MM, -1, @Fecha), 112) + '01' as date)
set @FechaFin = dateadd(DD, -1, cast(convert(char(6), @Fecha, 112) + '01' as date))


declare @sql Nvarchar(max)
set @sql = N'xp_cmdshell ''BCP "select count(distinct(till_no)) as cajas from poshist..dbo_register_header 
where proc_date between '''''+ CONVERT(NVARCHAR(30), @FechaIni)+ ''''' and '''''+ CONVERT(NVARCHAR(30), @FechaFin) + ''''' 
and till_no not in (61,62)" QUERYOUT  "C:file.xls"  -T -c'' '


exec(@sql)



What I have tried:

This is the output

usage: BCP {dbtable | query} {in | out | queryout | format} datafile
[-m maxerrors] [-f formatfile] [-e errfile]
[-F firstrow] [-L lastrow] [-b batchsize]
[-n native type] [-c character type] [-w wide character type]
[-N keep non-text native] [-V file format version] [-q quoted identifier]
[-C code page specifier] [-t field terminator] [-r row terminator]
[-i inputfile] [-o outfile] [-a packetsize]
[-S server name] [-U username] [-P password]
[-T trusted connection] [-v version] [-R regional enable]
[-k keep null values] [-E keep identity values]
[-h "load hints"] [-x generate xml format file]
[-d database name]
NULL

解决方案

DECLARE @Fecha date, @FechaIni date, @FechaFin date;
SELECT @Fecha = GETDATE();
SET @FechaIni = CAST(CONVERT(CHAR(6), DATEADD(MM, -1, @Fecha), 112) + '01' AS DATE);
SET @FechaFin = DATEADD(DD, -1, CAST(CONVERT(CHAR(6), @Fecha, 112) + '01' AS DATE));


DECLARE @sql NVARCHAR(4000);
set @sql = N'BCP "select count(distinct(till_no)) as cajas from 
                  poshist..dbo_register_header 
                  where proc_date between '''+ CONVERT(NVARCHAR(30), @FechaIni)+ ''' 
                  and '''+ CONVERT(NVARCHAR(30), @FechaFin) + ''' and till_no not in 
                 (61,62)" QUERYOUT  "C:file.xls"   -S '+ @@SERVERNAME +' -T -c';           
EXEC master..xp_cmdshell @SQL;


Few things to try or check

  • Instead of executing the command immediately, try printing it. Replace the
    exec(@sql) with print @sql so you can see what the output is
  • Copy the output from and try running the generated command manually in command prompt to see the it really works on the operating system level
  • Not sure but it looks like you have extra apostrophes around the dates
  • Ensure that you have enabled xp_cmdshell. See xp_cmdshell Server Configuration Option | Microsoft Docs[^]
  • What comes to the file name you seem to be missing the backslash after the drive. So instead of c:file.xls you should have c:\file.xls
  • Also make sure that the SQL Server process has write privileges to the directory you're using. Depending on the OS level it may be that you are not allowed to create files in the destination folder
  • Try redirecting the output to a file so that you can investigate what happens during the execution. You can use -o or command line redirection with > and 2>


这篇关于Sql不使用cmdshell创建.xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆