您可以使用以下代码创建SP-它也支持NULLS。
select 'my_table_name' into @tableName;
/*find column names*/
select GROUP_CONCAT(column_name SEPARATOR ', ') from information_schema.COLUMNS
where table_schema =DATABASE()
and table_name = @tableName
group by table_name
into @columns
;
/*wrap with IFNULL*/
select replace(@columns,',',',IFNULL(') into @selectColumns;
select replace(@selectColumns,',IFNULL(',',\'~NULL~\'),IFNULL(') into @selectColumns;
select concat('IFNULL(',@selectColumns,',\'~NULL~\')') into @selectColumns;
/*RETRIEVE COLUMN DATA FIELDS BY PK*/
SELECT
CONCAT(
'SELECT CONCAT_WS(','''\'\',\'\''',' ,
@selectColumns,
') AS all_columns FROM ',@tableName, ' where id = 5 into @values;'
)
INTO @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
/*Create Insert Statement*/
select CONCAT('insert into ',@tableName,' (' , @columns ,') values (\'',@values,'\')') into @prepared;
/*UNWRAP NULLS*/
select replace(@prepared,'\'~NULL~\'','NULL') as statement;