Author |
Topic |
skc40
Starting Member
34 Posts |
Posted - 2013-11-09 : 20:14:46
|
Hi all,The [Data] field is varchar(100) in Temp_Table and contains records of various lengths. e.g. 100, 50, 25. I want to create a text file (temp_data.txt) using sql cmd. Below query will produce a text file of 100 characters for all records.But what I want is a variable length file as indicated above. sqlcmd -b -Q "SET NOCOUNT ON; SELECT [Data] from Temp_Table" -o "Temp_Data.txt"Thanks |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2013-11-09 : 20:28:38
|
Use bcp.exe instead:bcp "SELECT [Data] from Temp_Table" queryout Temp_Data.txt -Sserver1\instance1 -T -t, -c -r\r\nTara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
skc40
Starting Member
34 Posts |
Posted - 2013-11-09 : 21:09:40
|
[quote]Originally posted by tkizer Use bcp.exe instead:bcp "SELECT [Data] from Temp_Table" queryout Temp_Data.txt -Sserver1\instance1 -T -t, -c -r\r\nTara, thanks for your help. However, i am not that familiar with using bcp.If the query can only be executed by a UserName on a specified server/database and saved in a specified location as Temp_Data.txt (11.11.11.11\Temp\Temp_Data.txt)and what would be the new bcp query?Something like this?bcp -U "userName" -P "pwd" -S "ServerName" -d "DatabaseName" "SELECT [Data] from Temp_Table" queryout "11.11.11.11\Temp\Temp_Data.txt" -Sserver1\instance1 -T -t, -c -r\r\n |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
skc40
Starting Member
34 Posts |
Posted - 2013-11-10 : 13:26:25
|
bcp "SELECT [Data] FROM Temp_Table" queryout "\\11.11.11.11\Temp\Temp_text.txt" -S "110.11.100.00"\"Test" -U "User_Name" -P "pwd" -t, -c -r\r\n' User name not provided, either use -U to provide the user name or use -T for Trusted ConnectionVisakh, I followed your instructions, but somehow-- not able to get this working. Above is the error message that I received.Following are the variables. Query: SELECT [Data] from Temp_Table Output File: \\11.11.11.11\Temp\Temp_TextDB_Server: 110.11.100.00Database: TEMPUser: User_Namepassword: pwdFile Type: Variable LengthCould you please help here.Thanks |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2013-11-10 : 19:31:39
|
bcp "SELECT [Data] FROM Temp_Table" queryout \\11.11.11.11\Temp\Temp_text.txt -S 110.11.100.00\Test -T -t, -c -r\r\nIs Temp a shared folder? If you get an error regarding the path, then you need to fix it so that you are referencing a shared folder on the network resource. Otherwise, save it locally.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
skc40
Starting Member
34 Posts |
Posted - 2013-11-10 : 20:02:24
|
TKizer, I tried your solution and used SQL connection instead. C:\WINDOWS\TEMP>bcp "SELECT [Data] FROM Temp_Table" queryout "\\11.11.11.11\Temp\Temp_text.txt" -S "110.11.100.00\Test" -U "User_Name" -P "pwd" -t, -c -r\r\n But received error message as below...ERROR MESSAGE SQLState = 08001, NativeError = -1Error = [Microsoft][SQL Native Client]SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF]. SQLState = 08001, NativeError = -1Error = [Microsoft][SQL Native Client]An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections.SQLState = S1T00, NativeError = 0Error = [Microsoft][SQL Native Client]Login timeout expiredThe Server Name is actually 110.11.100.00 and the Database Name is Test.The file path is correct as well and need to save it so that the file can be attached and send it to the respective party (part of the job). Could you please advise.Thanks |
|
|
skc40
Starting Member
34 Posts |
Posted - 2013-11-10 : 20:46:53
|
TKizer, finally able to adjust the query below :)C:\WINDOWS\TEMP>bcp "SELECT [Data] FROM Test.dbo.Temp_Table" queryout "\\11.11.11.11\Temp\Temp_text.txt" -S "110.11.100.00" -U "User_Name" -P "pwd" -t, -c -r\r\nOutput was as expected, however there's an extra line as belowHEADER RECORDCRLF--- RECORD LENGTH 60CRLFEMPLOYEE RECORDCRLF --RECORD LENGTH 100CRLFEMPLOYEE RECORDCRLF--RECORD LENGTH 100CRLFTRAILER RECORDCRLF--RECORD LENGTH 20CRLF |
|
|
skc40
Starting Member
34 Posts |
Posted - 2013-11-10 : 20:58:37
|
TKizer/Visakh-- thank you all for your help. Finally was able to generate the desired result-set.Final query below did the trick :)bcp "SELECT [Data] FROM Test.dbo.Temp_Table" queryout "\\11.11.11.11\Temp\Temp_text.txt" -S "110.11.100.00" -U "User_Name" -P "pwd" -t, -cTY |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-11-11 : 01:43:38
|
cool------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2013-11-11 : 12:23:43
|
quote: Originally posted by skc40 TKizer/Visakh-- thank you all for your help. Finally was able to generate the desired result-set.Final query below did the trick :)bcp "SELECT [Data] FROM Test.dbo.Temp_Table" queryout "\\11.11.11.11\Temp\Temp_text.txt" -S "110.11.100.00" -U "User_Name" -P "pwd" -t, -cTY
You're welcome, glad to help. The -r\r\n is my typical EOL option. Glad you figured out to remove it to get the formatting you needed.Tara KizerSQL Server MVP since 2007http://weblogs.sqlteam.com/tarad/ |
|
|
|