Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. how to access temp table field in subquery?

how to access temp table field in subquery?

Scheduled Pinned Locked Moved Database
helpquestiondatabasetutorialcareer
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    joost versteegen
    wrote on last edited by
    #1

    hi, I get an error : The multi-part identifier "e.ent_name" could not be bound. when trying to access a field from a temp table in a sub-query.

    declare @entities table(ent\_id int, ent\_name varchar(50), startDT datetime)
    
    insert into @entities 
    select ent.ent\_id, ent\_name, prod\_job.act\_start\_time\_local
    from job as prod\_job
    inner join ent as ent on prod\_job.run\_ent\_id = ent.parent\_ent\_id
    where prod\_job.wo\_id = 'B.100077779'
    
    select \* from @entities -- so far so good
    
    select \* from @entities as e 
    inner join 
    (
    	select top(1) act\_finish\_time\_local, oper\_id from job as cleaningJob
    	where cleaningJob.oper\_id like '%'+ e.ent\_name +'%' ----> error
    	order by act\_finish\_time\_local desc
    ) 
    as j on j.act\_finish\_time\_local < e.startDT
    

    how can i fix this? Thanks

    V M Richard DeemingR 3 Replies Last reply
    0
    • J joost versteegen

      hi, I get an error : The multi-part identifier "e.ent_name" could not be bound. when trying to access a field from a temp table in a sub-query.

      declare @entities table(ent\_id int, ent\_name varchar(50), startDT datetime)
      
      insert into @entities 
      select ent.ent\_id, ent\_name, prod\_job.act\_start\_time\_local
      from job as prod\_job
      inner join ent as ent on prod\_job.run\_ent\_id = ent.parent\_ent\_id
      where prod\_job.wo\_id = 'B.100077779'
      
      select \* from @entities -- so far so good
      
      select \* from @entities as e 
      inner join 
      (
      	select top(1) act\_finish\_time\_local, oper\_id from job as cleaningJob
      	where cleaningJob.oper\_id like '%'+ e.ent\_name +'%' ----> error
      	order by act\_finish\_time\_local desc
      ) 
      as j on j.act\_finish\_time\_local < e.startDT
      

      how can i fix this? Thanks

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      Have a look at [sql - What is a 'multi-part identifier' and why can't it be bound? - Stack Overflow](https://stackoverflow.com/questions/206558/what-is-a-multi-part-identifier-and-why-cant-it-be-bound/206667)

      1 Reply Last reply
      0
      • J joost versteegen

        hi, I get an error : The multi-part identifier "e.ent_name" could not be bound. when trying to access a field from a temp table in a sub-query.

        declare @entities table(ent\_id int, ent\_name varchar(50), startDT datetime)
        
        insert into @entities 
        select ent.ent\_id, ent\_name, prod\_job.act\_start\_time\_local
        from job as prod\_job
        inner join ent as ent on prod\_job.run\_ent\_id = ent.parent\_ent\_id
        where prod\_job.wo\_id = 'B.100077779'
        
        select \* from @entities -- so far so good
        
        select \* from @entities as e 
        inner join 
        (
        	select top(1) act\_finish\_time\_local, oper\_id from job as cleaningJob
        	where cleaningJob.oper\_id like '%'+ e.ent\_name +'%' ----> error
        	order by act\_finish\_time\_local desc
        ) 
        as j on j.act\_finish\_time\_local < e.startDT
        

        how can i fix this? Thanks

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        Reverse your operation Populate the @Entities table as you are doing change the select query Select from Job inner join on @Entities on - apply the filter here

        Never underestimate the power of human stupidity RAH

        J 1 Reply Last reply
        0
        • M Mycroft Holmes

          Reverse your operation Populate the @Entities table as you are doing change the select query Select from Job inner join on @Entities on - apply the filter here

          Never underestimate the power of human stupidity RAH

          J Offline
          J Offline
          joost versteegen
          wrote on last edited by
          #4

          thank you, i will try it

          1 Reply Last reply
          0
          • J joost versteegen

            hi, I get an error : The multi-part identifier "e.ent_name" could not be bound. when trying to access a field from a temp table in a sub-query.

            declare @entities table(ent\_id int, ent\_name varchar(50), startDT datetime)
            
            insert into @entities 
            select ent.ent\_id, ent\_name, prod\_job.act\_start\_time\_local
            from job as prod\_job
            inner join ent as ent on prod\_job.run\_ent\_id = ent.parent\_ent\_id
            where prod\_job.wo\_id = 'B.100077779'
            
            select \* from @entities -- so far so good
            
            select \* from @entities as e 
            inner join 
            (
            	select top(1) act\_finish\_time\_local, oper\_id from job as cleaningJob
            	where cleaningJob.oper\_id like '%'+ e.ent\_name +'%' ----> error
            	order by act\_finish\_time\_local desc
            ) 
            as j on j.act\_finish\_time\_local < e.startDT
            

            how can i fix this? Thanks

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Try using CROSS APPLY instead: Making OUTER and CROSS APPLY work for you[^]

            select * from @entities as e
            CROSS APPLY
            (
            select top(1) act_finish_time_local, oper_id
            from job as cleaningJob
            where cleaningJob.oper_id like '%'+ e.ent_name +'%'
            and cleaningJob.act_finish_time_local < e.startDT
            order by act_finish_time_local desc
            )
            as j


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups