多个基表不支持动态 sql 生成

dynamic sql generation is not supported against multiple base tables(多个基表不支持动态 sql 生成)

本文介绍了多个基表不支持动态 sql 生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试向 SQL DB 中的表添加新行,但遇到了问题:

I tried to add a new row to a Table in an SQL DB, but I had a problem :

不支持对多个基表进行动态 sql 生成

dynamic sql generation is not supported against multiple base tables

这是我尝试过的代码:

private MyClass myClass = new MyClass();
private SqlDataAdapter adapter;
private SqlDataAdapter adapter2;

private void GestionCollections_Load(object sender, EventArgs e)
{
     adapter = new SqlDataAdapter("select Id_Collection ID, Libelle_Collection Collection,Libelle_Editeur Editeur from Collection_ left join Editeur on Id_Editeur = Collection_.Id_Editeur_Editeur", myClass.cnx);
     adapter.Fill(myClass.ds, "Collection_");

     adapter2 = new SqlDataAdapter("Select Id_Editeur ID,Libelle_Editeur Editeur from Editeur", myClass.cnx);
     adapter2.Fill(myClass.ds, "Editeur");
}

private void AjouterBarButton_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
    String newKeyWordCollectionName = ajoutCollection.KeyWordCollectionName;
    String newKeyWordAEditeurName = ajoutCollection.KeyWordEditeurName;        
    DataRow row = myClass.ds.Tables["Collection_"].NewRow();
    row[1] = newKeyWordCollectionName;

    foreach(var myRow in myClass.ds.Tables["Editeur"].AsEnumerable())
    {
         if (newKeyWordAEditeurName == myRow[1] as String)
              row[2] = (int)myRow[0];
    }
     myClass.ds.Tables["Collection_"].Rows.Add(row);
     SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
     adapter.Update(myClass.ds, "Collection_");

}

推荐答案

更改您的选择查询并使用内部联接添加 distinct.

Change your select query and add distinct with inner join.

例如有两个查询,您可以从中了解我想告诉您的内容

For example there are two query from which you can understand that what I want to tell you

查询错误

select iop.pob_id, iop.pob_product_id, iop.pob_qty, iop.pob_unit_id
    , iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
    , **p.product_desc** as orderBy from inv_product_open_balc iop
left join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3

正确查询

select distinct iop.pob_id, iop.pob_product_id, iop.pob_qty
    , iop.pob_unit_id, iop.pob_rate, iop.pob_value, iop.pob_fiscalyear_id
    , **(select Product_desc from** inv_product p where p.product_id = iop.pob_product_id )as orderBy
from inv_product_open_balc iop
inner join inv_product p on iop.pob_product_id = p.product_id
where p.product_desc like 'Air Freshner%' and iop.pob_fiscalyear_id = 3

这篇关于多个基表不支持动态 sql 生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:多个基表不支持动态 sql 生成

基础教程推荐