Reading from a Spotfire Database DataSource without loading it to a table - sql

I am working on an IronPython script that pulls some new data from an oracle database, using DatabaseDataSource. I can get the script to pull the data just fine if I just overwrite or append to a table as the output. However, I would like to handle the output of one query in my script, and then use those results to generate another query. Does anybody know how I can do this?
Here's some abbreviated code for what I'm doing now for the first query:
PROVIDER = "System.Data.OracleClient"
DATASOURCE = "Data Source=(DESCRIPTION=(ADDRESS=(COMMUNITY=TCP)(PROTOCOL = TCP) (HOST=host)(PORT=1521))(CONNECT_DATA=(SID=sid))); UserId=userid;Password=password"
SQL = "SELECT PARM FROM PARAMETERS WHERE ..."
dbsettings = DatabaseDataSourceSettings(PROVIDER, DATASOURCE, SQL)
ds = DatabaseDataSource(dbsettings)
outputTable.ReplaceData(ds)
This works, but it just replaces outputTable with the data from the query, obviously. What I'd like to do is read the data I get back in the form of an array. Then, based on the results I will generate another query or set of queries, and eventually I'd merge the data from several of these into one table.

Related

SSIS save value as a parameter

I am using SELECT UpdateDate FROM dbo.log command in a execute sql task. I'm fairly new to this so please bear with me. I want to store the value as a variable then pass that into the where clause of a subsequent data flow. My questions are:
What is the correct way to setup the Execute SQL Task. In General I have the OLE DB Connection and direct input with the query above. Result Set is set to Single row and then I am storing this to a variable I have created called User:: UpdateDate. For some reason this doesn't work?
I then want to call this date in a data flow. ie. SELECT * FROM Users WHERE RecordDate > User::UpdateDate. I believe the syntax is different for this.
I would really appreciate some help with this. Many thanks in advance
In your Execute SQL Task Editor, configure the Parameter Mapping as shown below, obviously use your own variable, in this example I'm using PackageStartTime.
Then in your SQL statement, use below:
SELECT * FROM Users WHERE RecordDate > ?
To save value from a SQL Statement, you will need to set the Result Set to single row and configure result set as shown in the example below:
Execute SQL Task with ResultSet
First of all, create a variable of type System.Date example: #[User::UpdateDate].
Add an Execute SQL Task select the OLEDB connection and use the following command as SQL Statement:
SELECT TOP 1 UpdateDate FROM dbo.log
Set the ResultSet property to Single Row and in the ResultSet Tab add a Row with the following values:
ResultName = 0 (which means the first column)
VariableName = #[User::UpdateDate]
Additional Information
SSIS Basics: Using the Execute SQL Task to Generate Result Sets
OLEDB Source with parameterized SQL Command
Inside the Data Flow Task, add an OLEDB Source, select the Access Mode to SQL Command. And write the following command:
SELECT * FROM Users WHERE RecordDate > ?
Click on the Parameters button and map the variable #[User::UpdateDate] as the first parameter.
Additional Information
Map Query Parameters to Variables in a Data Flow Component
Parameterized OLEDB source query

How to run a sql within a field in the Pentaho?

I have a table with a 'query' field containing a select sql and another 'parameters' field containing the sql parameters. I have already merged these two fields into a new field containing a correct select sql statement. Now I need to execute this new field containing select sql, get the return from select (the output fields) and generate an excel file.
In your transformation, you can use the Execute row SQL script (under scripting), which does exactly what you want, executing the SQL contained in the selected input field for each row passed to the step.
Please do consider if that's really what you want. It works fine for a small set or complicated logic, but is very inefficient compared to batch inserts and updates.
If your SQL statements do look like "INSERT [some data]" and you have many rows to process, consider splitting the streams with filter steps and applying calculations or constants to each case to set the values correctly, then directing all to a table output step.

Export data frame to SQL server using RODBC package

I am using RODBC package in R to import / export data frames from SQL Server database. While there is no problem in importing. I dont know how to export the contents of a data frame into an existing SQL table.
I am trying to use sqlQuery() function available in the package, but I am not sure how to insert multiple records in the table.
A sample on how to insert the rows will be helpful
I have ensured that columns of my table and data frame are same.
Use sqlSave with append property. See my code below:
sqlSave(uploaddbconnection, outputframe, tablename =
"your_TableName",rownames=FALSE, append = TRUE)
This is my code on using sqlSave(). I am using SQL Server 2008. Conn is a connection that I created using odbcConnect():
#creating data to be saved in SQL Table
data_to_save<-cbind(scenario_1,scenario_2,scenario_3,scenario_4,store_num,future_date,Province,index)
#use sqlSave() rather than sqlQuery() for saving data into SQL Server
sqlSave(conn,data.frame(data_to_save),"CC_Forecast",safer=FALSE,append=TRUE)
dbWriteTable(conn, "RESULTS", results2000, append = T)
use DBI package
I would like to add upon Yan's answer.
Before you use sqlSave() function, make sure you change your default database to the right database where your table are. Especially if you want to write to an existing table!
See here: for how to set up ODBC connection and how to change default database.
After that, you can use these to dump data to sql:
specificDB= odbcConnect(dsn ='name you set up in ODBC',uid = '***', pwd = '****')
sqlSave(specificDB, output_to_sql, tablename = 'a_table_in_specificDB', rownames = F,append = T)
close(specificDB)
It is slow. be patient.

MS Access SQL (Quickbooks) Update Query

I'm trying to create an Update Query in MS Access (2013) to a QuickBooks Database using QODBC.
I need to update the table PriceLevelPerItem. I am trying to update the field in said table called PriceLevelPerItemCustomprice with a value from another table, QueryThreeTable, and a column titled UpdatedPrice.
I need to update the table PriceLevelPerItem where the PriceLevelPerItemItemRefListID matches the value of ItemID from QueryThreeTable and ListID matches the QueryThreeTable.ItemListID (yes I know these are the wrong way around...)
So far this process has been a very annoying trial of many queries and any help would be greatly appreciated
This is what I've been working with:
UPDATE
PriceLevelPerItem
SET
(PriceLevelPerItemCustomPrice = QueryThreeTable.UpdatedPrice)
FROM
QueryThreeTable, PriceLevelPerItem
WHERE
QueryThreeTable.ItemID = PriceLevelPerItem.PriceLevelPerItemItemRefListID
AND
QueryThreeTable.ItemListID = PriceLevelPerItem.ListID;
I think the problem is that you're trying to use a DAO query inside a QODBC query. I think the two use different Data Access engines.
You're going to need to lookup your UpdatedPrice in your QueryThreeTable using DLookup. Or maybe you need to create a DAO loop using QueryThreeTable that then updates values in your QODBC table from there.
Make your QODBC query work without the use of QueryThreeTable and without any joins. Then come up with a way to dynamically create your query. You're resulting SQL should look something like this:
UPDATE
PriceLevelPerItem
SET
PriceLevelPerItemCustomPrice = 150.16
WHERE
PriceLevelPerItem.ListID = '310000-1146238368';

MS Access query with dynamic from statements

Ok this is vexing me. I have a query I created with an in statement in the from clause. What I am looking to do is have a global variable populate that from statement. Example
Select *
Form query1 in <Global Variable filename>
What is going on is I link to a file that has hundreds of linked table and queries in it. My database only has a few select queries and link table to different database. I did not want to re-build all the queries and linked table is my database. The issue is the file with all the links changes name once a month. I just want to read a text file with the current name of the database in it so I do not have to keep changing my queries every time the database name changes. Also this has to a query since I have other queries using the externally linked query.
I have one suggestion, but its pretty kludgy.
Rewrite the query on the fly with VBA call
Private Sub update_qtest()
Dim db As Database
Dim qd As QueryDef
Set db = CurrentDb
Set qd = db.QueryDefs("qtest")
qd.SQL = "SELECT * from query1 in " & g_file_name
End Sub
As I said, it's kludgy, but I don't think there's a way to pass the from clause as a parameter.
Another way to do this would be to just use the same file name each month so you wouldn't have to change anything in your Access app at all. You could easily code copying the file to the standard name over top of the previous copy (you'd have to delete it before copying, of course), which would retain the history.