SQL Server 游标数据作为存储过程的结果

发布于 2024-08-27 19:34:46 字数 412 浏览 9 评论 0原文

我有一个存储过程

DECLARE cursor FOR SELECT [FooData] From [FooTable];
OPEN cursor ;  
FETCH NEXT FROM cursor INTO @CurrFooData;
WHILE @@FETCH_STATUS = 0
BEGIN
  SELECT @CurrFooData AS FooData;
  INSERT INTO Bar (BarData) VALUES(@CurrFooData);
  FETCH NEXT FROM cursor INTO @CurrFooData;
END;
CLOSE cursor 
DEALLOCATE cursor 

但结果我有很多表,而不是一个。如何返回一张包含“FooData”列和所有“@CurrFooData”行的表?

I have a stored procedure

DECLARE cursor FOR SELECT [FooData] From [FooTable];
OPEN cursor ;  
FETCH NEXT FROM cursor INTO @CurrFooData;
WHILE @@FETCH_STATUS = 0
BEGIN
  SELECT @CurrFooData AS FooData;
  INSERT INTO Bar (BarData) VALUES(@CurrFooData);
  FETCH NEXT FROM cursor INTO @CurrFooData;
END;
CLOSE cursor 
DEALLOCATE cursor 

But in result I have a lot of tables, not one. How can I return one table with 'FooData' column and all '@CurrFooData' rows?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

毅然前行 2024-09-03 19:34:46

德米特里,我认为你真的应该尝试一起摆脱那个光标。在第二个示例中,您选择了两个字段 FooData1FooData2,但最终,您只插入了值......

您可以重写第二个查询很容易 - 就像:

INSERT INTO Bar (BarData) 
    SELECT FooData1 FROM FooTable

这就是你的整个光标和所有事情真正在做的事情。

尽量避免使用游标 - 99% 的时间,它们是不需要的 - 开始考虑数据集 - 不要坚持 SQL 中的过程循环和游标 - 这不是一个很好的匹配!

如果您需要输出刚刚插入的内容,请使用 OUTPUT 子句:

INSERT INTO Bar (BarData) 
    OUTPUT Inserted.* 
    SELECT FooData1 FROM FooTable

Dmitry, I think you should really try to get rid of that cursor all together. In the second example, you're selecting two fields FooData1 and FooData2, but in the end, you're only ever inserting of the values....

You could rewrite that second query easily - something like:

INSERT INTO Bar (BarData) 
    SELECT FooData1 FROM FooTable

That's all that your entire cursor and everything is really doing.

Try to avoid cursors at all costs - 99% of the time, they're not needed - start to think in SETS of data - do not stick to procedural loops and cursor in SQL - that's just not a good match!

If you need to output what you've just inserted, use the OUTPUT clause:

INSERT INTO Bar (BarData) 
    OUTPUT Inserted.* 
    SELECT FooData1 FROM FooTable
岁月染过的梦 2024-09-03 19:34:46

是不是要输出刚刚插入的数据?如果是这种情况,并且您使用的是 SQL Server 2005 之前的 SQL Server 版本,那么您可以将所需的值填充到临时表中,如下所示:

Create Table #FooTemp ( FooData ... )

Insert #FooTemp( FooData )
Select FooData
From FooTable

Insert Bar( BarData )
Select FooData
From #FooTemp

Select FooData
From #FooTemp

这种方法的缺点是由于临时表的原因,它可能会导致每次运行存储过程时重新编译。

如果您使用的是 SQL Server 2000+,您可以仅在表变量中执行与上面相同的操作:

Declare @FooTemp Table ( FooData ... )

Insert @FooTemp( FooData )
Select FooData
From FooTable

Insert Bar( BarData )
Select FooData
From @FooTemp

Select FooData
From @FooTemp

如果您使用的是 SQL Server 2005+,您可以使用 OUTPUT 子句,如下所示:

Insert Bar( BarData )
 OUTPUT inserted.BarData
Select FooData
From FooTable

Is it that you want to output the data you just inserted? If that is the case, and if you are using a version of SQL Server prior to SQL Server 2005, then you can stuff the values you want into a temp table like so:

Create Table #FooTemp ( FooData ... )

Insert #FooTemp( FooData )
Select FooData
From FooTable

Insert Bar( BarData )
Select FooData
From #FooTemp

Select FooData
From #FooTemp

The downside to this approach is that it will likely cause a recompile on your stored procedure each time it is run because of the temp table.

If you are using SQL Server 2000+ you could do the same as above only in a table variable:

Declare @FooTemp Table ( FooData ... )

Insert @FooTemp( FooData )
Select FooData
From FooTable

Insert Bar( BarData )
Select FooData
From @FooTemp

Select FooData
From @FooTemp

If you are using SQL Server 2005+, you can use the OUTPUT clause like so:

Insert Bar( BarData )
 OUTPUT inserted.BarData
Select FooData
From FooTable
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文