R: run multiple line sql from text file - sql

In R, how can I import the contents of a multiline text file (containing SQL) into multiple lines of SQL?
I've studied Import multiline SQL query to single string and managed to get a simple sql script working. However, when SQL demands a new line (ie when you add a SELECT statement) it doesn't work when you put all lines of sql script in one line.
The sql .txt file looks like:
CREATE TABLE #Countries (Country varchar(255), Region varchar(255))
INSERT INTO #Countries VALUES ('China', 'EM')
SELECT * FROM #Countries
The R code looks like:
fileconn<-file("R/sql.txt","r")
sqlString<-readLines(fileconn)
sqlString<-paste(sqlString,collapse="","")
sqlconn <- odbcDriverConnect(connection = ....)
sqlQuery(sqlconn,sqlString)
I've tried CAT and GSUB as well, but I've got the feeling that the problem occors when the third statement follows the second in one line.
Can anyone help me with this problem? Many thanks.

There are two ways of separating SQL commands. Either you send them separately. This is what you would get by just executing each line of the file in a for loop, but then of course you got the problem what to do if a single command does require more than one line, right? The second way to separate SQL commands is simply to end them with a ;. If you put that at the end of each command, you should be able to pass as many of them as you like to the DB in a single string.

Related

SPSS Insert Value from Dataset into SQL Query

I have an SQL query written into a GET DATA command. It works perfectly as it is, but now I would like to modify it to take in a string value from my current dataset.
Sample query -
SELECT * FROM TABLE WHERE field IN ('AAA', 'BBB', 'CCC', 'DDD')
I would like the bracketed list to be a string value that already exists in my dataset (ref - previous post)
I have tried several things to date including using a concatenated string in the /SQL portion of the GET DATA command. For the concatenation I tried referencing the field itself and a scratch variable.
I've also attempted to call the command via a macro using the variable as an input. Unfortunately, the SQL string only goes through with the variable name rather than its contents.
I've seen a few posts that recommend using the WRITE OUT command for similar use cases but haven't found anything regarding concatenating into an SQL query. Would this be the only option for me?
Looks to me like write out is a good option for you.
Assuming you have one row of data in which myconcat has the value "'AAA', 'BBB', 'CCC', 'DDD'" (if you have more then one row in the data, first select only the one):
select if (the row with the full concatenated list) .
string cmd (a2000).
compute cmd=concat("define !mysqlcommand() 'SELECT * FROM TABLE WHERE field IN (", rtrim(myconcat), ")' !enddefine.").
write out = "path\create val list macro.sps" /cmd.
execute.
insert file = "path\create val list macro.sps".
The code writes a new syntax file where it creates a macro call. After the insert command runs this, you theoretically have a macro defined so when you use the macro call !mysqlcommand in syntax it will automatically write in the full SQL command you've created, so:
.
.
/sql = !mysqlcommand
.
.

SQL query executed through Windows batch: missing table

I execute a SQL query (for PostgreSQL) via psql.exe inside a Windows batch. I get an error I can't explain, saying that a FROM clause is missing for a table that is not called within the query (see below). When I search in the batch file for geo_c3_0_3_mo table, the string is not found...
Any idea on this kind of issue?
EDIT :
If I copy-paste the query from the batch file into a pgAdminIII SQL query window, the query runs perfectly and no error message is returned.
When I remove one of the subqueries, the error either disappear or mention another badly written table name (for instance: missing FROM-clause for table "geoc__0_3_mo")... It seems more and more that the issue comes from the length of the line (19,413 characters!). To me, it is not possible to write the query on several lines within a batch file, like inside a pgAdminIII SQL query window. The solution would be to keep the query inside a *.sql file and to call that file from the batch file.
Write the query to a tempfile in your batch, then execute it with psql -f. This will bypass command-line length issues.

How can I link values in a text file to values in a SQL Server table?

UPDATE 2
Essentially, I'd like to pass the dataTable into SQL Server, but not actually "create it", just like a temporary thing using:
cmd.CommandText = "SELECT *
FROM table1
INNER JOIN dataTable
ON sample.name = dataTable.name"
How can I pass the dataTable from vb.net (.NET 2.0) to SQL Server in a similar fashion?
UPDATE 1
So I'm thinking maybe passing the data from the text file into a datatable and using that to compare against the SQL Server table? How would I go about doing that if at all possible?
ORIGINAL POST
I have a table in SQL Server 2012 (i.e., dbo.sample1) within the table there is a column that contains names (i.e., abc01, abc02, abc03, hijk01, hijk02...)
I ran some vb code to extract certain file names without extension from a directory on my machine (i.e., abc01, abc02...) that met certain conditions, these file names were saved on separate lines within a text file.
Is there any easier convenient way of linking ONLY the names on my text file to the ones on my table so as not to show any rows that are not on the text file? I figured I can sit and plug in a bunch of WHERE name = 'abc01'... but didn't really want to site there and do that for all of the names I have. But I'm not sure is this might even work correctly as I need to do an INNER JOIN on of 2 tables in the DB to the values in the text file.
If this is a long problem, then please point me in the right direction and I can research and move forward with it, but any help is greatly appreciated, thanks!
If the list of filenames is reasonable you can us the IN() clause:
SELECT * from XXX WHERE name IN ('abc01', 'abc02', 'abc03',...)
If the list is too long look into bulk copy up to a temp table to use with a JOIN.

Regex to split SQL script but ignore splitting GO under commented script

I'm trying to parse a sort of big SQL script containing commands like create functions and store procedures. I want to split the file in strings whenever I find a GO statement (I want to execute it using ordinary ado.net instead of SMO).
The problem is that I haven't find a suitable regular expression for that so far. Using a simple \bGO\b ignoring case will split it. But will also split all go(s) inside a comment like
/*****************************\
sql statement 1
GO
sql statement 2
GO
\****************************/
My requirement is : Do not split the script if it is under comment even though the script contains GO statement. Suppose my entire script is as below :
sql statement 1
GO
/*****************************\
sql statement 2
GO
sql statement 3
GO
\****************************/
sql statement 4
Expected output should be like
First command :
sql statement 1
Second command :
/*****************************\
sql statement 2
GO
sql statement 3
GO
\****************************/
sql statement 4
Have any idea on this ?
Thanks in advance. :)
You can remove all the comments and then split by GO:
/\/\**\\[^\\]*?\\\**\// # match the comment
\/\**\\ # matches /*****\
[^\\]*? # any text within the comments
\\\**\/ # matches \*****/
Remove above finds and split the result by GO.

Pentaho kettle : how to execute "insert into ... select from" with the sql script step?

I am discovering Pentaho DI and i am stuck with this problem :
I want to insert data from a csv file to a custom DB, which does not support the "insert table" step. So i would like to use the sql script step, with one request :
INSERT INTO myTable
SELECT * FROM myInput
And my transformation would like this :
I don't know how to get all my data from the csv to be injected in the "myInput" field.
Could someone help me ?
Thanks a lot :)
When you first edit the SQL Script step, click 'Get fields' button. This is going to load the parameters(fields from your csv) into box on the bottom left corner. Delete the parameters(fields) you don't want to insert.
In your sql script write your query something like this where the question marks are your parameters in order.
insert into my_table (field1,field2,field3...) values ('?','?','?'...);
Mark the checkboxes execute for each row and execute as a single statement. That's really about it. Let me know if you have any more questions and if you provide sample data I'll make you a sample ktr file to look at.
I think you get the wrong way. You should get a cvs file input step and a table output step.
As rwilliams said, In cvs file input step Get fields; the more important, in table output there is a Database Fields tab. Enter field mapping is right choise.The guess function is amazing.
And more, system can generate target table create sql statement when target table not exists in target connection Db server.
Use the following code
with cte as
(
SELECT * FROM myInput
)
select *
into myTable
from cte;