Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Other SQL Server Topics (2005)
 How to create a new line in a batch file

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-03-19 : 11:31:34
I am dynamically creating a BAT file to execute a series of bcp imports/exports. This has to be done dynamically, as I don't know the name of the tables until runtime.

I have the following procedure that creates the first 2 sentences of the batch file:
CREATE PROCEDURE dbo.CreateBATScript
(
@ServerName VARCHAR(100)
, @DatabaseName VARCHAR(100)
)
AS
SET NOCOUNT ON

DECLARE @SQLCMD1 VARCHAR(MAX)
, @SQLCMD2 VARCHAR(MAX)
, @BATScript NVARCHAR(MAX)

SET @SQLCMD1 = ' sqlcmd -S '+@ServerName+' -E -d '+@DatabaseName+' -Q "IF OBJECT_ID(''''dbo.NewStoreMapping'''',''''u'''')IS NOT NULL BEGIN DROP TABLE dbo.NewStoreMapping END;"'''

-- Create first part of the script:
SET @BATScript = 'SELECT ''@ECHO OFF'''

-- DROPS & CREATES the dbo.NewStoreMapping table
SET @BATScript = @BATScript+'+CHAR(10)+'''+@SQLCMD1

--SELECT @BATScript
EXEC (@BATScript)


I execute this procedure from the command line and output the results to a bat file (which will eventually contain all the bcp commands):

SQLCMD -E -SWS23 -dsainsburys -Q "EXEC dbo.CreateBATScript 'WS23','sainsburys','D:\',NULL,NULL" -oD:\bcpscript.bat /h-1 

PAUSE


However, all the results are appearing on the same line, even though I put a CHAR(10). What have I done wrong, or is this not possible?

Thanks

Hearty head pats

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-03-19 : 11:44:25
Are you by chance a unix man? End of line in windows is

CHAR(13) + CHAR(10)

carriage return + line feed.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2009-03-19 : 11:49:25
Thanks Charlie!!

That works like a dream! But no, I'm not a UNIX (wo)man, just a Windows amateur

Hearty head pats
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2009-03-19 : 13:11:04
Glad it worked for you!





Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -