0

I have tried some solutions from the internet but they are not working for me .

My task is to get the out put of stored procedure into a table.The data is being inserted inside a cursor by loop . I am creating temporary table to store and display the data.

My code:

ALTER procedure [dbo].[usp_Test]
AS
   SET NOCOUNT ON;

   declare @caseId int;
   declare  @CHG_ID int;
   declare @HEAR_ID int;

   SET @CHG_ID = 1
   set @testid = 1;

   DECLARE db_cursor CURSOR FOR 
     SELECT C_CASE_ID
     FROM table1     // tHERE WILL BE MULTIPLE CASEIDS    

   -- here I am trying to delete the temporary table, but it does not work
   IF OBJECT_ID('tempdb..##test_temp_table') IS NOT NULL 
      TRUNCATE TABLE ##test_temp_table
   ELSE
      CREATE TABLE test_temp_table(HEAR_ID int)

   OPEN db_cursor 

   FETCH NEXT FROM db_cursor INTO @caseId 

   WHILE @@FETCH_STATUS = 0   
   BEGIN    
      insert into test_temp_table 
        EXEC STOREDPROCTEST2 @caseId, 1, @HEAR_ID OUTPUT;

      -- LOOP THROUGH THE CURSOR TO GET ALL     CASE IDS
      FETCH NEXT FROM db_cursor INTO @caseId 

      SELECT HEAR_ID FROM test_temp_table;
   END   

   CLOSE db_cursor  
   DEALLOCATE db_cursor;

I have two issues:

  1. I cannot delete the temporary table
  2. I am not seeing any output from the temporary table
1
  • 1
    As a starting observation, you mostly are referring to test_temp_table, which isn't a temporary table no matter what you name it, and despite the fact that you check for a global temp table named ##test_temp_table using that OBJECT_ID() statement. Which are you trying to do? Commented May 14, 2015 at 17:18

1 Answer 1

1

[##test_temp_table] and [test_temp_table] are two different tables. First one is a global temp table, second one is a user table. I believe you want to replace the user table with the global temp table, i.e. replace object [test_temp_table] with [##test_temp_table]. or vice versa. In the end, you have to ensure you are querying the correct table.

Sign up to request clarification or add additional context in comments.

1 Comment

AHiggins ,can you also show how to insert the data into the table from the stored proc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.