我需要创建一个脚本文件,其中包含我在mysql表中选择的记录的Insert语句。
对于Instance,当我Select * from emp where empid = 5
时,说我得到了10条记录。
现在我应该创建一个脚本文件,其中包含10条记录的插入语句到emp表。
我使用C#作为代码...
如何?
答案 0 :(得分:0)
那么现在你的问题是有道理的。 您可以在一个查询中执行此操作
INSERT INTO B.emp SELECT * FROM A.emp WHERE empid = 5
答案 1 :(得分:0)
DELIMITER $$
DROP PROCEDURE IF EXISTS `sample`.`InsGen` $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsGen`(
in_db varchar(20),
in_table varchar(20),
in_ColumnName varchar(20),
in_ColumnValue varchar(20)
)
BEGIN
declare Whrs varchar(500);
declare Sels varchar(500);
declare Inserts varchar(200);
declare tablename varchar(20);
declare ColName varchar(20);
set tablename=in_table;
# Comma separated column names - used for Select
select group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')'))
INTO @Sels from information_schema.columns where table_schema=in_db and table_name=tablename;
# Comma separated column names - used for Group By
select group_concat('`',column_name,'`')
INTO @Whrs from information_schema.columns where table_schema=in_db and table_name=tablename;
#Main Select Statement for fetching comma separated table values
set @Inserts=concat("select concat('insert IGNORE into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');')
as MyColumn from ", in_db,".",tablename, " where ", in_ColumnName, " = " , in_ColumnValue, " group by ",@Whrs, ";");
PREPARE Inserts FROM @Inserts;
EXECUTE Inserts;
END $$
DELIMITER ;
答案 2 :(得分:0)
它适用于Norhtwind.Customer表。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using System.Data.Odbc;
using System.IO;
using System.Threading;
namespace DataAccessLayer
{
public class DBManager
{
//FILE LOGGER METHOD
public void SqlLogger(string SqlText)
{ if (!File.Exists("SQL.txt"))
{ using (StreamWriter sw = new StreamWriter("SQL.txt"))
{ sw.Flush();
sw.Close();
sw.Dispose();
}
}
using (StreamWriter sw = File.AppendText("SQL.txt"))
{ sw.WriteLine(SqlText);
sw.WriteLine(" ");
sw.Flush();
sw.Close();
sw.Dispose();
}
}
// DATABASE METHODS
public static OdbcConnection g_con = new OdbcConnection("Dsn=wsodbc1");
public DBManager()
{
}
public OdbcConnection GetConnection()
{ try { g_con.Open(); }
catch (Exception ex) { }
return g_con;
}
public OdbcCommand GetCommand(string Query)
{
OdbcCommand cmd = new OdbcCommand(Query);
cmd.Connection = GetConnection();
return cmd;
}
public void ExeCommand(string Query)
{
OdbcCommand cmd = GetCommand(Query);
cmd.ExecuteNonQuery();
}
public OdbcDataReader GetReader(string Query)
{ return GetCommand(Query).ExecuteReader(); }
// SQL INSERT GENERATOR
public void GenerateSql(string tablename)
{
File.Delete("SQL.txt");
List<string> cols = new List<string>();
string ColNames;
OdbcDataReader reader = GetReader("SELECT * FROM Customers ");
DataTable schemaTable = reader.GetSchemaTable();
foreach (DataRow row in schemaTable.Rows)
{ foreach (DataColumn column in schemaTable.Columns)
{
if (column.ColumnName.Equals("ColumnName")) cols.Add(row[column].ToString().ToUpper());
}
}
ColNames = string.Join(",", cols.ToArray());
while (reader.Read())
{
SqlLogger("INSERT INTO " + tablename + " (" + ColNames + ") VALUES (" + Row_Values(reader) + ")");
}
}
public string Row_Values(OdbcDataReader r)
{
List<string> colsVals = new List<string>();
for (int i = 0; i < r.FieldCount; i++ )
{
if (r[i].GetType().ToString().Equals("System.String"))
{
if (r[i] == null)
{
colsVals.Add("NULL");
}
else
{
colsVals.Add("'" + r[i].ToString().Replace("'","''").Replace(",","-") + "'");
}
}
else if (r[i].GetType().ToString().Equals("System.DBNull"))
{
colsVals.Add("NULL");
}
else
{
if (r[i] == null)
{
colsVals.Add("NULL");
}
else
{
var VAL = r[i].ToString();
colsVals.Add("'" + VAL.ToString().Replace("'", "''").Replace(",", "-") + "'");
}
}
}
return string.Join(",", colsVals.ToArray());
}
}
}