-
Notifications
You must be signed in to change notification settings - Fork 0
/
16496553060588.html
461 lines (300 loc) · 14.9 KB
/
16496553060588.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<!doctype html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>
mysql自定义function问题解决 - MayGodBlessYou
</title>
<link href="atom.xml" rel="alternate" title="MayGodBlessYou" type="application/atom+xml">
<link rel="stylesheet" href="asset/css/foundation.min.css" />
<link rel="stylesheet" href="asset/css/docs.css" />
<script src="asset/js/vendor/modernizr.js"></script>
<script src="asset/js/vendor/jquery.js"></script>
<script src="asset/highlightjs/highlight.pack.js"></script>
<link href="asset/highlightjs/styles/github.css" media="screen, projection" rel="stylesheet" type="text/css">
<script>hljs.initHighlightingOnLoad();</script>
<script type="text/javascript">
function before_search(){
var searchVal = 'site:jackro.cn ' + document.getElementById('search_input').value;
document.getElementById('search_q').value = searchVal;
return true;
}
</script>
</head>
<body class="antialiased hide-extras">
<div class="marketing off-canvas-wrap" data-offcanvas>
<div class="inner-wrap">
<nav class="top-bar docs-bar hide-for-small" data-topbar>
<section class="top-bar-section">
<div class="row">
<div style="position: relative;width:100%;"><div style="position: absolute; width:100%;">
<ul id="main-menu" class="left">
<li id=""><a target="_self" href="index.html">Home</a></li>
<li id=""><a target="_self" href="archives.html">Archives</a></li>
<li id=""><a target="_self" href="about.html">About</a></li>
<li id=""><a target="_self" href="https://github.com/JackRo">Github</a></li>
</ul>
<ul class="right" id="search-wrap">
<li>
<form target="_blank" onsubmit="return before_search();" action="https://google.com/search" method="get">
<input type="hidden" id="search_q" name="q" value="" />
<input tabindex="1" type="search" id="search_input" placeholder="Search"/>
</form>
</li>
</ul>
</div></div>
</div>
</section>
</nav>
<nav class="tab-bar show-for-small">
<a href="javascript:void(0)" class="left-off-canvas-toggle menu-icon">
<span> MayGodBlessYou</span>
</a>
</nav>
<aside class="left-off-canvas-menu">
<ul class="off-canvas-list">
<li><a target="_self" href="index.html">Home</a></li>
<li><a target="_self" href="archives.html">Archives</a></li>
<li><a target="_self" href="about.html">About</a></li>
<li><a target="_self" href="https://github.com/JackRo">Github</a></li>
<li><label>Categories</label></li>
<li><a href="Android.html">Android</a></li>
<li><a href="iOS.html">iOS</a></li>
<li><a href="dotNET.html">dotNET</a></li>
<li><a href="Java.html">Java</a></li>
<li><a href="Git.html">Git</a></li>
<li><a href="DB.html">DB</a></li>
<li><a href="Shell.html">Shell</a></li>
<li><a href="PHP.html">PHP</a></li>
</ul>
</aside>
<a class="exit-off-canvas" href="#"></a>
<section id="main-content" role="main" class="scroll-container">
<script type="text/javascript">
$(function(){
$('#menu_item_index').addClass('is_active');
});
</script>
<div class="row">
<div class="large-8 medium-8 columns">
<div class="markdown-body article-wrap">
<div class="article">
<h1>mysql自定义function问题解决</h1>
<div class="read-more clearfix">
<span class="date">2018/04/03</span>
<span>posted in </span>
<span class="posted-in"><a href='DB.html'>DB</a></span>
<span class="comments">
</span>
</div>
</div><!-- article -->
<div class="article-content">
<p>最近公司生产环境的系统有个需求,需要往mysql数据库的一张表里批量添加数据,业务部门给的数据大致如下:</p>
<span id="more"></span><!-- more -->
<table>
<thead>
<tr>
<th>中心</th>
<th>3.5L箱子(条码)</th>
<th>8L箱子(条码)</th>
<th>15L箱子(条码)</th>
<th>20L箱子(条码)</th>
</tr>
</thead>
<tbody>
<tr>
<td>浙江物流一区</td>
<td>6个(X04571301-X04571306)</td>
<td>8个(X03571301-X03571308)</td>
<td>4个(X02571301-X02571304)</td>
<td>12个(X01571301-X01571312)</td>
</tr>
<tr>
<td>浙江物流二区</td>
<td>24个(X04571401-X04571424)</td>
<td>42个(X03571401-X03571442)</td>
<td>78个(X02571401-X02571478)</td>
<td>36个(X01571401-X01571436)</td>
</tr>
</tbody>
</table>
<p>这样的数据,首先想到的是在我的macOS上连上服务器数据库,然后把数据一条一条insert进去。但是我insert了几十条后,我发现业务部门给的数据累计有700多条。我当然不愿意就这样老老实实的一条一条insert了,因为实在是太耗费时间了。</p>
<p>根据以往的经验,最先想到的是,把表导入到navicat里,然后生成批量sql插入脚本,再传到服务器上去执行。但是,业务部门给的Excel并不是标准的一条一条的数据展示的,而是像上面那样给定箱子条码范围和数量。这样,我还得先给它生成一个标准的Excel,也比较麻烦。</p>
<p>然后我想到用写程序的方式,写循环,连接数据库插入数据。但是本地环境去连接生产环境的数据库是需要走流程申请权限的,可能流程要走个一两天,业务部门又急着要这个数据,这种方法又要摒弃掉了,只能使用下面一种方案了。</p>
<h2><a id="mysql%E8%87%AA%E5%AE%9A%E4%B9%89function%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>mysql自定义function实现批量添加数据</h2>
<h3><a id="mysql%E5%AE%9A%E4%B9%89function%E8%AF%AD%E6%B3%95%E5%A6%82%E4%B8%8B%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>mysql定义function语法如下:</h3>
<pre><code class="language-plain_text">CREATE FUNCTION func_name ( [func_parameter] ) //括号是必须的,参数是可选的
RETURNS type
[ characteristic ...] routine_body
</code></pre>
<ul>
<li>CREATE FUNCTION 用来创建函数的关键字</li>
<li>func_name 表示函数的名称</li>
<li>func_parameters为函数的参数列表,参数列表的形式为:[IN|OUT|INOUT] param_name type
<ol>
<li>IN:表示输入参数</li>
<li>OUT:表示输出参数</li>
<li>INOUT:表示既可以输入也可以输出</li>
<li>param_name:表示参数的名称</li>
<li>type:表示参数的类型,该类型可以是MySQL数据库中的任意类型</li>
</ol>
</li>
<li>RETURNS type:语句表示函数返回数据的类型</li>
<li>characteristic: 指定函数的特性,取值与存储过程的定义相同,可以有以下几种取值方式
<ol>
<li>LANGUAGE SQL: 说明routine_body部分由SQL语句组成,当前系统支持的语言为SQL, SQL是LANGUAGE特性的唯一值</li>
<li>[NOT] DETERMINISTIC: 指明存储过程执行的结果是否正确。DETERMINISTIC表示结果是确定的。每次执行存储过程时,相同的输入会得到相同的输出;<br />
而NOT DETERMINISTIC表示结果是不确定的,相同的输入可能得到不同的输出。如果没有指定任意一个值,默认为NOT DETERMINISTIC</li>
<li>{CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA}: 指明子程序使用SQL语句限制
<ol>
<li>CONTAINS SQL表明子程序包含SQL语句,但是不包含读写数据的语句</li>
<li>NO SQL表明子程序不包含SQL语句</li>
<li>READS SQL DATA说明子程序包含读写数据的语句</li>
<li>MODIFIES SQL DATA表明子程序包含写数据的语句</li>
<li>默认情况下,系统会指定为CONTAINS SQL</li>
</ol>
</li>
<li>SQL SECURITY { DEFINER|INVOKER}: 指明谁有权限来执行。DEFINER表示只有定义存储过程者才能执行;INVOKER表示拥有权限的调用者可以执行。<br />
默认情况下,系统指定为DEFINER</li>
<li>COMMENT 'string': 注释信息,可以用来描述存储过程或者函数</li>
</ol>
</li>
<li>routine_body是SQL代码内容,可以用BEGIN...END来表示SQL代码的开始与结束</li>
</ul>
<h3><a id="%E5%AE%9E%E7%8E%B0%E6%89%B9%E9%87%8F%E6%B7%BB%E5%8A%A0%E6%95%B0%E6%8D%AE%E7%9A%84function%E8%BF%87%E7%A8%8B%E5%A6%82%E4%B8%8B%EF%BC%9A" class="anchor" aria-hidden="true"><span class="octicon octicon-link"></span></a>实现批量添加数据的function过程如下:</h3>
<p>首先,我把服务器数据库这张表的表结构拷到了本地,便于本地调试所写function是否正确。该表结构与所需要插入的数据相关的字段有三个:</p>
<pre><code class="language-plain_text">ybxtm, dept_id, box_size //ybxtm对应箱子条码, dept_id对应上面数据的中心, box_size对应箱子规格(3.5L, 8L, 15L, 20L)
</code></pre>
<p>然后,实现function实现及调用:</p>
<pre><code class="language-sql"> DELIMITER $$ /*定义分隔符,用于在Linux环境下防止function定义的内部分号造成的中途输出*/
CREATE FUNCTION addData(deptId INT, boxSize DOUBLE, ybxtm VARCHAR(20), amount INT)
RETURNS INT DETERMINISTIC /*该参数需要指定,不指定Linux环境下执行时会提示你需要指定function的characteristic*/
BEGIN
DECLARE i INT DEFAULT 1;
DECLARE result INT DEFAULT 1;
DECLARE sampleBoxNum VARCHAR(20) DEFAULT '';
myloop: LOOP
SET sampleBoxNum = ybxtm;
IF i > amount
THEN LEAVE myloop;
END IF;
IF i < 10
THEN SET sampleBoxNum = concat(sampleBoxNum, concat('0', i));
ELSE
SET sampleBoxNum = concat(sampleBoxNum, i);
END IF;
INSERT INTO lx_ybx (ybxtm, dept_id, box_size) VALUES (sampleBoxNum, deptId, boxSize);
SET result = result + 1;
SET i = i + 1;
END LOOP myloop;
RETURN result - 1;
END $$ /*结束时需要挂上分隔符,告诉Linux终端,function定义结束*/
/*调用function*/
SELECT addData(29, 20, 'X025714', 78);
</code></pre>
</div>
<div class="row">
<div class="large-6 columns">
<p class="text-left" style="padding:15px 0px;">
<a href="16496553059675.html"
title="Previous Post: Android 面试展示 iOS debug app 时闪退">« Android 面试展示 iOS debug app 时闪退</a>
</p>
</div>
<div class="large-6 columns">
<p class="text-right" style="padding:15px 0px;">
<a href="16496553059414.html"
title="Next Post: Android Studio 中鼠标悬停 Fetching Documentation 很长时间">Android Studio 中鼠标悬停 Fetching Documentation 很长时间 »</a>
</p>
</div>
</div>
<div class="comments-wrap">
<div class="share-comments">
</div>
</div>
</div><!-- article-wrap -->
</div><!-- large 8 -->
<div class="large-4 medium-4 columns">
<div class="hide-for-small">
<div id="sidebar" class="sidebar">
<div id="site-info" class="site-info">
<div class="site-a-logo"><img src="media/16496656235321/icon.png" /></div>
<h1>MayGodBlessYou</h1>
<div class="site-des"></div>
<div class="social">
<a target="_blank" class="github" target="_blank" href="https://github.com/JackRo" title="GitHub">GitHub</a>
<a target="_blank" class="rss" href="atom.xml" title="RSS">RSS</a>
</div>
</div>
<div id="site-categories" class="side-item ">
<div class="side-header">
<h2>Categories</h2>
</div>
<div class="side-content">
<p class="cat-list">
<a href="Android.html"><strong>Android</strong></a>
<a href="iOS.html"><strong>iOS</strong></a>
<a href="dotNET.html"><strong>dotNET</strong></a>
<a href="Java.html"><strong>Java</strong></a>
<a href="Git.html"><strong>Git</strong></a>
<a href="DB.html"><strong>DB</strong></a>
<a href="Shell.html"><strong>Shell</strong></a>
<a href="PHP.html"><strong>PHP</strong></a>
</p>
</div>
</div>
<div id="site-categories" class="side-item">
<div class="side-header">
<h2>Recent Posts</h2>
</div>
<div class="side-content">
<ul class="posts-list">
<li class="post">
<a href="16496553059675.html">Android 面试展示 iOS debug app 时闪退</a>
</li>
<li class="post">
<a href="16496553060588.html">mysql自定义function问题解决</a>
</li>
<li class="post">
<a href="16496553059414.html">Android Studio 中鼠标悬停 Fetching Documentation 很长时间</a>
</li>
<li class="post">
<a href="16496553060491.html">macOS 中 git 提交操作 .DS_Store 文件引发的问题解决</a>
</li>
<li class="post">
<a href="16496553060993.html">Safari 浏览器设置 Java 支持</a>
</li>
</ul>
</div>
</div>
</div><!-- sidebar -->
</div><!-- hide for small -->
</div><!-- large 4 -->
</div><!-- row -->
<div class="page-bottom clearfix">
<div class="row">
<p class="copyright">Copyright © 2015
Powered by <a target="_blank" href="http://www.mweb.im">MWeb</a>,
Theme used <a target="_blank" href="http://github.com">GitHub CSS</a>.</p>
</div>
</div>
</section>
</div>
</div>
<script src="asset/js/foundation.min.js"></script>
<script>
$(document).foundation();
function fixSidebarHeight(){
var w1 = $('.markdown-body').height();
var w2 = $('#sidebar').height();
if (w1 > w2) { $('#sidebar').height(w1); };
}
$(function(){
fixSidebarHeight();
})
$(window).load(function(){
fixSidebarHeight();
});
</script>
</body>
</html>