Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Undeclared variable error messege problem


Undeclared variable error messege problem

Author
Message
ali.m.habib
ali.m.habib
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 163
I defined a variable

and want to right its value to text file I used this code for transfer



DECLARE @FileName varchar(50),
@bcpCommand varchar(2000),
@CTX_TRAILER char(2000)
set @CTX_TRAILER ='HIHIHI'
SET @FileName = 'D:\alianz\output\CEMTXOUT_ALIANZ'

SET @bcpCommand = 'bcp " select @CTX_TRAILER " queryout "'
SET @bcpCommand = @bcpCommand + @FileName + '" -T -c'
EXEC master..xp_cmdshell @bcpCommand
select @bcpCommand


the above code always give error Must declare the variable '@CTX_TRAILER' any advice please, this is a sample code that I will use to build large one
ALZDBA
ALZDBA
SSCertifiable
SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)SSCertifiable (7K reputation)

Group: General Forum Members
Points: 6984 Visits: 8839
Try this:

SET @bcpCommand = 'bcp " select ' + @CTX_TRAILER +' " queryout "'



Why aren't you using the T-SQL "Backup database" command with a SQLCMD ?

Johan


Don't drive faster than your guardian angel can fly ...
but keeping both feet on the ground won't get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


"press F1 for solution", "press shift+F1 for urgent solution" :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
ali.m.habib
ali.m.habib
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 163
ALZDBA (7/6/2009)
Try this:

SET @bcpCommand = 'bcp " select ' + @CTX_TRAILER +' " queryout "'



Why aren't you using the T-SQL "Backup database" command with a SQLCMD ?


It not owrk I tried '+ @Variable+' , I didn't understand T-SQL "Backup database" command with a SQLCMD
David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7968 Visits: 9427
1. The first SET @bcpCommand is malformed, it needs to be

SET @bcpCommand = 'bcp "select ''' + @CTX_TRAILER +'''" queryout "'

2. @CTX_TRAILER is char(2000) and @bcpCommand varchar(2000) therefore it is too small to contain the command and the command will be truncated thus causing an error

based on the definition you posted @bcpCommand needs to be at least varchar(2083)


Far away is close at hand in the images of elsewhere.

Anon.


ali.m.habib
ali.m.habib
Valued Member
Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)Valued Member (68 reputation)

Group: General Forum Members
Points: 68 Visits: 163
I displayed the output and it work but when trbasfer it give this error


Error = [Microsoft][ODBC SQL Server Driver]Unable to open BCP host data-file

do u have any idea to fix it
David Burrows
David Burrows
SSCertifiable
SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)SSCertifiable (8K reputation)

Group: General Forum Members
Points: 7968 Visits: 9427
Make sure the destination folder exists and that the SQL Server service account has write access.


Far away is close at hand in the images of elsewhere.

Anon.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search