sql遍历表中的每一行 [英] sql loop through each row in a table

查看:124
本文介绍了sql遍历表中的每一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,可以生成股票的买入和卖出信号.我还创建了测试不同信号并为每笔交易提供回报的逻辑.

I've created a program that generates buy and sell signals of stocks. I've also created logic that tests the different signals and provides the return for each trade.

下一步是在很长一段时间内模拟策略及其规则.所有信息都导出到文本文件并导入到 SQL Server 数据库中的表中.我意识到我需要声明一些变量,例如 StartCapitalCurrentCapitalNumberOfPositionsPositionsLeft.其中一列名为 BuyPrice 并指示何时购买以及以哪个价格购买,当发生这种情况时,NumberOfPositions 应减去 1.

The next step is to simulate the strategy and its rules over a long period of time. All information is exported to text files and imported to a table in a SQL Server database. I've realized that I need to declare a number of variables such as StartCapital, CurrentCapital, NumberOfPositions, PositionsLeft. One of the columns is named BuyPrice and indicates when to buy and to which price, when this occurs NumberOfPositions should be subtracted by 1.

SellPrice 列指示何时出售以及以哪个价格出售,当发生这种情况时,NumberOfPositions 需要加一.NumberOfPositions 的最大值应该是 5,最小值应该是 0.想要的结果是看看 CurrentCapital 是如何展开的.

The column SellPrice indicates when to sell and to which price, when this occurs NumberOfPositions needs to be added by one. The maximum of NumberOfPositions should be 5 and minimum 0. The desired result is to see how the CurrentCapital unfolds.

我非常感谢任何输入和某种 SQL 代码来启动表单.

I would very much appreciate any input and some kind of SQL code to start form.

推荐答案

还有另一种循环方式.我已经看到很多答案将计数器加 1.但是,根据我的经验,不能保证您在数据集中的 id 不会有间隙.

There is another way to loop. I've seen a lot of answers increment the counter by 1. However, in my experience, there is no guarantee that your ids in the dataset won't have gaps.

这是我经常使用的解决方案:

Here is a solution that I often use:

declare @idColumn int

select @idColumn = min( TableID ) from Table

while @idColumn is not null
begin
    /*
        Do all the stuff that you need to do
    */
    select @idColumn = min( TableID ) from Table where TableID > @idColumn
end

这篇关于sql遍历表中的每一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆