sqlcmd command to SET IDENTITY_INSERT ON - identity

I am creating a batch file (.bat) with sqlcmd command containing sql insert query. But in which table it will insert data that has one IDENTITY column. Can anyone please help me with the sqlcmd command to SET IDENTITY_INSERT ON?

Related

How to see insert statements in SQL

Is there a way for me to see the insert statements that were used in a SQL table I created. I have provided a picture of the table. I just want to see the statements used to insert these values.
Adding to #Arihant answer,
go to
xampp folder
open cmd there
then
cd mysql
cd bin
then run command given by #Arihant
mysqldump <databasename> <tablename> > dbdump.sql
in same path xampp/mysql/bin you will find a file of name dbdump.sql having all create and insert statements

How to execute SQL command with parameters in bash script

I would like to make a bash script which will add user to my custom table in MySQL database. I have username as script argument stored in variable $USERNAME and I need to execute this query into MySQL server:
INSERT INTO `user` VALUES ('$USERNAME', password)
I know how to execute a sql file but that won't help me when I have username in variable. The only way I can think of is to execute php file with GET values and execute sql command in php. And I'm not eve sure that with php will work as I think.
Is there any better way?
Thanks
EDIT:
After your helpfull answers I went with this command which works:
mysql -ppassword --default-character-set=utf8 -e "INSERT INTO table VALUES (\"$USERNAME#\",\"password\")" database
You could try using a heredoc:
#!/bin/bash
USERNAME="example"
mysql <<MYSQL
INSERT INTO user VALUES ('$USERNAME', password);
MYSQL
The command would be:
mysql -pyourpasswordwithoutspaces -e "Your insert query goes here with semicolon;"
I'm not sure if the semicolons are necessary or not, but it always good to provide them.

What is max number of characters executing from MySQL command line?

I need to execute SQL from MySQL command line with option "--execute=statement, -e statement" but I am afraid my SQL statement is too long which does not fit in option.
If my SQL statement exceeds, how can I set max number of characters of "execute" option?
The sql statement will only execute after the ; sign so you can wrap the statement to the next line.....there is no limit per se
if the statement is too large save in a file and save it as a .sql the give the path to the file on the console
mysql -e "source /path-to-backup/backup-file.sql" db_name

Exporting MySQL table to .csv file

This is my first day with VBA. I am trying to export a .csv file in MS Access using MySQL query in VBA. I have tried using this code:
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim sql As String
Set db = CurrentDb
sql = "SELECT wo_id FROM WorkOrder"
Set rs = db.OpenRecordset(sql)
DoCmd.TransferText acExportDelim, , "rs", "C:\export.csv", True
rs.Close
Set rs = Nothing
which gives me:
Run time error '3011':
The Microsoft Office Access database engine could not find the object 'rs'. Make sure the object exists and that you spell its name and path name correctly
MORE INFO
I have a MySQL table called WorkOrder (I would like to pull data from here).
What am I missing?
You can only export MS-Access objects (tables or queries) using DoCmd.TransferText. If you have a query called qryOutput you can export it:
DoCmd.TransferText acExportDelim, , "qryOutput", "C:\export.csv", True
You could create the query on runtime (using db.CreateQueryDef), export it and delete it.
If you are working with MySQL, maybe it is easier to export the data directly from the command line:
c:\> mysql -h YourHost -u YourUser -pYourPassword dbYourDatabase -e"SELECT wo_id FROM WorkOrder" > c:\export.txt
This will create a tab-separated text file with the result of your query (including headers).
On a Unix-like command line you could convert this output on-the-fly to a comma separated text file using sed:
$ mysql [connectionParameters] -e"select..." | sed 's/\t/,/g' > export.csv
I don't know if there are any pure-windows command line utilities to do this. I use Cygwin for this kind of things. If you want to use it, be sure to install sed using Cygwin Setup program.
Hope this helps.

How can you repair all tables in all databases from the MySQL command prompt when MYI file is corrupted or missing?

When dealing with MySQL database corruption, if the MYI index file is missing or if its header is corrupted you can't use a myisamchk command:
myisamchk --safe-recover --force --sort_buffer_size=2G --key_buffer_size=2G /var/lib/mysql/*/*.MYI
You have to do the repair from the MySQL command prompt with the use_frm option:
repair tbl_name use_frm;
Per MySQL documentation's on repairing tables
The USE_FRM option is available for use if the .MYI index file is missing or if its header is corrupted. This option tells MySQL not to trust the information in the .MYI file header and to re-create it using information from the .frm file. This kind of repair cannot be done with myisamchk.
With myisamchk, you can easily drop into each database folder and repair every table by using asterisks at the end of command:
/var/lib/mysql/*/*.MYI
You can't do anything similar from the MySQL command prompt.
There's a StackOverflow question with an answer that explains how to repair all tables within one specific database from the MySQL command prompt with a procedure:
CREATE DEFINER = 'root'#'localhost'
PROCEDURE MYDATABASE.repair_all()
BEGIN
DECLARE endloop INT DEFAULT 0;
DECLARE tableName char(100);
DECLARE rCursor CURSOR FOR SELECT `TABLE_NAME` FROM `information_schema`.`TABLES` WHERE `TABLE_SCHEMA`=DATABASE();
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET endloop=1;
OPEN rCursor;
FETCH rCursor INTO tableName;
WHILE endloop = 0 DO
SET #sql = CONCAT("REPAIR TABLE `", tableName, "`");
PREPARE statement FROM #sql;
EXECUTE statement;
FETCH rCursor INTO tableName;
END WHILE;
CLOSE rCursor;
END
Is it possible to modify a procedure like this to loop through all your MySQL databases and repair every table within those databases?
I think this could be useful for anyone who has a large number of databases and runs into serious corruption.
mysqlcheck is a more convenient command-line interface to the MySQL CHECK, REPAIR, ANALYZE and OPTIMIZE statements.
mysqlcheck --repair --use-frm --all-databases
Here's my solution for when I had to fix all of the MyISAM files in my DB:
find ./ -name "*.MYI" -exec myisamchk -r {} \;
It traverses all of the databases.