2010-07-07 93 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

相关问题