The following block work fine in studio
select int_custodial_agent_nbr into #tempCA from [IND-2].dbo.custodial_agen
ts where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select 'TEMP_BAM_' + cast(int_custodial_agent_n
br as varchar(20)) from #tempCA) select @str_sql = 'select * from ' + @table exec(@str_sql)
But when trying to send it to BCP with xp_cmdshell recive Function sequence error.
declare @cmdstring varchar(4000),
@bcpstring varchar(4000)
set @cmdstring = 'select int_custodial_agent_nbr into #tempCA from [IND-2].dbo.custodial_agen
ts where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select ''TEMP_BAM_'' + cast(int_custodial_agent_n
br as varchar(20)) from #tempCA) select @str_sql = ''select * from '' + @table exec(@str_sql)'
set @bcpstring = 'bcp "' + @cmdstring + '" queryout d:\test\data\bcpTest2.txt -T -c'
print @bcpstring
exec xp_cmdshell @bcpstring
Not sure if using a temp table is the problem.
Any help would be appreciated.
Start Free Trial