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
 General SQL Server Forums
 New to SQL Server Programming
 DTS Export to formated TXT file using variables

Author  Topic 

luisfilipenunes
Starting Member

4 Posts

Posted - 2012-10-30 : 21:47:57
Hi,


I need to export data from a SQL table into a text file with fixed positions defined as constants.

I wrote the following script for this export using Nortwind DB:

declare @arm char(2), @cliente char(5), @typ char(3), @na1 char(2), @ref char(15), @str char(27)
select @arm = '51', @cliente = '00717', @typ = 'REF', @na1 = ' '
set @ref = (SELECT CustomerID FROM Northwind..Customers where CustomerID = 'CACTU')
select @str = @arm + @cliente + @typ + @na1 + @ref
select @str

The result is:
-------------------

5100717REF CACTU

----------------------

How can I use this script on a DTS in order to export all records from the Customers table keeping the format "5100717REF" and adding the CustomerID as variable to get the different rows on the table to the export file.

Example:

The expected result is a text file containing all table rows:
-------------------

5100717REF ALFKI
5100717REF ANATR
5100717REF ANTON
...
5100717REF CACTU
...
5100717REF WOLZA


Thanks for your help in advance!


Luis Nunes

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2012-10-31 : 03:36:03
select @arm = '51', @cliente = '00717', @typ = 'REF', @na1 = ' ', @ref = CustomerID from Customers



Too old to Rock'n'Roll too young to die.
Go to Top of Page

luisfilipenunes
Starting Member

4 Posts

Posted - 2012-11-03 : 15:59:11
Thanks for the hint, but when I try to go deeper on my task I have an issue regarding formatting this table record.
I need format the CustomerID to char(15) and the other table records to char (20)

This script export the information:

declare @arm char(2), @client char(5), @typ char(3), @na1 char(2), @ref char (50), @descpr char (35), @descabr char (15),
@zloc char(1), @corr char(2), @srt1 char (32)
select @ref = ContactName from Customers
select @arm = '51', @client = '00717', @typ = 'REF', @na1 = ' ', @zloc = ' ', @corr = 'LS'
select @arm, @client, CustomerID , @typ, @na1, @ref, @zloc, @corr, Address from [Customers]

but I need @ref to give me the correspondent exported line and is returning always the last one.



Luis Nunes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-03 : 22:09:44
why do you need all these variables? wont you need just this?


select '51','00717', CustomerID ,'REF', '',ContactName,'','LS', Address from [Customers]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

luisfilipenunes
Starting Member

4 Posts

Posted - 2012-11-04 : 05:21:37
Thanks a lot! I was pretty sure I was not going the right way.

The only thing I need now to solve/investigate is how to change the format of the selected record, i.e., I need "CustomerID" to have 15 fixed positions (char15) and "ContactName" and "Address" with char(35) when exported.

Who can I do this?

My script now is simplified to this:


SELECT '51', '00717', 'REF', ' ', CustomerID,ContactName, Address, ' ', ' ', '00000', '00000', '00000', '000000000',
'00000000', '00000', '00000', '00000', '000000000', '00000000', '00000', '00000', '00000', '00000', '000000000',
'00000000', '00000', '0000000', 'N'
FROM Northwind..[Customers]



Luis Nunes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-04 : 11:36:42
[code]
SELECT '51', '00717', 'REF', ' ', CAST(CustomerID AS char(15)) AS CustomerID,CAST(ContactName AS Char(35)) AS ContactName, CAST(Address AS Char(35)) AS Address, ' ', ' ', '00000', '00000', '00000', '000000000',
'00000000', '00000', '00000', '00000', '000000000', '00000000', '00000', '00000', '00000', '00000', '000000000',
'00000000', '00000', '0000000', 'N'
FROM Northwind..[Customers]
[/code]


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

luisfilipenunes
Starting Member

4 Posts

Posted - 2012-11-06 : 04:31:18
My question is solved and it was quite simple after all ;) but i'll never get there fast enough.

Thanks a lot for your hints!


Luis Nunes
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-06 : 12:07:44
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -