Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 890409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:43:42+00:00 2026-05-15T13:43:42+00:00

Say I have this great query in my stored procedure. Select * from Temp

  • 0

Say I have this great query in my stored procedure.

Select * from Temp

How would I store the results of this in the same stored procedure since in the next line I want to go through it in a loop(I don’t know how to do this yet either) and do stuff to it.

I found something like this

 DECLARE total_count INT DEFAULT 0
 SET total_count = 10; 

but it seems like that does not work.

Msg 156, Level 15, State 1, Procedure csp_test, Line 3
Incorrect syntax near the keyword 'DECLARE'.
Msg 155, Level 15, State 2, Procedure csp_test, Line 3
'INT' is not a recognized CURSOR option.

Edit

Ok this is what I go so far. I have no clue what I am doing so I don’t know if this is remotely right.

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go


ALTER PROCEDURE [dbo].[csp_test]  
AS
BEGIN

declare @temp2 table (
    idx int identity(1,1),
    field varchar(max))

insert into @temp2 (field)
Select * from temp


END

So What I think this is doing is it makes some table variable then inserts all my results from temp table into this temp2 table variable. Then I loop through them or something like that?

I don’t if what I have is so far right. I then found this and not sure if this would be the next step

declare @counter int

set @counter = 1

while @counter < (select max(idx) from @temp)
begin
    -- do what you want with the rows here
    set @counter = @counter + 1
end

Temp Table script

USE [test]
GO
/****** Object:  Table [dbo].[temp]    Script Date: 07/06/2010 19:20:34 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[temp](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [temp] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
 CONSTRAINT [PK_temp] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T13:43:42+00:00Added an answer on May 15, 2026 at 1:43 pm

    This is a handy template for creating a temp table, filling with data, then cursor through the data for some reason

    -- create temp table
    CREATE TABLE #tmp (field1 int, field2 varchar(10)) ON [PRIMARY]
    
    -- populate temp table
    insert into #tmp (field1, field2)
    
    select  something1, something2
    from    someTable
    
    -- variables for cursor through temp table
    declare @field1 int
    declare @field2 varchar(10)
    
    -- open cursor
    declare myCursor Cursor for select field1, field2 from #tmp
    open myCursor
    
    -- get 1st row of data
    fetch next from myCursor into @field1, @field2
    
    -- loop through the data
    while @@fetch_status = 0 begin
          -- do sumthin.. data is in @field1 and @field2
          -- get next row
          fetch next from myCursor into @field1, @field2
    end
    
    -- get rid of cursor
    close myCursor
    deallocate myCursor
    
    -- drop temp table
    drop table #tmp
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say I have this $result = mysql_query('SELECT views FROM post ORDER BY views ASC');
Say I have this given XML file: <root> <node>x</node> <node>y</node> <node>a</node> </root> And I
Say i have this PHP code: $FooBar = a string; i then need a
Say I have this simple form: class ContactForm(forms.Form): first_name = forms.CharField(required=True) last_name = forms.CharField(required=True)
Say I have this column returned in a command for Crystal: deposit_no 123 130
Say I have this table: Person table -------------- PersonId Address table ------------ AddressId PersonAddressId
Lets say I have this array, int[] numbers = {1, 3, 4, 9, 2};
Let's say I have this code: if (md5($_POST[$foo['bar']]) == $somemd5) { doSomethingWith(md5($_POST[$foo['bar']]); } I
Let's say I have this model named Product with a field named brand .
So lets say I have this interface: public interface IBox { public void setSize(int

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.