Store Powershell result in SQL table - sql

Since I am new to Powershell I am looking for some assistance for my problem.
What I need is to count the number of files in a network folder and use that number for a Bulk-Insert loop. Currently, the number of loop cyles is stored in a "status-table" (in SQL). I want to be able to update that status table with the actual number of files.
The powershell cmdlet to count the number of files looks like this:
$FileCount = #(Get-ChildItem -Path '\\YS001UVE\Download\MIS\MIS FactData 20??m?1.csv' -Name -File | sort Name).count
But then I need to update the existing "status-table" with the outcome of the Powershell script .
What is the correct SQL coding for this?
Thanks in advance and regards
Peter

There are many ways to do that. This Post contains an example to do this task. For some database connections you may need to install something on your computer (for MySQL you need the MySQL Connector).
I've done this myself and know, how confusing it can be at first. Under this link, you can find my script (sorry for the German comments and text). The database connection starts at about line 270 in the function Import-ToDatabase. Be aware that I am not updating one single line in my script, but a whole new table.
Let me know if you need some more help
Here are a few more links:
https://www.joseespitia.com/2016/06/03/connecting-and-writing-to-a-sql-db-with-powershell/
https://www.sqlservercentral.com/scripts/insert-data-into-a-sql-server-table-using-powershell-using-invoke-sqlc
https://www.sqlservercentral.com/blogs/performing-an-insert-from-a-powershell-script

Related

How do you write queries in SQL?

I am currently taking CS50, an online introductory course in coding by Harvard. We have just covered SQL and I am trying to attempt the question "Movies" in the problem set now, a description of which can be found here: https://cs50.harvard.edu/x/2020/psets/7/movies/
However, I am not sure how to do this correctly.
For example, for 1.sql, my code is as follows:
SELECT title FROM movies WHERE year = 2008;
I literally wrote only that one line and nothing more in the file "1.sql".
But when I run
$ cat 1.sql | sqlite3 movies.db
in my terminal, nothing happens?
Is this how I am supposed to write code for SQL? Or am I missing some stuff that I should be including (e.g. as headers or what) above my query?
To be clear, I believe I know how to write a query itself but I do not know the "protocol" to write it, if I may. I mean, for example, I am positively sure that "SELECT title FROM movies WHERE year = 2008;" fulfils the question's first requirement.
Some enlightenment would be appreciated!
EDIT 1
Okay first I must apologise to everyone who so very kindly took the time to comment on my post. For some very odd reason, my query did not return any results the first time I ran it. However, when I tried it again, it worked perfectly! Not sure what went wrong honestly, but all is well now! So sorry for wasting everyone's time ):
EDIT 2
Okay I also figured out that the reason why I could not execute my query is that I was in "sqlite3" in my terminal. I was supposed to run the command to execute my query in the main terminal i.e. not when it says "sqlite3". Stupid. I know.
Option 1) you can run "$ cat 1.sql | sqlite3 movies.db" in your normal terminal (not in the sqlite3 mode)
Option 2) you can open sql environment by typing "sqlite3 movies.db" (it will open movies.db if it exists, or create a temporary one if it doesn't exist). Then you can type ".read 1.sql" after the "sqlite>"
Open Browser and test it as mention on your course site
Usage
To test your queries on CS50 IDE, you can query the database by running
$ cat filename.sql | sqlite3 movies.db
where filename.sql is the file containing your SQL query.
Or you can paste them into DB Browser for SQLite’s Execute SQL tab and click ▶.

Newbie: What is the easiest way to bulk add data to a sqlite database?

I'm a total newbie to SQL, and I'd like to know whether anyone knows a means for easily "copy and pasting" hundreds of entries to a sqlite database. Again, I'm not a professional programmer, so software that could automate that process would be great. (I primarily code in JavaScript, but SQL code can be used as well if you could kindly explain the code.)
Essentially, the text I'd be adding would be delimited by a character (the '|' character in my case) for the columns, and line breaks for the rows. It would be added onto a table that's already being used in the database, with columns already set up.
Thanks a lot!! Any suggestions are most appreciated!
You can use DB Browser for SQLite and then File > Import > From CSV file.. after creating a New Database.

how to execute stored procedure or query via batch file and send the results in email via batch file

I can execute the query via batch file and send it to the folder but I need to send the results to an email and I am not sure how to send the sql query/sp results in an email via batch file.
Any suggestions and help is appreciated.
Thanks
It sounds like you're executing your SQL query and I'm guess dumping it to a text file (let's say output.txt). I'm assuming Windows and I'm assuming SQL Server since you didn't specify. The answer is below, but I would like to offer two options instead:
OPTION 1: Setup SQL Server to send your email which would have several benefits including:
SQL Agency can control scheduling & logging
You get a SQL-Only solution
Email is centrally managed instead of being in a bunch of different batch files.
It's not hard to setup email in SQL and is clearly explained here:
How to send email from SQL Server?
OPTION 2: Use PowerShell to make your SQL call.
The answer to your question (below) uses PowerShell to send the email message. Since you'll use PS for that you might as well use PS to make your database call. You can find hundreds of examples of how to call SQL from PS.
Finally the answer you asked for...
If you want to continue to use a command line solution (maybe you have to kick off a bunch of non-SQL commands) you'll need to use PowerShell (assuming you don't want start making things complicated, like writing your own .exe).
STEP 1: Read the contents of the query (output.txt) by using:
$sqlOutput = Get-Content Output.txt -Raw
STEP 2: Use $sqlOutput as the body of the email. Here's a nice walkthrough:
http://www.howtogeek.com/120011/stupid-geek-tricks-how-to-send-email-from-the-command-line-in-windows-without-extra-software/
STEP 3: If you want to attach the file as an attachment, you can use the example here:
https://msdn.microsoft.com/en-us/library/system.net.mail.attachment(v=vs.110).aspx

How do I programmatically run a complex query on an as400?

I'm new at working on an as400 and I have a query the joins across 4 tables. The query itself is fine, it runs in STRSQL and displays the results.
What I am in struggling with is getting the query to be able to run programmatically (it will eventually be run from a scheduled CL script).
I tried have creating a physical file that contains the query running it with RUNQRY, but it simply displays the query itself, not the actual result set.
Does anyone know what I am doing wrong?
UPDATE
Thanks everyone for the direction and the resources, with them I was able to reach my goal. In case it helps anyone, this is what I ended up doing (all of this was done in it's own library, ALLOCATE):
Created a source physical file (using CRTSRCPF): QSQLSRC, and created a member named SQLLEAGSEA, with the type of TXT, that contains the SQL statement.
Created another source physical file: QCLSRC, and created a member named POPLEAGSEA, with the type of CLP, that changes the current library to ALLOCATE then runs the query using RUNSQLSTM (more detail on this below). Here is the actual command:
RUNSQLSTM SRCFILE(QSQLSRC) SRCMBR(SQLLEAGSEA) COMMIT(*NONE) NAMING(*SYS)
Added the CLP to the scheduled jobs (using ADDJOBSCDE), running the following command:
CALL PGM(ALLOCATE/POPLEAGSEA)
With regard to RUNSQLSTM, my research indicated that I wasn't going to be able to use this function, because it didn't support SELECT statements. What I didn't indicate in my question was what I needed to do with the the result - I was going to be inserting the resultant data into another table (had I done that I'm sure the help could have figured that out a lot quicker). So effectively, I wasn't going to be doing an SELECT, my end result is actually an INSERT. So my SQL statement (in SQLLEAGSEA) begins with:
INSERT INTO
ALLOCATE/LEAGSEAS
SELECT
...
BLAH BLAH BLAH
...
From my research, I gather that RUNSQLSTM doesn't support SELECT because it doesn't have a mechanism to do anything with the results. Once I stopped taking baby steps and realized I needed to SELECT AND INSERT in the same statement, it solved my main problem.
Thanks again everyone!
The command is RUNSQLSTM to run a static SQL statement in a physical file member or stream file.
It is a non-interactive command so it will not execute sql statements that attempt to return a result set.
If you want more control, including the ability to run interactive statements, see the Qshell db2 utility.
For example:
QSH CMD('db2 -f /QSYS.LIB/MYLIB.LIB/MYSRCFILE.FILE/MYSQL.MBR')
Note that the db2 utility only accepts the *SQL naming convention.
QM Query
If all the SQL you need is the single complex SQL statement, and this is what it sounds like, then your best bet is to use Query Management Query (see QM Query manual here).
The results can be directed to a display, a spool file, or a physical file (ie a DB2 table). The default output when run interactively is to the screen, but when run in a (scheduled) batch job it will default to a spool file report.
You can create the QM Query interactively via WRKQMQRY, in prompted mode (much like Query/400) or in SQL mode. Or you can compile the QM Query from source, with the CRTQMQRY command.
To run your QM Query, STRQMQRY command.
RUNSQL cmd
If you are using a system that has IBM i 7.1 fully up-to-date, and has Technology Refresh 4 (TR4) installed, then you could also use the new RUNSQL command to execute a single statement. (see discussion in developerWorks)
SQL Scripting w/ RUNSQLSTM cmd
From CL you can run SQL scripts of multiple SQL statements from a source file member. There is no standard default source file name for this, but QSQLSRC is commonly used. The source member can contain multiple non-interactive SQL statements. This means you cannot use a SELECT statement (directly) since theoretically it will not know where to send the results. CL commands are even allowed if given a CL: prefix. Both SQL and CL statements should be terminated with a semicolon ;. While the SQL statements cannot display data directly to the screen, the same restriction does not apply to the scripted CL commands.
The STRQMQRY command can be embedded in the RUNSQLSTM script, by placing the prefix "CL: " in front of the command. Since STRQMQRY can direct output to the screen, a report, or an output table, this can come in very useful.
Remember that to direct your output from a SELECT query to a file you can use either the INSERT or CREATE TABLE statements.
CREATE TABLE newtbl AS
( full-select )
WITH DATA;
Or, to put the results into a table you create in your job's QTEMP library:
DECLARE GLOBAL TEMPORARY TABLE AS
( full-select )
WITH DATA;
[Note: If you create the source to be used by CRTQMQRY, you are advised to create it as CRTSRCPF yourlib/QQMQRYSRC RCDLEN(91), since the compiler will only use 79 columns of your source data (adding 12 for sequence and change date =91). However for QM Forms, which can be used to provide additional formatting, the CRTQMFORM compiler will use 81 columns so RCDLEN(93) is advised for QQMFORMSRC.]
RUNQRY is a utility that lets you execute a query that was created by another utility named WRKQRY. If you really want to process SQL statements held in a file try RUNSQLSTM. It uses a source physical file to store the statements, not a database file. The standard name for that source physical file is QQMQRYSRC. To create that file, CRTSRCPF yourlib/QQMQRYSRC. Then you can use PDM to work with that source PF. WRKMBRPDM yourlib/QQMQRYSRC. Use F6 to create a new source member. Make it source type TXT. Then use option 2 to will start an editor called SEU. Copy/paste your SQL statements into this editor. F3 to save the source. Once the source is saved, use RUNSQLSTM to execute it.
It is (now) possible to run SQL directly in a CL program without using QM Query, RUNSQLSTM or QShell.
Here is an article that discusses the RUNSQL statement in CL programs...
http://www.mcpressonline.com/cl/the-cl-corner-introducing-the-new-run-sql-command.html
The article contains information on what OS levels are supported as well as clear examples of several ways to use the RUNSQL statement.
This will work in two steps:
RUNSQL SQL('CREATE TABLE QTEMP/REPORT AS (SELECT +
EXTRACT_DATE , SYSTEM, ODLBNM, SUM( +
OBJSIZE_MB ) AS LIB_SIZE FROM +
ZSYSCOM/DISKRPTHST WHERE ODLBNM LIKE +
''SIS%'' GROUP BY EXTRACT_DATE, SYSTEM, +
ODLBNM ORDER BY LIB_SIZE DESC) WITH +
DATA') COMMIT(*NONE) DATFMT(*USA) DATSEP(/)
RUNQRY QRYFILE((QTEMP/REPORT)) OUTTYPE(*PRINTER) +
OUTFORM(*DETAIL) PRTDFN(*NO) PRTDEV(*PRINT)
The first step creates a temporary table result in qtemp and the second step/line runs an adhoc query over just the temporary table to a spool file.
Thanks,
Michael Frilot
There is of course a totally different solution: You could write and compile a program containing the statement. It requires some longer reading into, especially if you are new to the platform, but it should give you most flexibility over what you do with results. You can use SQL in C, C++, RPG, RPG/LE, REXX, PL (of which I don't know, what it is) and COBOL. Doing that, you can react in any processable way on results from one query and start/create other queries based on what you get.
Although some oldfashioned RPG-programmers try everything to deny SQL in RPG exists, it is possible today for many cases, to write RPG-programs with SQL only and no direct file access (without F-Specs, for those who know RPG).
If your solution works for you, perfect. If you need to do something else, try a look into this pdf: http://publib.boulder.ibm.com/infocenter/iseries/v5r3/topic/rzajp/rzajp.pdf
The integration into RPG is not too bad. It works with the normal program flow. Would look something like this (in free form):
/free
// init search values:
searchval = 'Someguy';
// so the sql query:
exec sql
SELECT colum1, colum2
INTO :var1, :var2
FROM somelib/somefile
WHERE keycol=:searchval;
// now do something with the values:
some_proc(var1);
/end-free
In this, var1, var2, and searchval are ordinary RPG-variables. No quoting needed. Works also with datastructures (externally defined e.g., the record format of the file itself fits well). You can work with cursors and loops, too, of course. I feel that RPG-programs tend to be easier to read with this.

VARBINARY output in MS MGMT Studio different than Powershell/.NET

I'm in a bit of a situation with outputting data from a table where the only column I'm selecting is a VARBINARY(MAX) type.
In management studio, when I execute the query, I get back what I expect in the format:
0x1FABCDEFG......etc
Now, when the same query is executed in powershell, via a simple setup of a SqlCommand, SqlDataAdapter and DataSet, the file is eventually output which uses the following command:
$dataSet.Tables[0] | Select-Object * | Export-Csv $outputFileName -Force -NoTypeInformation;
..it returns a bunch of rows that simply contain:
System.Byte[]
A byte array isn't what I want, just the same output that's shown when you execute it in SQL Management Studio...
Is there some kind of cast/convert magic I can do in the query (SQL 2005 btw), or do I need to go through the DataSet object and mess around with the data there (which is not ideal for the situation but can be done).
Any help or a pat on the back letting me know that I should just take my whole idea out back and shoot it would be greatly appreciated. :)
I have found this and think it may help. Microsoft blog Article which explains how to do hex to string and vise versa. If you add it to the SQL command it might do the trick