Repository navigation
Expand file tree
/
Copy pathfastexportcsv.R
More file actions
90 lines (61 loc) · 2.69 KB
/
Copy pathfastexportcsv.R
File metadata and controls
90 lines (61 loc) · 2.69 KB
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
# Copyright 2021 by Teradata Corporation. All Rights Reserved.
# This sample program demonstrates how to FastExport into a CSV file.
options (warning.length = 8000L)
options (width = 1000)
main <- function () {
con <- DBI::dbConnect (teradatasql::TeradataDriver (), host = "whomooz", user = "guest", password = "please")
tryCatch ({
sTableName <- "FastExportCSV"
tryCatch ({
sRequest <- paste0 ("DROP TABLE ", sTableName)
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
}, error = function (e) {
cat (paste0 ("\nIgnoring ", strsplit (trimws (e), "\n") [[1]][[1]], "\n"))
})
sRequest <- paste0 ("\nCREATE TABLE ", sTableName, " (c1 INTEGER NOT NULL, c2 VARCHAR(10))")
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
tryCatch ({
sInsert <- paste0 ("\nINSERT INTO ", sTableName, " VALUES (?, ?)")
cat (paste0 (sInsert, "\n"))
DBI::dbExecute (con, sInsert, data.frame (
c1 = c (1, 2, 3, 4, 5, 6, 7, 8, 9),
c2 = c (NA, "abc", "def", "mno", NA, "pqr", "uvw", "xyz", NA)
))
csvFileName <- "dataR.csv"
sSelect <- paste0 ("{fn teradata_require_fastexport}{fn teradata_write_csv(", csvFileName, ")}SELECT * FROM ", sTableName, " ORDER BY 1")
cat (paste0 ("\n", sSelect, "\n"))
res <- DBI::dbSendQuery (con, sSelect, immediate = TRUE)
tryCatch ({
cat (paste0 ("\nread ", csvFileName, "\n"))
print (read.csv (file=csvFileName, sep=",", header=TRUE), right = TRUE, row.names = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_warnings}", sSelect)
cat (paste0 ("\n", sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_get_errors}", sSelect)
cat (paste0 ("\n", sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
sRequest <- paste0 ("{fn teradata_nativesql}{fn teradata_logon_sequence_number}", sSelect)
cat (paste0 ("\n", sRequest, "\n"))
print (DBI::dbGetQuery (con, sRequest), right = FALSE)
}, finally = {
cat (paste0 ("\nfile.remove(", csvFileName, ")\n"))
file.remove(csvFileName)
DBI::dbClearResult (res)
}) # end finally
}, finally = {
sRequest <- paste0 ("DROP TABLE ", sTableName)
cat (paste0 (sRequest, "\n"))
DBI::dbExecute (con, sRequest)
}) # end finally
invisible (TRUE)
}, finally = {
DBI::dbDisconnect (con)
}) # end finally
} # end main
withCallingHandlers (main (), error = function (e) {
listStackFrames <- head (tail (sys.calls (), -1), -2) # omit first one and last two
nStackFrameCount <- length (listStackFrames)
cat (paste0 ("[", 1 : nStackFrameCount, "/", nStackFrameCount, "] ", listStackFrames, "\n\n", collapse = ""))
})