问题:求 SQL 语句,关于插入记录的数据来自另一个表的查询结果限制:不能使用存储过程详细描述:
有两个表 tableA 和 tableB ,两个表中的字段均为 ID 和 NAME。
现在,向tableA插入一条新记录,
[ID]取值为id1;
[NAME]取值来自于表tableB(取值查询语句为“select [NAME] from [tableB] where [ID]='id1'”)
下面的SQL语句是小弟根据上面的意思写的,语法纯属胡乱蒙出来的:)
INSERT INTO [tableA] ([ID], [NAME]) VALUES ('id1'
, (select [NAME] from [tableB] where [ID]='id1'))请各位朋友给出正确的语句或解决方法,感激不尽!

解决方案 »

  1.   

    insert into tablea (id,name) (select id,name from tableb where id='id1')
      

  2.   

    INSERT INTO [tableA] 
    (
    [ID], 
    [NAME]

    ('id1',
    select [NAME] from [tableB] where [ID]='id1'
    )
      

  3.   

    insert into [tableA] select [ID],[NAME] from [tableB] where [ID]='id1'
      

  4.   

    string sql1 = String.Format ("insert into 表名(列名1,列名2......) values ( '{0}','{1}'......)",字段1,字段2......);
    SqlCommand cmd = new SqlCommand (sql1,sqlConnection);
    try
    {
    sqlConnection.Open ();
    cmd.ExecuteNonQuery ();
    sqlConnection.Close ();
    return true;
    }
    catch 
    {
    return false;
    }
      

  5.   

    INSERT INTO [tableA] 
    (
    [ID], 
    [NAME]

    ('id1',
    select [NAME] from [tableB] where [ID]='id1'
    )应该没问题
      

  6.   

    insert into tableA select [ID],[NAME] from tableB where ID='id1'
      

  7.   

    string strInsert="insert into tableA(id,name) select id,name from tableb where id='id1'"
      

  8.   

    谢谢各位朋友,最符合要求的答案如下:
    INSERT INTO [tableA] ([ID], [NAME])
    (select 'id1', [NAME] from [tableB] where [ID]='id1')
    以上答案来自帖子http://community.csdn.net/Expert/topic/3834/3834560.xml?temp=.3589899中的朋友提供