如何逃避单引号 [英] How to escape single quotes

查看:81
本文介绍了如何逃避单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我想为我的查询添加值(我将查询保存为csv文件)。

  DECLARE   @ year   varchar  
SET @ year = ' 2015-01-01';
exec xp_cmdshell ' bcpSELECT * FROM SplitValues(''' + @ year + ' '')queryoutC:\doc.csv-c -t, - T -S',no_output



但是我在 +

错误语法p $ p> 

如何无错误地增加价值?



MS SQL SERVER T-SQL

解决方案

在exec命令期间无法构造查询。首先构造它然后调用exec:



  DECLARE   @ year   varchar  @sql   VARCHAR  
SET @ year = ' 2015-01-01';
SET @ sql = ' bcpSELECT * FROM SplitValues(''' + @ year + ' '')queryoutC:\ doc.csv-c -t, - T -S'

exec xp_cmdshell,no_output







这是一个愚蠢的规则,但规则是


尝试更改 @year 的定义:

  DECLARE   @ year   varchar  50 
SET @ year = ' 2015-01-01';

Woudl工作得更好。

然后,如果没有解决它,也尝试:

  DECLARE   @ CMD   VARCHAR  100 
SET @ CMD = ' bcpSELECT * FROM SplitValues(''' + @ year + ' '')queryoutC:\doc.csv-c -t, - T -S'
EXEC xp_cmdshell @ CMD ,no_output



如果你 SELECT @CMD ,你会看到字符串是正确的:

 bcpSELECT * FROM SplitValues('2015-01-01')queryoutC: \doc.csv-c -t, -  T -S 


Hi all . I want to add value to my query ( i save query as csv document) .

DECLARE @year varchar
SET @year = '2015-01-01' ;
exec xp_cmdshell 'bcp "SELECT * FROM SplitValues(''' + @year + ''')" queryout "C:\doc.csv" -c -t"," -T -S ', no_output  


But i have an error

incorrect syntax near the  "+"


How to add value without errors?

MS SQL SERVER T-SQL

解决方案

You can't construct the query during exec commands. Construct it first and then call exec:

DECLARE @year varchar, @sql VARCHAR
SET @year = '2015-01-01' ;
SET @sql = 'bcp "SELECT * FROM SplitValues(''' + @year + ''')" queryout "C:\doc.csv" -c -t"," -T -S '

exec xp_cmdshell, no_output




'tis a silly rule, but a rule it is


Try changing the definition of @year:

DECLARE @year varchar(50)
SET @year = '2015-01-01' ;

Woudl work better.
Then, if that doesn't fix it, also try:

DECLARE @CMD VARCHAR(100)
SET @CMD = 'bcp "SELECT * FROM SplitValues(''' + @year + ''')" queryout "C:\doc.csv" -c -t"," -T -S '
EXEC xp_cmdshell @CMD, no_output


If you SELECT @CMD, you will see the string is correct:

bcp "SELECT * FROM SplitValues('2015-01-01')" queryout "C:\doc.csv" -c -t"," -T -S 


这篇关于如何逃避单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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