0

I am creating two table and trying to do select * into using sql and dynamic sql . I am not sure why the same query in dynamic sql is throwing me error . Can you please help

Note: I know I can use create table and insert into to make it work. But my question is why below is not working with dynamic sql

create table emp_info (emp_id int,sal float) 
create table emp_sal(emp_id int,sal float)

insert into emp_info values (1,1000)
insert into emp_info values (2,1000)
insert into emp_sal values (1,1000)
insert into emp_sal values (2,1000)

BEGIN   
   SELECT A.emp_id,A.sal  into  #EE_tmp
   FROM emp_info A, emp_sal B   
   WHERE A.emp_id = 1   AND A.emp_id = B.emp_id   
   select * from  #EE_tmp 
 END 

Result: emp_id sal 1 1,000

BEGIN
    DECLARE @dsql varchar(10000)
    SET @dsql = ' SELECT A.emp_id,A.sal  into  #EE_tmp'
                 + ' FROM emp_info A, emp_sal B'
                 + '  WHERE A.emp_id = 1'
                 +'  AND A.emp_id = B.emp_id'    
                 EXECUTE(@dsql)      
                 select * from #EE_tmp
 END

Result:

Error: #EE_tmp not found. Specify owner.objectname or use sp_help to check whether the object exists (sp_help may produce lots of output).

ughai
  • 9,830
  • 3
  • 29
  • 47

0 Answers0