http://topic.csdn.net/u/20080728/17/0cdb96bc-9817-4570-8039-fcba3b156be4.html
第二点。--*************************************************
--(2)我经常执行一些语句时,老是报错,怎么也找不到问题在哪?
/*
我执行:
EXEC master..xp_cmdshell 'bcp "select * from test.dbo.tb" queryout D:\test.txt -c -t, -T'
但是执行下面语句就报错
---------------------------
DECLARE @filePath VARCHAR(100)
SET @filePath = 'd:\test.txt'
EXEC master..xp_cmdshell 'bcp "select * from test.dbo.tb" queryout ' + @filePath + ' -c -t, -T'
---------------------------
因为xp_cmdshell后的命令字串是个常量字串,所以这样写不符合要求.如果@filePath是变量的话,可以用以下方式解决
双重exec嵌套
*/
DECLARE @filePath VARCHAR(100)
SET @filePath = 'd:\test.txt'
EXEC('
EXEC master..xp_cmdshell ''bcp "select * from test.dbo.tb" queryout ' + @filePath + ' -c -t, -T''
')
/*通常犯这类错误的还有:
----------------------
DECLARE @k INT
SET @k=1
EXEC sp_msforeachtable @command1='SELECT * FROM ? WHERE cid=' + RTRIM(@k),@whereand=' AND name IN (''ta'',''tb'')'
----------------------
@command1被要求为是常量,不能使用变量拼接,同样,采用exec嵌套
*/
DECLARE @k INT
SET @k=1
EXEC('
EXEC sp_msforeachtable @command1=''SELECT * FROM ? WHERE cid=' + @k + ''',@whereand='' AND name IN (''''ta'''',''''tb'''')''
')
第二点。--*************************************************
--(2)我经常执行一些语句时,老是报错,怎么也找不到问题在哪?
/*
我执行:
EXEC master..xp_cmdshell 'bcp "select * from test.dbo.tb" queryout D:\test.txt -c -t, -T'
但是执行下面语句就报错
---------------------------
DECLARE @filePath VARCHAR(100)
SET @filePath = 'd:\test.txt'
EXEC master..xp_cmdshell 'bcp "select * from test.dbo.tb" queryout ' + @filePath + ' -c -t, -T'
---------------------------
因为xp_cmdshell后的命令字串是个常量字串,所以这样写不符合要求.如果@filePath是变量的话,可以用以下方式解决
双重exec嵌套
*/
DECLARE @filePath VARCHAR(100)
SET @filePath = 'd:\test.txt'
EXEC('
EXEC master..xp_cmdshell ''bcp "select * from test.dbo.tb" queryout ' + @filePath + ' -c -t, -T''
')
/*通常犯这类错误的还有:
----------------------
DECLARE @k INT
SET @k=1
EXEC sp_msforeachtable @command1='SELECT * FROM ? WHERE cid=' + RTRIM(@k),@whereand=' AND name IN (''ta'',''tb'')'
----------------------
@command1被要求为是常量,不能使用变量拼接,同样,采用exec嵌套
*/
DECLARE @k INT
SET @k=1
EXEC('
EXEC sp_msforeachtable @command1=''SELECT * FROM ? WHERE cid=' + @k + ''',@whereand='' AND name IN (''''ta'''',''''tb'''')''
')
set @a1='12345'set @s='exec master..xp_cmdshell ''bcp "select * from table where id='''+@a1+'''" queryout "d:\text.txt" -c -q -U"sa" -P""''' exec(@s)
眼睛都花了
我知道怎么写了
但是为什么bcp前要加两个引号呢一对引号不就闭合了吗
也就是set @s='exec master..xp_cmdshell ''bcp "select * from table where id='''+@a1+'''" queryout "d:\text.txt" -c -q -U"sa" -P""'''
同样颜色的是一对闭合引号
不知道我理解的对不对
EXEC('
exec master..xp_cmdshell ''bcp "select * from table where id=' + @a1 + '" queryout "d:\text.txt" -c -q -Usa -Pxx''
')
insert tb select 1 union all select 2
declare @a1 char(5),@s varchar(1000)
set @a1='1'
EXEC('
exec master..xp_cmdshell ''bcp "select * from db1..tb where id=' + @a1 + '" queryout "d:\text.txt" -c -q -S. -Usa -Pxxx''
')/*
NULL
开始复制...
NULL
已复制了 1 行。
数据包的大小(字节): 4096
时钟时间(毫秒): 共 1
NULL*/
也就是EXEC('
exec master..xp_cmdshell ''bcp "select * from table where id=''''' + @a1 + '''''" queryout "d:\text.txt" -c -q -Usa -Pxx''
')
declare @a1 char(5),@s varchar(1000)
set @a1='1'
EXEC('
exec master..xp_cmdshell ''bcp "select * from db1..tb where id=' + @a1 + '" queryout "d:\text.txt" -c -q -S. -Ufc -Pbd5178''
')/*
NULL
开始复制...
NULL
已复制了 1 行。
数据包的大小(字节): 4096
时钟时间(毫秒): 共 1
NULL*/