declare @fields varchar(max) set @fields=(select name+',' from sys.columns where object_id = object_id(N'p.e_LOG_AFSIP_BLOCK_ALL') and name in ( select ColName from cfg.Dict_Col where TblID=(select TblID from cfg.Dict_Tbl where TblName='p.e_LOG_AFSIP_BLOCK_ALL') ) for xml path('')) set @fields='select '+LEFT(@fields,LEN(@fields)-1)+ ' from p.e_LOG_AFSIP_BLOCK_ALL' print @fields exec (@fields)
执行语句使用exec (sql)