SQL Update Query

Post Reply
nsalvarez
Posts: 10
Joined: Thu Sep 19, 2013 6:38 pm
OLAP Product: TM1
Version: 10.1
Excel Version: 2010

SQL Update Query

Post by nsalvarez »

I need to add a simple update query to my prolog for a process. How can I accomplish this? The data source I want to run the query against is the datasource the process is using. It does not work if I try to put it in the query box. Here is the query I need to add:

Code: Select all

Update ViewDailySalesStaging

set MaterialCost = (Amount * .40)

WHERE 
(

LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,GETDATE(),102),7)

OR  LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,DATEADD(m,-1, GETDATE()),102),7)

)

AND

DetailType = 'Intake'

AND

CompanyCode = 22

Here is my select statement for the process data source:

Code: Select all

select 
*
from  ViewDailySalesStaging
WHERE 
LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,GETDATE(),102),7)
OR  LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,DATEADD(m,-1, GETDATE()),102),7)
User avatar
qml
MVP
Posts: 1096
Joined: Mon Feb 01, 2010 1:01 pm
OLAP Product: TM1 / Planning Analytics
Version: 2.0.9 and all previous
Excel Version: 2007 - 2016
Location: London, UK, Europe

Re: SQL Update Query

Post by qml »

Three words for you:

ODBCOpen()
ODBCOutput()
ODBCClose()

Have a look at the documentation or search this forum, there's plenty of resource available on how to run any SQL command from TI script.
Kamil Arendt
nsalvarez
Posts: 10
Joined: Thu Sep 19, 2013 6:38 pm
OLAP Product: TM1
Version: 10.1
Excel Version: 2010

Re: SQL Update Query

Post by nsalvarez »

I tried the following but I keep getting an error. It wants me to add a comma but I am not sure why.
odbcopen('MyDB', 'Admin', 'password');

odbcoutput('MyDB', 'Update ViewDailySalesStaging set MaterialCost = (Amount * .40) WHERE (LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,GETDATE(),102),7)OR LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,DATEADD(m,-1, GETDATE()),102),7))AND DetailType = 'Intake' AND CompanyCode = 22 ');

odbcclose ('MyDB');

The error is: Syntax error on or before:
Intake' AND CompanyC
missing comma
declanr
MVP
Posts: 1828
Joined: Mon Dec 05, 2011 11:51 am
OLAP Product: Cognos TM1
Version: PA2.0 and most of the old ones
Excel Version: All of em
Location: Manchester, United Kingdom
Contact:

Re: SQL Update Query

Post by declanr »

nsalvarez wrote:I tried the following but I keep getting an error. It wants me to add a comma but I am not sure why.
odbcopen('MyDB', 'Admin', 'password');

odbcoutput('MyDB', 'Update ViewDailySalesStaging set MaterialCost = (Amount * .40) WHERE (LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,GETDATE(),102),7)OR LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,DATEADD(m,-1, GETDATE()),102),7))AND DetailType = 'Intake' AND CompanyCode = 22 ');

odbcclose ('MyDB');

The error is: Syntax error on or before:
Intake' AND CompanyC
missing comma

TM1 uses the ' character to signify the start and end of a string input, therefore anything between ' and ' is one string item, you have 4 ' characters in what TM1 is expecting to be just 1 input. There are a number of ways you can get around this, all of which will be covered on the forum in one place or another... personally I tend to go with:

Code: Select all

String = ''' | 'Turnips''' | ' and ''' | 'Carrots''';
Declan Rodger
tomok
MVP
Posts: 2836
Joined: Tue Feb 16, 2010 2:39 pm
OLAP Product: TM1, Palo
Version: Beginning of time thru 10.2
Excel Version: 2003-2007-2010-2013
Location: Atlanta, GA
Contact:

Re: SQL Update Query

Post by tomok »

nsalvarez wrote:The error is: Syntax error on or before:
Intake' AND CompanyC
missing comma
That's because the apostrophe is the string delimiter in TM1. When you enclosed Intake in apostrophes you broke the string and you are getting a syntax error. You can use the EXPAND function, in conjunction with double apostrophes to fix:

Code: Select all

odbcopen('MyDB', 'Admin', 'password');
sql = EXPAND('Update ViewDailySalesStaging set MaterialCost = (Amount * .40) WHERE (LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,GETDATE(),102),7)OR LEFT(CONVERT(VARCHAR,TransactionDate,102),7) = LEFT(CONVERT(VARCHAR,DATEADD(m,-1, GETDATE()),102),7))AND DetailType = ''Intake'' AND CompanyCode = 22 ')
odbcoutput('MyDB', sql);
odbclose('MyDB');
Tom O'Kelley - Manager Finance Systems
American Tower
http://www.onlinecourtreservations.com/
nsalvarez
Posts: 10
Joined: Thu Sep 19, 2013 6:38 pm
OLAP Product: TM1
Version: 10.1
Excel Version: 2010

Re: SQL Update Query

Post by nsalvarez »

Thanks for the help everyone!
Post Reply