Select into a temp table from within a while loop (cursor)

Mike Marks

I'm trying to essentially "union" the result sets of my cursor. Since I can't figure out how to "union" them, I figured, hey, why not store the results into a temp table, and then just select * from that temp table? Problem is, when I run this code, it says that my temp table already exists. I think when I do select into, that tries to create a new temp table each time. How do I insert into a temp table without using select into?

drop table #mikemarks
declare db_cursor CURSOR FOR SELECT jobid from cxxxxxxx.Campaign_Data_Extension_Names
declare @jobid int
open db_cursor
fetch next from db_cursor into @jobid while @@FETCH_STATUS = 0
begin
    select j.jobid as 'JobID', j.subscriberkey as 'EmailAddress', j.createddate as 'EventDate', jc.errorcode as 'ErrorCode', jc.Description as 'ErrorCodeDescription' into #mikemarks
    from jobsubscribererror j with (nolock)
    inner join jobsubscribererrorcode jc on j.errorcodeid = jc.errorcodeid
    where j.jobid = @jobid
    fetch next from db_cursor into @jobid
end
close db_cursor
deallocate db_cursor
Sean Lange

If you need this in a temp table you can do this in a single statement instead of looping. Something like this.

select j.jobid as 'JobID'
    , j.subscriberkey as 'EmailAddress'
    , j.createddate as 'EventDate'
    , jc.errorcode as 'ErrorCode'
    , jc.Description as 'ErrorCodeDescription' 
into #mikemarks
from jobsubscribererror j
inner join jobsubscribererrorcode jc on j.errorcodeid = jc.errorcodeid
join cxxxxxxx.Campaign_Data_Extension_Names n on j.jobid = n.jobid

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

MySQL: Cursor for select from temp table

From Dev

Select as in SQL while selecting from Temp table

From Dev

Temp Table Populate WHILE/Cursor

From Dev

Select from two temp tables into another temp table

From Dev

Select * into Temp Table from Dynamic SQL Results

From Dev

Select into temp table in PostgreSQL?

From Dev

select data from #Temp table after #temp table create in another query in asp.net c#

From Dev

StringIndexOutOfBoundsException error from stringbuilder within a while loop

From Dev

Passing variable from within while loop to popupdiv

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

Get a sum from a temp table into my select query

From Dev

Can I use SELECT from dataframe instead of creating this temp table?

From Dev

while loop within while loop

From Dev

IF statement from a temp table

From Dev

Select Into #Temp From Select Query

From Dev

Select from same table, within IF statement

From Dev

In Sql Server, how do you put value from cursor into temp table?

From Dev

Insert data from a table to a temporary table, and then select from the temp table specific rows

From Dev

Declare a cursor within FOR loop ORACLE

From Dev

Updating columns within a Cursor For Loop

From Dev

Mysql select query with temp table

From Dev

Mysql select query with temp table

From Dev

Add new columns to a SQL Server table within a while loop

From Dev

Slow Performance within While Loop when Updating MYSQL Table

From Dev

Add new columns to a SQL Server table within a while loop

From Dev

create table temp2 select * from temp1 is not taking all the properties of the source table in hive 0.14

From Dev

Using the data from a for loop as a table in the From in a Select

From Dev

Declare a Cursor From a Table as Variable (In the Select-Statement)

From Dev

Select values from table and show it by while?

Related Related

  1. 1

    MySQL: Cursor for select from temp table

  2. 2

    Select as in SQL while selecting from Temp table

  3. 3

    Temp Table Populate WHILE/Cursor

  4. 4

    Select from two temp tables into another temp table

  5. 5

    Select * into Temp Table from Dynamic SQL Results

  6. 6

    Select into temp table in PostgreSQL?

  7. 7

    select data from #Temp table after #temp table create in another query in asp.net c#

  8. 8

    StringIndexOutOfBoundsException error from stringbuilder within a while loop

  9. 9

    Passing variable from within while loop to popupdiv

  10. 10

    Can I use SELECT from dataframe instead of creating this temp table?

  11. 11

    Get a sum from a temp table into my select query

  12. 12

    Can I use SELECT from dataframe instead of creating this temp table?

  13. 13

    while loop within while loop

  14. 14

    IF statement from a temp table

  15. 15

    Select Into #Temp From Select Query

  16. 16

    Select from same table, within IF statement

  17. 17

    In Sql Server, how do you put value from cursor into temp table?

  18. 18

    Insert data from a table to a temporary table, and then select from the temp table specific rows

  19. 19

    Declare a cursor within FOR loop ORACLE

  20. 20

    Updating columns within a Cursor For Loop

  21. 21

    Mysql select query with temp table

  22. 22

    Mysql select query with temp table

  23. 23

    Add new columns to a SQL Server table within a while loop

  24. 24

    Slow Performance within While Loop when Updating MYSQL Table

  25. 25

    Add new columns to a SQL Server table within a while loop

  26. 26

    create table temp2 select * from temp1 is not taking all the properties of the source table in hive 0.14

  27. 27

    Using the data from a for loop as a table in the From in a Select

  28. 28

    Declare a Cursor From a Table as Variable (In the Select-Statement)

  29. 29

    Select values from table and show it by while?

HotTag

Archive