例如:有现成表 A ,用于保存自建的数据库表字段,结构如下
id  tablename  fieldname
现在,新建一个表 B,字段如下
id
title
dsc现在的问题是:如何点一个导入按钮,让 B 的结构插入 A 中结果为:
id   tablename  fieldname
1    B          id
2    B          title
3    B          dsc

解决方案 »

  1.   

    try:insert into A(id,tablename,fieldname)
    select 
        b.colid,a.name,b.name 
    from 
        sysobjects a,syscolumns b 
    where 
        a.id=b.id and object_id('B')=a.id
      

  2.   

    获得表名与字段名关联数据:
    select
        b.colid,a.name,b.name 
    from 
        sysobjects a,syscolumns b 
    where 
        a.id=b.id and object_id('sysobjects')=a.id
    order by 
        b.colid
      

  3.   

    insert into A
    select id = ordinal_position , 
           tablename = 'B' ,
           col_name(object_id('B'),ordinal_position)
    from information_schema.columns
    where table_name = 'B'
      

  4.   

    create table A(id int, tablename varchar(10) , fieldname varchar(10))
    create table B(id int , title varchar(10),[dec] varchar(10))
    goinsert into a
    select id = ordinal_position , 
           tablename = 'B' ,
           col_name(object_id('B'),ordinal_position)
    from information_schema.columns
    where table_name = 'B'select * from Adrop table a , b/*
    id          tablename  fieldname  
    ----------- ---------- ---------- 
    1           B          id
    2           B          title
    3           B          dec(所影响的行数为 3 行)*/
      

  5.   


    insert into A
    select -(b.offset) as id,a.name,b.name 
    from syscolumns b , sysobjects a 
    where a.id=b.id and a.name='b' /**
    id     name                                                                                                                             name                                                                                                                             
    ------ -------------------------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------------------------- 
    1      b                                                                                                                                id
    2      b                                                                                                                                title
    3      b                                                                                                                                dsc(所影响的行数为 3 行)
    **/
      

  6.   

    insert into A
    select id = ordinal_position , 
           tablename = 'B' ,
           col_name(object_id('B'),ordinal_position)
    from information_schema.columns
    where table_name = 'B'