How can I specifie column order while Bulk Copying (BCP) Out the tables from SYBASE_IQ. - sql

Below is the code I am using in the batch script to BCP process.
call bcp.exe DBName.tablename out FILENAME.csv -e FILENAMEerr.txt -c -t"|" -U USER DETAILS -P PASSWORD -S Servicename -r"\n"

Related

Shell script to load the SqlServer table data into csv File

Need a Unix shell script to load the sqlServer table data into csv File.
Could some one please share the sample shell script.
Below Working:-->
sqlcmd -S $SQLHOSTNAME -U $SQLUSERNAME -P $SQLPASSWORD -d $SQLDATABASE -s" " -W -w 3000 -Q "SET NOCOUNT ON; $query;" | sed 2d >$csv_filename

Import CSV from Linux to Azure SQL Server

I have an Azure SQL Server database and a linux box. I have a csv file on the linux machine that I want to import into SQL Server. I have a table already created where I am going to import this file.
Why does this command return an Unknown argument: -U?
bcp table in ~/test.csv -U myUsername -S databaseServerName -d dbName -q -c -t
When I rearrange the arguments passed to bcp like below, it returns an Unknown argument: -S
bcp table in ~/test.csv -S databaseServerName -d dbName -U myUsername -q -c -t
So contrary to the documentation:
https://learn.microsoft.com/en-us/sql/tools/bcp-utility?redirectedfrom=MSDN&view=sql-server-2017#U
I hit issues where bcp does not like spaces after the argument names.
https://granadacoder.wordpress.com/2009/12/22/bcp-export/
quote from the article above:
//
The other syntax sugar is that there is no space after the -S
argument. As seen below
-SMyServerName\MyInstanceName
bcp.exe "SELECT cast(LastName as char(50)) , cast(FirstName as
char(50)) , cast(MiddleName as char(50)) , cast(Suffix as char(50))
FROM MyAdventureWorksDB.Person.Person ORDER BY NEWID()" queryout
PeopleRock.txt -c -t -T -SMyServerName\MyInstanceName
also
https://www.easysoft.com/products/data_access/odbc-sql-server-driver/bulk-copy.html#importing-data-table
check your syntax sugar in linux (below example is from above easysoft link)
./bcp AdventureWorks.HumanResources.myTeam in ~/myTeam.csv \
-f ~/myTeam.Fmt -U mydomain\myuser -S mymachine\sqlexpress
Note the above has the dbname.schemaname.tablename (before the "in" word above)

Unexpected argument executing cmdexec on a SQL job to export to CSV

I try to run this on a SQL job:
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" > D:\Test.csv
How can I fix this error?
Sqlcmd: '> D:\Test.csv': Unexpected argument.
Have you tried like this -
sqlcmd -S . -d CI_Reports -E -s"," -W -Q "SET NOCOUNT ON SELECT * FROM [dbo].[Table]" -o D:\Test.csv
where -o output_file which would identify the file that receives output from sqlcmd.
Additionally you could try BCP which is best suited for bulk coping data between an instance of Microsoft SQL Server and a data file in a user-specified format.
Read more here.

BCP import all files from a folder to database

BCP Import
How to do BCP import with all files in a folder.
folder
file1.csv
file2.csv
Need to import both the files.
bcp <tableName> in <filename> -t "^" -r "\n" -c -C 28591 -S <databaseinstance> -U <username> -P <password>
Using the above BCP cmd, we can import only one file at a time.
simple BCP command import only single file.
To achieve the above we need to use looping with the command.
I have used the following command simple command.
for /r %i in (*) do bcp <tablename> in %i -t "^" -r "\n" -c -C 28591 -S <databaseinstance> -U <username> -P <password>
It works.

Saving psql output to csv file

I have a query written in a file located at /path/to/query. How can I save the output result to a csv file, without using COPY in the query? I tried the following command, but the output file's fields are separated by " | ".
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ','
It is not explained in the documentation, but the -F option requires the -A option (unaligned table output) to work:
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A
If you don't wish the headers in your csv, this means, without extra rows at the top and at the bottom, use the -t option too.
psql -U username -d dbname -f /path/to/query -o /path/to/output/file -F ',' -A -t
From the help:
-A, --no-align unaligned table output mode
-F, --field-separator=STRING
set field separator (default: "|")
-t, --tuples-only print rows only