Score:0

How to insert and update data from CSV using sqlcmd on ubuntu 20.04?

in flag

I can convert data database to .csv using sqlcmd syntax on my ubuntu

sqlcmd -S ipdb -U sa -P "passwordsa" -d dbname -Q "select * from tabelname" -o "direktori/file.csv" -s"," -W -w 700

but how can I insert data from my .csv to table using sqlcmd syntax?

Preman Terminal avatar
in flag
@matigo yes, can you help me?
FloT avatar
us flag
I cannot try it, but as a hint I would try something like `sqlcmd -S ipdb -U sa -P "passwordsa" -d dbname -Q "BULK INSERT tabelname FROM 'direktori/file.csv' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', TABLOCK)"`. More details about "bulk insert" on official website : https://docs.microsoft.com/en-us/sql/t-sql/statements/bulk-insert-transact-sql?view=sql-server-ver15
Preman Terminal avatar
in flag
Thanks you @FloT And I read to know more about bulk insert, and I find this link https://docs.microsoft.com/en-us/sql/linux/sql-server-linux-migrate-bcp?view=sql-server-ver15
FloT avatar
us flag
Great ! May I suggest you to answer your own question and mark it as solved then ?
Preman Terminal avatar
in flag
okay, but I dont know how to mark it as solved. Could you please show me the way? @FloT
FloT avatar
us flag
There should be an "Answer your question" button somewhere below the question. Click it, add your solution and save it. Then I guess you'll be able to accept your answer by clicking on the tick below the upvote / downvote buttons. You can refer to this post : https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work
Preman Terminal avatar
in flag
@FloT okay thankyou very much. Have A nice day :)
Score:1
in flag

It solved by using bulk copy data (bcp) syntax.

bcp Tablename in ~/filename.txt -S localhost -U sa -P <your_password> -d Databasename -c -t  ','
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.