Select * From eit_InfoGroup[a] Left Join  (Select CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))[s] on [a].[Id]=[s].Aid
关联表是动态的,所以得根据 eit_InfoGroup 中的 ModelID 来获取 表明这样写好像不行啊 ,帮帮忙

解决方案 »

  1.   


    (Select CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))
    --你的意思是上面的语句返回的是表的名字??如果是这样,那么需要动态执行:declare @tablename varchar(20)
    select @tablename=(Select CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))exec('Select * From eit_InfoGroup[a] Left Join '+@tablename+' [s] on [a].[Id]=[s].Aid')
      

  2.   

    能不能不要用 存储过程 一句 SQL 就搞定?
      

  3.   

    try:exec('Select * From eit_InfoGroup[a] Left Join '+
    (Select CustomTable From eit_ContentModel 
    where id = (select ModelID from eit_infogroup where id=34120))
    +' [s] on [a].[Id]=[s].Aid')我那不是存储过程
      

  4.   

    错误提示:列名 'Aid' 无效。其实我那个关联表里 有 Aid 这个列名的
      

  5.   

    红色 部分没有Aid这一列。
    Select * From eit_InfoGroup[a] Left Join (Select CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))[s] on [a].[Id]=[s].Aid
    试试下面代码
    Select * From eit_InfoGroup[a] 
    Left Join (Select id as Aid,CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))[s] 
    on [a].[Id]=[s].Aid
      

  6.   

    楼主啊,你没有给出表结构,我们只能盲人摸象啊,你注意一下,我不清楚你的Aid是哪里来的,所以我直接把
     eit_ContentModel表中的id 作为了Aid,如果你的表eit_ContentModel中有Aid 那么试试底下。
    Select * From eit_InfoGroup[a] 
    Left Join (Select id,Aid,CustomTable From eit_ContentModel where id = (select ModelID from eit_infogroup where id=34120))[s] 
    on [a].[Id]=[s].Aid