我编写了一个程序,将所有数字从1添加到给定数字(输入3-你得到6,4得到10,等等)。我认为这是一段有趣的小代码,所以我想我会把它变成一个while循环(你开始循环,它会输出所有内容直到一个特定的数字。
问题: 我需要做什么才能将其变成while循环?
我有什么
1 service = build("customsearch", "v1", developerKey="some_key")
anaconda/lib/python2.7/site-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
135 else: # IGNORE
136 pass
--> 137 return wrapped(*args, **kwargs)
138 return positional_wrapper
139
anaconda/lib/python2.7/site-packages/googleapiclient/discovery.pyc in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials)
194 logger.info('URL being requested: GET %s' % requested_url)
195
--> 196 resp, content = http.request(requested_url)
197
198 if resp.status == 404:
anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in request(self, uri, method, body, headers, redirections, connection_type)
1606 content = ""
1607 else:
-> 1608 (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
1609 except Exception, e:
1610 if self.force_exception_to_status_code:
anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in _request(self, conn, host, absolute_uri, request_uri, method, body, headers, redirections, cachekey)
1348 auth.request(method, request_uri, headers, body)
1349
-> 1350 (response, content) = self._conn_request(conn, request_uri, method, body, headers)
1351
1352 if auth:
anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in _conn_request(self, conn, request_uri, method, body, headers)
1270 try:
1271 if hasattr(conn, 'sock') and conn.sock is None:
-> 1272 conn.connect()
1273 conn.request(method, request_uri, body, headers)
1274 except socket.timeout:
anaconda/lib/python2.7/site-packages/httplib2/__init__.pyc in connect(self)
1057 # something else (such as SSL protocol mismatch).
1058 if e.errno == ssl.SSL_ERROR_SSL:
-> 1059 raise SSLHandshakeError(e)
1060 else:
1061 raise
SSLHandshakeError: [Errno 1] _ssl.c:507: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
答案 0 :(得分:1)
这不是一个合理的SQL设计 - 使用tally table代替:
with E1(N) AS (
select N from (values (1),(1),(1),(1),(1),(1),(1),(1),(1),(1) )E1(N)
), --10E+1 or 10 rows
E2(N) as (select 1 from E1 a cross join E1 b), --10E+2 or 100 rows
E4(N) as (select 1 from E2 a cross join E2 b), --10E+4 or 10,000 rows
E8(N) as (select 1 from E4 a cross join E4 b), --10E+8 or 100,000,000 rows
cteTally(N) as (
select top (@InNumber)
row_number() over (order by (select null))
from E8
)
select sum(N)
from cteTally;
Tally表非常有用,在大多数数据库中都存在一个永久计数表,其中包含许多处理该数据库大多数应用程序的行。例如,在支持抵押贷款计算的数据库中,11,000行足以处理30年抵押贷款中每天一行(30 * 366 = 10,980)。
然后使用名为 dbo.Tally 的永久计数表,代码变得简单:
select sum(N)
from dbo.Tally
were N <= @InNumber;
在SQL中使用WHILE循环是一种可怕的代码味道。它们有时可能是必要的,但是在30多年的编程中,我可以依靠一只手的手指我不得不使用它的次数。
<强>更新强>:
当然,作为上面提到的评论员,无论这种实现多么有效,真正的声音设计都会使用封闭形式的公式(在这种情况下为高斯),无论何时知道存在。