반응형

/*******************************************************************************************************************
-- Title : [MSPy] sp_execute_external_script를 활용한 Python Script 실행 예제
-- Reference : www.sqlshack.com
-- Key word : sp_execute_external_script csv pandas dataframe .csv
*******************************************************************************************************************/

■ Scripts

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
 
/**************************************************
-- 기본 사용 개요
**************************************************/ 
 
-- Print the input value
exec sp_execute_external_script 
@language =N'Python',
@script=N'
OutputDataSet = InputDataSet
print("Input data is {0}".format(InputDataSet))'
@input_data_1 = N'SELECT 1 as col';
 
-- Find mean of a given list
execute sp_execute_external_script 
@language = N'Python',
@script = N'
l = [15, 18, 2, 36, 12, 78, 5, 6, 9]
print(sum(l) / float(len(l)))';
 
-- Format operator
exec sp_execute_external_script
@language = N'Python',
@script= N'
print("""jan:{2} feb:{0} mar:{2} Apr:{1} May:{2} Jun:{1} Jul:{2} Aug:{2} Sep:{1} Oct:{2} Nov:{1} Dec:{2}"""
     .format(InputDataSet.AA, InputDataSet.BB, InputDataSet.CC))',
@input_data_1 = N'select 28 as AA, 30 as BB, 31 as CC';
 
-- Using loops and branches
execute sp_execute_external_script 
@language = N'Python',
@script = N'
for i in range(5):
    if i<3 :
        print("i is now:", i*2)';
 
 
/**************************************************
-- Input/Output 처리
**************************************************/
 
-- Create table
USE Tempdb;
go
 
DROP TABLE IF EXISTS dbo.EMP;
go
 
CREATE TABLE [dbo].[EMP]
( [empno] [intNOT NULL
, [ename] [varchar](10NULL
, [job] [varchar](9NULL
, [mgr] [intNULL
, [hiredate] [datetimeNULL
, [sal] float NULL
, [comm] [numeric](72NULL
, [dept] [intNULL
PRIMARY KEY CLUSTERED ([empno])
);
 
-- Insert data
INSERT INTO EMP VALUES
  (1,'Prashanth','ADMIN',6,'12-17-1990',18000,NULL,4)
, (2,'Jayaram','MANAGER',9,'02-02-1998',52000,300,3)
, (3,'thanVitha','SALES I',2,'01-02-1996',25000,500,3);
 
-- Select data
SELECT EMPNO, ENAME, SAL from EMP;
 
-- Output w/o column name
EXECUTE sp_execute_external_script 
@language = N'Python',
@script=N'
OutputDataSet = InputDataSet',
@input_data_1 = N'SELECT empno, ename, sal from EMP';
 
-- Output w/ column name
EXECUTE sp_execute_external_script 
@language = N'Python',
@script=N'
OutputDataSet = InputDataSet',
@input_data_1 = N'SELECT [empno],[ename],sal,0 as Bonus from EMP'
WITH RESULT SETS ((EMPN0 int, ENAME1 varchar(10), SAL2 float, Bonus3 float))
 
-- Output w/ calculation
EXECUTE sp_execute_external_script 
@language = N'Python',
@script=N'
OutputDataSet = InputDataSet
for i in OutputDataSet["sal"]:
    print(i)
    OutputDataSet["Bonus"]=OutputDataSet["sal"]*0.05',
@input_data_1 = N'SELECT [empno],[ename],sal,0 as Bonus from EMP'
WITH RESULT SETS ((EMPNO2 int, ENAME varchar(10), SAL float, Bonus float));
 
 
/**************************************************
-- Textfile 및 Pandas 처리
**************************************************/
 
-- Read Textfile w/ .tsv
EXECUTE sp_execute_external_script  
@language = N'Python',
@script = N'
import csv
with open(''D:\\PyProject\\20171225_MSPython\\python.txt'') as f:
    reader = csv.DictReader(f, delimiter = ''\t'')
    for row in reader:
        print (row)';
 
-- Using Pandas w/ .csv
EXECUTE sp_execute_external_script  
@language = N'Python',  
@script = N'
import pandas as ps
df = ps.read_csv("D:\\PyProject\\20171225_MSPython\\InputServer_1.csv")
print (df)
#Finding the number of rows
print (df.shape)
#Looking at the first row of the data
print (df.head(1))
#Find the average of each statistic
print (df.mean())';
 
 
/**************************************************
-- 통계 칼럼을 통한 평균 찾기
**************************************************/
 
-- Create table and insert data
DROP TABLE IF EXISTS MyData;
 
CREATE TABLE MyData([Col1] INT NOT NULLON [PRIMARY];
INSERT INTO MyData VALUES(1);
INSERT INTO MyData VALUES(10);
INSERT INTO MyData VALUES(100);
GO
 
-- Select all rows of MyData table
SELECT * FROM MyData;
 
-- Find mean of Col1 
EXECUTE sp_execute_external_script  
@language = N'Python',
@script = N'
import pandas as ps
print("*******************************")
OutputDataSet = InputDataSet
print (OutputDataSet)
print (OutputDataSet["col1"].mean())
print("*******************************")
vMeans = OutputDataSet["col1"].mean()
OutputDataSet["col2"] = vMeans',
@input_data_1 = N'SELECT col1, 0 as col2 from Mydata'
WITH RESULT SETS ((col1 int, col2_mean float));
cs


■ Files

InputServer_1.csv

python.txt


 
 
 

반응형

+ Recent posts