DB2 SQL referencing a list
Moderators: chulett, rschirm, roy
DB2 SQL referencing a list
Hi,
I'm writing an SQL pull from a DB2 database as follows (not completely debuged but u get the picture):
SELECT POLICY
,DEC (SUM (TBASICPRM), 12,2)
FROM CMIDBO.PREMTRAN
WHERE COMPANY = 1
AND PRODUCT IN ('COM' , 'FRM')
AND POLICY IN (119102207
,628332100
,855064102
)
GROUP BY POLICY
WITH UR
;
What I would like to do is for the 'AND POLICY IN ()" part reference a list of policies from a file location (instead of having to manually change on every run).
The data is currently siting in a sequential file created on an earlier stage.
Any suggestions?
Thanks,
Adam
I'm writing an SQL pull from a DB2 database as follows (not completely debuged but u get the picture):
SELECT POLICY
,DEC (SUM (TBASICPRM), 12,2)
FROM CMIDBO.PREMTRAN
WHERE COMPANY = 1
AND PRODUCT IN ('COM' , 'FRM')
AND POLICY IN (119102207
,628332100
,855064102
)
GROUP BY POLICY
WITH UR
;
What I would like to do is for the 'AND POLICY IN ()" part reference a list of policies from a file location (instead of having to manually change on every run).
The data is currently siting in a sequential file created on an earlier stage.
Any suggestions?
Thanks,
Adam
Adam Love
Parameterize it. Run this job via a sequence job so that you can pass
to get the list and pass it to the parameter.
Code: Select all
Field(cat list.txt, @FM, 1)
Creativity is allowing yourself to make mistakes. Art is knowing which ones to keep.
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Use a job parameter to contain the entire IN list. Your query then looks like:
Populate the IN list (by whatever method you desire, perhaps a routine that reads the file) in a job sequence that invokes this job through a Job activity. Some error handling has been omitted for clarity:
Code: Select all
SELECT POLICY ,DEC (SUM (TBASICPRM), 12,2)
FROM CMIDBO.PREMTRAN
WHERE COMPANY = 1
AND PRODUCT IN ('COM' , 'FRM')
AND POLICY IN (#PoliciesList#)
GROUP BY POLICY
WITH UR
;
Code: Select all
FUNCTION PolicyList(PoliciesFile)
* Argument is pathname of file containing policies to process.
Ans = ""
OpenSeq PoliciesFile To fPoliciesFile
Then
Loop
While ReadSeq Line From fPoliciesFile
Ans<-1> = Line ; * assumes one policy per line
Repeat
Convert @FM To "," In Ans
CloseSeq fPoliciesFile
End
Else
Call DSLogWarn("Unable to open file.", "PolicyList")
End
RETURN(Ans)
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Re-opened this post because I have a question regarding the FUNCTION PolicyList(PoliciesFile) script you recommended Ray.
I understand #PoliciesList# will reference the function, however where do you suggest I actually write the FUNCTION section? Within the SQL area of a DB2 stage after the SELECT statement?
I'm abit new to SQL and am unsure of its use within Datastage.
Thanks in advance,
Adam
I understand #PoliciesList# will reference the function, however where do you suggest I actually write the FUNCTION section? Within the SQL area of a DB2 stage after the SELECT statement?
I'm abit new to SQL and am unsure of its use within Datastage.
Thanks in advance,
Adam
Adam Love
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Not really. There is a class that the vendor used to offer, called Programming with DataStage BASIC, and one with a similar title that I offer (see website), but not much in the way of documentation per se.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Ok I used all my "resources" and was able to create the routine to pull the data. It worked :D .
In my job sequence I have a simple routine activity linked to a parellel job (containing the DB2 sql pull). On runtime I recieve the following error:
I set up my job parameter ppFNDPolicyList in parellel job DB2FNDtest as a string, so I assumed the string of values I returned with the subroutine would work well... however it seems it does not like this Paramvalue.
Any suggestions?
p.s. I attempted a different approach using Exec. command stage and was getting some help on a different post today... I was getting the same error.
Thanks for your help again.
In my job sequence I have a simple routine activity linked to a parellel job (containing the DB2 sql pull). On runtime I recieve the following error:
Code: Select all
db2testing..JobControl (@Copy_of_jobDB2FNDtest): Controller problem: Error calling DSSetParam(ppFNDPolicyList), code=-4
[ParamValue/Limitvalue is not appropriate]
Any suggestions?
p.s. I attempted a different approach using Exec. command stage and was getting some help on a different post today... I was getting the same error.
Thanks for your help again.
Adam Love
-
- Participant
- Posts: 3593
- Joined: Thu Jan 23, 2003 5:25 pm
- Location: Australia, Melbourne
- Contact:
There are two guides to programming in DataStage basic. There is a section in the Server Job Developers Guide and there is a DataStage BASIC guide. There are also some example SDK routines. There is also a large archive of code in this forum if you just search for specific commands such as DSSetParam or ReadSeq.
Certus Solutions
Blog: Tooling Around in the InfoSphere
Twitter: @vmcburney
LinkedIn:Vincent McBurney LinkedIn
Blog: Tooling Around in the InfoSphere
Twitter: @vmcburney
LinkedIn:Vincent McBurney LinkedIn
Thanks for the sources.
I have created a working routine that a Routine stage brings in the following info:
''0123450000','0123450008','0412354009','0123456008''
This is brought in as a string. It is important I keep the single quotations around the numbers as I am using this string as a parameter value in the sql IN command found in the SQL code at the top of this post.
When assigning this string to a parameter (defined as a string) I recieve the following error:
Can this string even be set to a parameter as a string with these single qoutations?
I have created a working routine that a Routine stage brings in the following info:
''0123450000','0123450008','0412354009','0123456008''
This is brought in as a string. It is important I keep the single quotations around the numbers as I am using this string as a parameter value in the sql IN command found in the SQL code at the top of this post.
When assigning this string to a parameter (defined as a string) I recieve the following error:
Code: Select all
db2testing..JobControl (@Copy_of_jobDB2FNDtest): Controller problem: Error calling DSSetParam(ppFNDPolicyList), code=-4
[ParamValue/Limitvalue is not appropriate]
-
- Participant
- Posts: 54607
- Joined: Wed Oct 23, 2002 10:52 pm
- Location: Sydney, Australia
- Contact:
Is this a String parameter type, or some other type? You should not get this message for String parameters. You seem to be missing the leading and trailing single quote characters, but what's between the double quotes is still a valid string - this would cause problems in your IN clause later.
Can you please post your routine code? Encase it in Code tags.
Can you please post your routine code? Encase it in Code tags.
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
Hi Ray,
I have defined the Parameter type as String.
Here is my routine:
This code is being used on a sequential file that appears like this:
However I can save the data to the sequential file in this format, if it is easier:
So, with the first type of sequential file I am assuming I am required to use a Field() command? or should it accept as is?
I have defined the Parameter type as String.
Here is my routine:
Code: Select all
Ans = ""
OpenSeq FoundationPoliciesFile to fFoundationFile
Then
Loop
While ReadSeq Line from fFoundationFile
Ans<-1> = Line;
Repeat
Convert @FM To "," In Ans
CloseSeq fFoundationFile
End
Else
Call DSLogWarn("Unable to open file.", "FoundationPoliciesFile")
End
RETURN(Ans)
Code: Select all
'0136530000'
'0322430008'
'0414643009'
'0532516008'
Code: Select all
'0136530000','0322430008','0414643009','0532516008',
Adam Love