2010-07-07 131 views
1

說我在存儲過程中有這個優秀的查詢。如何在存儲過程中存儲來自查詢的結果

Select * from Temp 

我怎麼會這樣在相同的存儲過程的結果存儲在自下一行我想通過它在一個循環(我不知道如何做到這一點還沒有任何)和做的東西到它。

,我發現這樣的事情

DECLARE total_count INT DEFAULT 0 
SET total_count = 10; 

但似乎不起作用。

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. 

編輯

確定這是我走這麼遠。我不知道我在做什麼,所以我不知道這是否遙遠。

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 

所以我認爲這是做的是它使一些表變量,然後將所有我的結果從臨時表插入到這個temp2表變量。然後我通過他們或類似的東西循環?

我不會如果我擁有的是迄今爲止的權利。後來我發現這一點,不知道這將是下一步

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 

臨時表腳本

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 

回答

0

這是創建一個臨時表,填充數據,然後通過數據光標因某種原因

-- 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 
2
--Variable table @table 
declare @table as Table (int i, ...) 

insert into @table 
Select * from Temp 

--Temporary table #temp 
create table #temp (int i, ...) 

insert into #table 
Select * from Temp 

--Use it 

--Finally 
drop table #temp 

,你發現了什麼應該是:

DECLARE @total_count INT DEFAULT 0 
SET @total_count = 10; 

變量始於@

有關差異的信息,我發現此articlestackoverflow question

+0

約翰寫一個方便的模板:您需要申報表,並保存爲INT,你還可以將數據檢查這個http://odetocode.com/code/365.aspx – 2010-07-07 02:07:10

+0

所以我必須做一些內部表然後擺脫它?你的第一個方法是不要丟棄它。它會自動下降嗎?如果在其他表中存在,我如何遍歷結果? – chobo2 2010-07-07 02:07:11

+0

表變量不需要刪除或刪除。如果在同一個存儲過程中創建它們,它們的資源會自動清理(在sql2008中,它們可以作爲參數傳遞,而我不知道它是如何處理這裏的資源的)。循環聲明一個CURSOR就像這樣。 DECLARE cursor_name CURSOR FOR select_statement_using_temporary_table - 完整的參考在這裏:http://technet.microsoft.com/es-es/library/ms180169.aspx – user347594 2010-07-07 02:11:55