Solved

Getting Function sequence error in BCP executing T-SQL block

Posted on 2007-11-15
10
3,276 Views
Last Modified: 2012-05-05
The following block work fine in studio

select int_custodial_agent_nbr into #tempCA from [IND-2].dbo.custodial_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select 'TEMP_BAM_' + cast(int_custodial_agent_nbr 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_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select ''TEMP_BAM_'' + cast(int_custodial_agent_nbr 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.

0
Comment
Question by:FusionApps
10 Comments
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
I had something similar a whileback ---i think i made declare @cmdstring varchar(8000) and for some reason that worked......

declare @cmdstring varchar(8000),
      @bcpstring varchar(8000)

apologies if totally wrong....
0
 

Author Comment

by:FusionApps
Comment Utility
Tried the solution and got same error
0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
can you post a error code? full error message?
0
 

Author Comment

by:FusionApps
Comment Utility
Here is the output with error.

bcp "select int_custodial_agent_nbr into #tempCA from [IND-2].dbo.custodial_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select 'TEMP_BAM_' + cast(int_custodial_agent_nbr as varchar(20)) from #tempCA) select @str_sql = 'select * from ' + @table exec(@str_sql)" queryout d:\test\data\bcpTest2.txt -T -c
output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQLState = HY010, NativeError = 0
Error = [Microsoft][SQL Native Client]Function sequence error
NULL
0
 

Author Comment

by:FusionApps
Comment Utility
Tried not using a temp table and get a different error.

bcp "declare @tempdata varchar(1000) select @tempdata = cast(int_custodial_agent_nbr as varchar(20)) from [IND-2].dbo.custodial_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select 'TEMP_BAM_' + @tempdata)  select @str_sql = ('select str_data from ' + @table) exec(@str_sql)" queryout d:\test\data\bcpTest2.txt -T -c
output
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
SQLState = HY000, NativeError = 0
Error = [Microsoft][SQL Native Client]BCP host-files must contain at least one column
NULL
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:FusionApps
Comment Utility
Here is the SQL.

declare @cmdstring varchar(4000),
      @bcpstring varchar(4000)


set @cmdstring = 'declare @tempdata varchar(1000) select @tempdata = cast(int_custodial_agent_nbr as varchar(20)) from [IND-2].dbo.custodial_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select ''TEMP_BAM_'' + @tempdata)  select @str_sql = (''select str_data from '' + @table) exec(@str_sql)'

set @bcpstring = 'bcp "' + @cmdstring + '" queryout d:\test\data\bcpTest2.txt -T -c'  

print @bcpstring
exec xp_cmdshell @bcpstring

0
 

Author Comment

by:FusionApps
Comment Utility
I have test data in a table and when I run the sql it returns data.

declare @tempdata varchar(1000) select @tempdata = cast(int_custodial_agent_nbr as varchar(20)) from [IND-2].dbo.custodial_agents where int_custodial_agent_id = 14 DECLARE @table varchar(20), @str_sql varchar(200) select @table = (select 'TEMP_BAM_' + @tempdata)  select @str_sql = ('select str_data from ' + @table) exec(@str_sql)
0
 
LVL 12

Accepted Solution

by:
kselvia earned 500 total points
Comment Utility
Dos command line is limited to 255 characters.

bcp command is getting truncated

You need to put those statements into a stored procedure and make BCP execute the stored procedure
0
 
LVL 31

Expert Comment

by:James Murrell
Comment Utility
Dos command line is limited to 255 characters - couldn't you have the script in a file that the dos calls?
0
 
LVL 6

Expert Comment

by:Rajesh_mj
Comment Utility
Hi,

This error would occur on the querires that does not return any columns

Just check:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=535939&SiteID=1

I feel kselvia's suggestion is the best . create a stored procedure &
run bcp command as 'bcp "exec myproc" queryout "outputfile" -S server -U user -P pwd '
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now