使用varchar值作为列名来选择值

时间:2012-01-22 16:01:51

标签: mysql stored-functions

我正在编写一个mysql函数来删除行并在键值对表中进行备份。我能够做大部分但是无法从检索到的列名中选择一个值

     CREATE FUNCTION delete_item (
  item_id_val int,
  cur_usr_id int,
  item_type_val varchar(45),
  current_purpose varchar(45),
  cur_date datetime
) RETURNS INT
BEGIN
  declare col_name varchar(45);
  declare no_of_cols int;
  declare ind int;
  declare c1 cursor for select column_name from information_schema.columns where table_name='tbl_name';
  select count(column_name) from information_schema.columns where table_name='tbl_name' into no_of_cols;
  open c1;
  while no_of_cols > 0 do
    fetch c1 into col_name;
    insert into deleted_data(tbl_name,field_nm,val,activity_date) values('tbl_name',col_name,
    (**select col_name from tbl_name where item_id=item_id_val**),cur_date);
    set no_of_cols = no_of_cols-1;
  end while;
  close c1;
  return 1;

粗体文本(* ed文本)将列名称本身保存在值的位置。我需要在该表中存储该列名的值。需要帮助的人

0 个答案:

没有答案