由于列为动态生成,为了从数据库中直接查找出列名不为
DECLARE @STR VARCHAR(8000)
DECLARE @STR2 VARCHAR(8000)
SELECT @STR=ISNULL(@STR+',','')+NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID('主表名称') AND NAME!='主键'
SELECT @STR2=ISNULL(@STR2+','')+NAME FROM SYSCOLUMNS WHERE ID=OBJECT_ID('辅表名称) AND and Name!='外键'
set @STR='select '+@Str+','+@Str2+' from tb_classroom left join '
set @STR2=' Tb_Class on 主表的主键=辅表对应外键 '
print @STR+@Str2
Set @STR=@STr+@Str2
EXEC(@STR)