Quantcast
Channel: SQL Server Setup & Upgrade forum
Viewing all articles
Browse latest Browse all 7696

Strange Behaviour in a stored Procedure

$
0
0

Hello forum

I am attempting to build an execute script as a part of a wider process.  essentially data is imported into a temporary table and then exported via bcp; so in order to capture the column headings I am using a cursor to build a string and then execute it.  However, if I "PRINT" the script the script is correct, but if I "EXECUTE" either with EXECUTE or sp_executesql, the script is incorrect.

Here is the Code: -

Set

@ColNameScript =(Select'Select ')


Set

@TotalCol =(Selectcount(*)from syscolumnswhere idin(select idfrom sysobjectswherename='voltemp'))


Set

@CurrentCol=1


Declare

Column_Curs Cursorforselectnamefrom syscolumnswhere idin(select idfrom sysobjectswherename='voltemp')


Open

Column_Curs


Fetch

NextFrom Column_CursInto @ColumnName


While

@@Fetch_Status=0



Begin


IF @CurrentCol<> @TotalCol

BEGIN


Set @ColNameScript=(Select @ColNameScript+ '^'+ @ColumnName+'^'+' AS ['+ @ColumnName+'],')


END


IF @CurrentCol= @TotalCol

BEGIN


Set @ColNameScript=(Select @ColNameScript+ '^'+ @ColumnName+'^'+ ' AS ['+ @ColumnName+'] INTO COLNAMETEMP')


END


FetchNextFrom Column_CursInto @ColumnName

Set @CurrentCol=@CurrentCol+1


End

Close

Column_Curs


Deallocate

Column_Curs


SELECT

REPLACE(@ColNameScript,'^','''')


PRINT

@ColNameScript


EXECUTE

@ColNameScript;

If I comment out the EXECUTE command the script is printed as thus: -

Select

'territoryName'AS [territoryName],'organisation'AS [organisation],'displayname'AS [displayname],'URN'AS [URN],'vacancyid'AS [vacancyid],'jobtitle'AS [jobtitle],'Displayfrom'AS [Displayfrom],'displayto'AS [displayto],'date'AS [date],'Advert/vacancytype'AS [Advert/vacancytype],'Type(s) of Work'AS [Type(s) of Work],'status'AS [status],'PageViews'AS [PageViews],'Total Interaction'AS [Total Interaction],'Advert Run'AS [Advert Run],'Job Live Days'AS [Job Live Days],'Click through rate'AS [Click through rate],'Days to go'AS [Days to go],'Clicks per day'AS [Clicks per day]INTO COLNAMETEMP

If I include the EXECUTE command the script is printed as thus: -

Select

^territoryName^AS [territoryName],^organisation^AS [organisation],^displayname^AS [displayname],^URN^AS [URN],^vacancyid^AS [vacancyid],^jobtitle^AS [jobtitle],^Displayfrom^AS [Displayfrom],^displayto^AS [displayto],^date^AS [date],^Advert/vacancytype^AS [Advert/vacancytype],^Type(s)of Work^AS [Type(s) of Work],^status^AS [status],^PageViews^AS [PageViews],^Total Interaction^AS [Total Interaction],^Advert Run^AS [Advert Run],^Job Live Days^AS [Job Live Days],^Click through rate^AS [Click through rate],^Daysto go^AS [Days to go],^Clicks per day^AS [Clicks per day]INTO COLNAMETEMP

SQL Server version is 2005 Standard Edition SP4.


Please click "Mark As Answer" if my post helped. Tony C.


Viewing all articles
Browse latest Browse all 7696

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>