Generate csv file
How can I generate CSV file from a select query without having any loop .Is there any in build function which can directly generates trhe csv ?
I am using oracle 10g .
My be you can try with and an SQL
create directory root as 'C:\';
declare
vclob clob := empty_clob();
begin
with mydata as
select
l || 'a' data1
, l || 'b' data2
, l || 'c' data3
, row_number() over(order by l) curr
, (row_number() over(order by l)) - 1 prev
from
ta
select
replace(ltrim(max(sys_connect_by_path(data1 || ',' || data2 || ',' || data2,':')), ':'), ':', chr(13))
into
vclob
from
mydata
start with
curr = 1
connect by
prior curr = prev;
dbms_advisor.create_file(buffer => vclob
,location => 'ROOT'
,filename => 'mycsv.csv'
end;
/I guess i will get lot of scolding form gurus and experts for above code :P
Regards,
Amit
Similar Messages
-
Needs to generate CSV file through DME
Hi Experts,
I am using DME to generate certain file, the file format is supposed to contain a fixed number of fields even if most are empty, with each field delimited or separated by comma (using CSV Standard File Format), and not tab or space delimited.
While customising the file through T-Code DMEE, file gets generated but not in CSV format due to which Bank system unable to read the data.
Please help me out in getting the file in the required format.
Its an urgent issue, hence looking for a quick reply from the experts.
Thanks in Advance
Regards,Hi Experts,
Please help me out....
Regards -
Generating .csv file with the datas from the database
Hi Java experts,
Ihave a current assignment to generate a .CSV file using java getting values from database. The output CSV file may have more than one row and each row should have a end of line delimiter. Any help in giving a template for generating a CSV file accessing from database is greatly appreciated.Ihave a current assignment to generate a .CSV file
using java getting values from database.well, I hope you learn something doing it yourself...
The output
CSV file may have more than one row and each row
should have a end of line delimiter. Any help in
giving a template for generating a CSV file accessing
from database is greatly appreciated.How about looking for a JDBC and a file/print stream tutorial? -
Generating CSV file using java/ stored proc
Hi Folks,
I am using Oracle 11g DB
My requirement is that I have to generate a CSV file which has customer details. Each value that is comma separated comes from different set of SQL queries. I want to write a stored procedure along with java code to acieve this. How do I go about it? Should I return multiple cursors from my stored proc and iterate from the java side to create the CSV file? Any other suggestions?
Thanks
Vishal>
I am using Oracle 11g DB
My requirement is that I have to generate a CSV file which has customer details. Each value that is comma separated comes from different set of SQL queries. I want to write a stored procedure along with java code to acieve this. How do I go about it? Should I return multiple cursors from my stored proc and iterate from the java side to create the CSV file? Any other suggestions?
>
Other suggestions? Yes - don't reinvent the wheel - it has already been created.
1. Tom already wrote such a proc and published it many years ago. See this AskTom thread
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:235814350980
2. See BluShadow's link from the '5. How do I read or write an Excel file?' question in the FAQ at the top of the sql and pl/sql forum
SQL and PL/SQL FAQ
3. Use a sql developer query like 'select /*csv*/ * from emp
4. Search the sql and pl/sql forum for csv and use of the many threads that already answer this. -
I was told I was able to generate out a CSV file through UTL_FILE method. Can someone provide the skeleton of how should I create it in PL/SQL?
I tried google search but it only says in words and not code itself.
I have this PL/SQL that I will want to generate a CSV file.
SET SERVEROUTPUT ON;
SET TIMING ON;
DECLARE
CURSOR C1
IS
select balance from contract ;
BEGIN
FOR i IN C1 LOOP
IF i.balance <> '0' THEN
DBMS_OUTPUT.PUT_LINE('Amount is not 0 ');
I will like to put the statement 'Amount is not 0 ' in a CSV file here
else
DBMS_OUTPUT.PUT_LINE('Amount is 0 ');
END IF;
end loop;
END;
/How am I suppose to do this?
Edited by: JoannaLee on Aug 26, 2008 7:14 PMYour question is a bit confusing. In your subject line, and again in your question, you talk about generating a CSV file. That is a file of comma-separated values (hence the acryonym). But your question seems to be asking about generating a file with a sentence in it-- that would be a flat file but not a CSV file.
The links that were provided show you exactly how to generate a CSV file based on an arbitrary query.
If you are not looking to generate a CSV file, and instead you just want to generate a flat file to which you can write data, you'd just need to use the UTL_FILE package. Assuming you're on a recent version of Oracle, you would need to create a directory object, i.e.
CREATE DIRECTORY directory_name AS 'path_to_directory_on_database_server'Then you'd grant privileges on that directory object to whatever user will own the procedure that wants to write to the directory
GRANT read, write ON directory_name TO some_userIn your code, you'd just need to open the file and write to it
DECLARE
l_file utl_file.file_type;
BEGIN
l_file := utl_file.fopen( 'DIRECTORY_NAME', 'name_of_file', 'w' );
<<Other code>>
utf_file.put_line( l_file, 'String you want to write to the file' );
<<More code>>
utl_file.fclose( l_file );
END;Justin -
Generating CSV file with column names and data from the MySQL with JAVA
Hi all,
Give small example on ...
How can I add column names and data to a CSV from from MySQL.
like
example
sequence_no, time_date, col_name, col_name
123, 27-apr-2004, data, data
234, 27-apr-2004, data, data
Pls give small exeample on this.
Thanks & Regards
Rama KrishnaHello Rama Krishna,
Check this code:
Example below exports data from MySQL Select query to CSV file.
testtable structure
CREATE TABLE testtable
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
text varchar(45) NOT NULL,
price integer not null);
Application takes path of output file as an argument.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class automateExport {
public static void main(String[] args) {
DBase db = new DBase();
Connection conn = db.connect(
"jdbc:mysql://localhost:3306/test","root","caspian");
if (args.length != 1) {
System.out.println(
"Usage: java automateExport [outputfile path] ");
return;
db.exportData(conn,args[0]);
class DBase {
public DBase() {
public Connection connect(String db_connect_str,
String db_userid, String db_password) {
Connection conn;
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
conn = DriverManager.getConnection(db_connect_str,
db_userid, db_password);
} catch(Exception e) {
e.printStackTrace();
conn = null;
return conn;
public void exportData(Connection conn,String filename) {
Statement stmt;
String query;
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
//For comma separated file
query = "SELECT id,text,price into OUTFILE '"+filename+
"' FIELDS TERMINATED BY ',' FROM testtable t";
stmt.executeQuery(query);
} catch(Exception e) {
e.printStackTrace();
stmt = null;
Greetings,
Praveen Gudapati -
Remove column name in generated CSV file through ODI
Hi,
I want to remove the column names in CSV file which I am creating through database.
I have tried putting "heading to zero in the the datastore", which I created for csv file, but it is not working.
Please assist.
Thanks,
Sudeep NaikOkay let me explain.After creating your csv file you open it.If 1st row contains column name then during reverse you use heading as 1. Regarding c1,c2 these are the column name defined by odi.Its not going to come as your data.The way i told you just make it by giving header 1 then reverse it.After that just drag it to your interface.Then on interface right click on source table and click on view data.Here you can find if your column name is coming as data or not.Hope you are clear now.
Thanks. -
Crystal report - generate CSV file through Java interface
Hi,
I need to generate a Character Seperated File through Crystal Report 10 using Java.
What constant stands for CSV Format and what value of SI_PROGID we can provide to pull the CSV format.
Eg. SI_PROGID = ceProgID.EXCEL or SI_PROGID = ceProgID.PDF .....
Currentlly we are crystalLibs.jar to connect the Crystal server.
Thanks In AdvanceIn BusinessObjects Enterprise installations, the following ProgIDs are valid.
CrystalEnterprise.Analytic
CrystalEnterprise.AppFoundation
CrystalEnterprise.Calendar
CrystalEnterprise.Category
CrystalEnterprise.Connection
CrystalEnterprise.Designer
CrystalEnterprise.Destination
CrystalEnterprise.DiskUnmanaged
CrystalEnterprise.Event
CrystalEnterprise.Excel
CrystalEnterprise.Folder
CrystalEnterprise.FavoritesFolder
CrystalEnterprise.Ftp
CrystalEnterprise.Hyperlink
CrystalEnterprise.Inbox
CrystalEnterprise.InfoView
CrystalEnterprise.LicenseKey
CrystalEnterprise.Managed
CrystalEnterprise.MyInfoView
CrystalEnterprise.ObjectPackage
CrystalEnterprise.Overload
CrystalEnterprise.Pdf
CrystalEnterprise.PersonalCategory
CrystalEnterprise.Powerpoint
CrystalEnterprise.Program
CrystalEnterprise.Report
CrystalEnterprise.Rtf
secEnterprise
secLDAP
CrystalEnterprise.Server
CrystalEnterprise.ServerGroup
CrystalEnterprise.Shortcut
CrystalEnterprise.Smtp
CrystalEnterprise.Txt
CrystalEnterprise.Universe
CrystalEnterprise.User
CrystalEnterprise.UserGroup
CrystalEnterprise.Webi
CrystalEnterprise.Word -
Using HP 548xxa oscilloscope LabView driver to generate csv files
I have downloaded the HP 548xxa Oscilloscope driver. I'm not sure how to use the driver so that I can capture waveforms and generate a CSV or fext file of the waveform data
To understand instrument drivers, you should start by reading chapter 16 of the LabVIEW Measurments Manual. This is a shipping document that you can access from Help>Search the LabVIEW Bookshelf. Your specific driver comes with a VI called hp548xxa Getting Started. This is a high level example that demonstrates how the subVIs in the driver can be used. You should look at this first and get this working. The actual subVI that gets waveform data is hp548xxa Data Read Waveform. This returns a single channel's data as a 1D array and x scale information (timing) as scalars. You can wire this data to Write to Spreadsheet File or create a waveform data type for each channel acquired and use Export Waveforms to Spreadsheet File. The attached example demonstr
ates this method. Hope it helps.
Attachments:
Scope_to_Spreadsheet.vi 49 KB -
Getting empty csv file using servlet
Hi
i am working on reports for my web application and i used struts frame work.
for my reports i want csv export, so for that i written servlet, once if i click generate button i am able to open popup window to save the generated csv file at my local system, but i am getting emplty csv file..
nothing si ther ein that file forget abt data atleast my header fields.
here is my servlet file..plz let me know where i am doing wrong..
public class ReportServlet extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
PrintWriter out = res.getWriter();
res.setContentType("text/csv");
res.setHeader("Content-Disposition","attachment; filename=\"export.csv\"");
out = res.getWriter();
AdvDetailReportBean reportBean = null;
ArrayList list =(ArrayList)req.getSession().getAttribute("advreportlist");
System.out.println(" servlet report list size is"+list.size());
String branchcode=(String)req.getSession().getAttribute("branchcode");
String bName=(String)req.getSession().getAttribute("branchname");
System.out.println(" servlet branch name"+bName);
System.out.println(" servlet branch code"+branchcode);
StringBuffer fw = new StringBuffer();
fw.append("Branch Code");
fw.append(',');
fw.append("Branch Name");
fw.append('\n');
fw.append(branchcode);
fw.append(',');
fw.append(bName);
fw.append('\n');
fw.append('\n');
fw.append("Customer Name");
fw.append(',');
fw.append("Constitution Code");
fw.append(',');
fw.append("Customer Status");
fw.append(',');
fw.append("Restructure Date");
fw.append(',');
fw.append("Total Provision");
fw.append(',');
fw.append("Limit Sanctioned");
fw.append(',');
fw.append("Principal");
fw.append(',');
fw.append("Balance");
fw.append(',');
fw.append("AccountID");
fw.append(',');
fw.append("Collateral SL No");
fw.append(',');
fw.append("Issue Date Of Collateral");
fw.append(',');
fw.append("MaturityDate Of Collateral");
fw.append(',');
fw.append("Subsidy");
fw.append(',');
fw.append("Guarantor SL No");
fw.append(',');
fw.append("Guarantor Rating Agency ");
fw.append(',');
fw.append("External Rating of Guarantor");
fw.append(',');
fw.append("Rating Expiry Date");
fw.append(',');
fw.append("Guarantee Amount");
fw.append(',');
fw.append('\n');
for (Iterator it = list.iterator(); it.hasNext(); )
reportBean = new AdvDetailReportBean();
reportBean = (AdvDetailReportBean)it.next();
fw.append(reportBean.getCustomername());
fw.append(',');
fw.append(reportBean.getConstitutionCode());
fw.append(',');
fw.append(reportBean.getCustomerStatus());
fw.append(',');
fw.append(reportBean.getRestructureDate());
fw.append(',');
fw.append(reportBean.getTotalProvision());
fw.append(',');
fw.append(reportBean.getLimitSanctioned());
fw.append(',');
fw.append(reportBean.getPrincipal());
fw.append(',');
fw.append(reportBean.getBalance());
fw.append(',');
fw.append(reportBean.getCurrentValue());
fw.append(',');
fw.append(reportBean.getAccountNumber());
fw.append(',');
fw.append(reportBean.getColCRMSecId());
fw.append(',');
fw.append(reportBean.getIssueDt());
fw.append(',');
fw.append(reportBean.getMarturityDt());
fw.append(',');
fw.append(reportBean.getUnAdjSubSidy());
fw.append(',');
fw.append(reportBean.getGuarantorFacilityId());
fw.append(',');
fw.append(reportBean.getRatingAgency());
fw.append(',');
fw.append(reportBean.getExternalRating());
fw.append(',');
fw.append(reportBean.getExpDtOfRating());
fw.append(',');
fw.append(reportBean.getGuaranteeAmt());
fw.append(',');
fw.append('\n');
}You don't seem to be writing anything to the response at all. Yes, you create a StringBuffer and write lots of stuff to the StringBuffer but then you do nothing else with that buffer.
-
How to avoid spaces in csv file at the time of spooling
Hi all,
I am spooling 30 query results into one CSV file ,I am getting two empty rows for each query.
Can any one suggest me how to avoid these spaces.
Thanks & Regards,
P Prakash
this is the script i am using to generate csv file .
SET linesize 12000
SET pagesize 10000
SET pause off
SET termout off
SET feed off
SET head off
SPOOL c:\tes111.csv replace
SELECT 'ISA01,ISA02,ISA03,ISA04,ISA05,ISA06,ISA07,ISA08,ISA09,ISA11,ISA12,ISA13,ISA14,ISA15,ISA16,GS01,GS02,GS03,GS04,GS05,GS06,GS07,GS08,ST01,ST02,ST03,BHT01,BHT02,BHT03,BHT04,BHT06,trnsctn_segment_count,included_trnsctn_sets_count,included_fnctnl_groups_count,input_acknwldgmnt_sid'
FROM DUAL;
SELECT ptr.athrztn_infrmtn_qlfr
|| ','
|| ptr.athrztn_infrmtn
|| ','
|| ptr.scrty_infrmtn_qlfr
|| ','
|| ptr.scrty_infrmtn
|| ','
|| ptr.intrchng_sndr_idntfr_qlfr
|| ','
|| ptr.intrchng_sndr_idntfr
|| ','
|| ptr.intrchng_rcvr_idntfr_qlfr
|| ','
|| ptr.intrchng_rcvr_idntfr
|| ','
|| ptr.intrchng_date
|| ','
|| ptr.intrchng_cntrl_stndrds_idntfr
|| ','
|| ptr.intrchng_cntrl_vrsn_nmbr
|| ','
|| ptr.intrchng_cntrl_nmbr
|| ','
|| ptr.acknwldgmnt_rqstd_indctr
|| ','
|| ptr.usg_indctr
|| ','
|| ptr.cmpnt_elmnt_sprtr
|| ','
|| ptr.fnctnl_idntfr_code
|| ','
|| ptr.aplctn_sndr_code
|| ','
|| ptr.applctn_rcvr_code
|| ','
|| ptr.fnctnl_grp_crtn_date
|| ','
|| ptr.fnctnl_grp_crtn_date
|| ','
|| ptr.grp_cntrl_nmbr
|| ','
|| ptr.rspnsbl_agncy_code
|| ','
|| ptr.vrsn_rls_indstry_idntfr_code
|| ','
|| ptr.trnsctn_set_idntfr_code
|| ','
|| ptr.trnsctn_set_cntrl_nmbr
|| ','
|| ptr.implementation_guide_vrsn_name
|| ','
|| ptr.hierarchical_structure_code
|| ','
|| ptr.trnsctn_set_purpose_lkpcd
|| ','
|| ptr.sbmtr_trnsctn_idntfr
|| ','
|| ptr.trnsctn_set_creation_date
|| ','
|| ptr.trnsctn_type_code
|| ','
|| ptr.trnsctn_segment_count
|| ','
|| ptr.included_trnsctn_sets_count
|| ','
|| ptr.included_fnctnl_groups_count
|| ','
|| ia.input_acknwldgmnt_sid
|| ','
FROM pa_transaction_request ptr, input_acknwldgmnt ia, input_batch_file ibf
WHERE ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
SELECT 'pa_rqst_sid,NM01,NM02,NM108,NM109'
FROM DUAL;
SELECT pr.pa_rqst_sid
|| ','
|| pr.entity_idntfr_lkpcd
|| ','
|| pr.entity_type_qlfr
|| ','
|| pr.payer_idntfctn_code_qlfr
|| ','
|| pr.payer_idntfctn_code
|| ','
FROM pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
SELECT 'meta_data_cid,data_value,iteration_number,Created_by,modified_by'
FROM DUAL;
SELECT prs.meta_data_cid
|| ','
|| prs.data_value
|| ','
|| prs.iteration_number
|| ','
|| prs.created_by
|| ','
|| prs.modified_by
|| ','
FROM pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_situational prs
WHERE pr.pa_rqst_sid = prs.pa_rqst_sid
AND prs.stnl_target_table_cid = 100
AND prs.target_table_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
SELECT 'NM101 ,NM103,NM104,NM105,NM108,NM109,PER02,PER04,PER05,PER06,PER07 ,PER08'
FROM DUAL;
SELECT pr.pa_rqst_sid
|| ','
|| prpl.pa_rqst_x_prvdr_lctn_sid
|| ','
|| prpl.rqstr_entity_idntfr_lkpcd
|| ','
|| prpl.last_name
|| ','
|| prpl.first_name
|| ','
|| prpl.middle_name
|| ','
|| prpl.idntfr_type_cid
|| ','
|| prpl.prvdr_lctn_iid
|| ','
|| prpl.rqstr_contact_name
|| ','
|| prpl.cmnctn_nmbr_1
|| ','
|| prpl.cmnctn_nmbr_2
|| ','
|| prpl.cmnctn_nmbr_type_lkpcd_1
|| ','
|| prpl.cmnctn_nmbr_type_lkpcd_2
|| ','
|| prpl.cmnctn_nmbr_type_lkpcd_3
FROM pa_request_x_provider_location prpl,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prpl.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat'
AND prpl.pa_prvdr_type_lkpcd = 'RR';
SELECT 'meta_data_cid,data_value,iteration_number,Created_by,modified_by'
FROM DUAL;
SELECT prs.meta_data_cid
|| ','
|| prs.data_value
|| ','
|| prs.iteration_number
|| ','
|| prs.created_by
|| ','
|| prs.modified_by
|| ','
FROM pa_request_x_provider_location prpl,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_situational prs
WHERE prpl.pa_rqst_sid = prs.pa_rqst_sid
AND prs.stnl_target_table_cid = 102
AND prs.target_table_sid = prpl.pa_rqst_x_prvdr_lctn_sid
AND prpl.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat'
AND prpl.pa_prvdr_type_lkpcd = 'RR';
-- 4. Subscriber
NM103 Name Last or Organization Name
NM104 Name First
NM108 Identification Code Qualifier
NM109 Identification Code
DMG02 Date Time Period birth date
DMG03 Gender Code
SELECT ' NM103,NM104,NM108,NM109,DMG02,DMG03'
FROM DUAL;
SELECT prxm.last_name
|| ','
|| prxm.first_name
|| ','
|| prxm.idntfr_type_cid
|| ','
|| prxm.mbr_idntfr
|| ','
|| prxm.birth_date
|| ','
|| prxm.gender_lkpcd
|| ','
FROM pa_request_x_member prxm,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prxm.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- 5. Subscriber Situational Info
NM102 Entity Type Qualifier
NM105 Name Middle
NM107 Name Suffix
REF01 Reference Identification Qualifier
REF02 Reference Identification
N301 Address Information
N302 Address Information
N401 City Name
N402 State or Province Code
N403 Postal Code
N407 Country Subdivision Code
SELECT 'meta_data_cid,data_value,iteration_number,Created_by,modified_by'
FROM DUAL;
SELECT prs.meta_data_cid
|| ','
|| prs.data_value
|| ','
|| prs.iteration_number
|| ','
|| prs.created_by
|| ','
|| prs.modified_by
|| ','
FROM pa_request_x_member prxm,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_situational prs
WHERE prxm.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND prs.stnl_target_table_cid = 101
AND prs.target_table_sid = prxm.pa_rqst_x_mbr_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- 6. Patient Event Level
TRN02 Reference Identification
TRN03 Originating Company Identifier
TRN04 Reference Identification
UM01 Request Category Code
UM02 Certification Type Code
UM03 Service Type Code
UM04-1 Facility Code Value
UM04-2 Facility Code Qualifier
UM06 Level of Service Code
UM07 Current Health Condition Code
UM08 Prognosis Code
UM09 Release of Information Code
UM10 Delay Reason Code
SELECT 'TRN02,TRN03,TRN04,UMO1,UMO2,UM04_1,UM04_2,UM06,UM07,UM08,UM09,UM10,'
FROM DUAL;
SELECT prd.patient_event_tracking_number
|| ','
|| prd.orginating_company_identifier
|| ','
|| prd.trace_assigning_entity_idntfr
|| ','
|| prs.rqst_ctgry_lkpcd
|| ','
|| prs.rqst_crtfctn_type_lkpcd
|| ','
|| prs.x12_pa_srvc_type_code
|| ','
|| prs.facility_type_code
|| ','
|| prs.unfrm_blng_facility_type_code
|| ','
|| prs.srvc_rqd_lkpcd
|| ','
|| prs.current_health_cndtn_lkpcd
|| ','
|| prs.prognosis_lkpcd
|| ','
|| prs.rls_of_info_lkpcd
|| ','
|| prs.delay_reason_lkpcd
|| ','
FROM pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_detail prd
WHERE prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND pr.pa_rqst_sid = prd.pa_rqst_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
--Patient Event Level situational information
SELECT 'meta_data_cid,data_value,iteration_number,Created_by,modified_by'
FROM DUAL;
SELECT prs.meta_data_cid
|| ','
|| prs.data_value
|| ','
|| prs.iteration_number
|| ','
|| prs.created_by
|| ','
|| prs.modified_by
|| ','
FROM pa_request_service prsv,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_situational prs
WHERE prsv.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND prs.stnl_target_table_cid = 103
AND prs.target_table_sid = prsv.pa_rqst_srvc_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
---Diagnosis
HI01 Health Care Code Information
HI01-1 Code List Qualifier Code
HI01-2 Industry Code
HI01-3 Date Time Period Format Qualifier
HI01-4 Date Time Period
SELECT 'HI01_1,HI01_2,HI01_3'
FROM DUAL;
SELECT prd.pa_diagnosis_type_lkpcd
|| ','
|| prd.diagnosis_iid
|| ','
|| prd.from_date
|| ','
FROM pa_request_x_diagnosis prd,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prd.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- Request detail
TRN02 Reference Identification
TRN03 Originating Company Identifier
TRN04 Reference Identification
SELECT 'TRN02,TRN03,TRN04'
FROM DUAL;
SELECT prd.patient_event_tracking_number
|| ','
|| prd.orginating_company_identifier
|| ','
|| prd.trace_assigning_entity_idntfr
|| ','
FROM pa_request_detail prd,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prd.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- ambulance Transaportation
CR101 Unit or Basis for Measurement Code
CR102 Weight
CR103 Ambulance Transport Code
CR104 Ambulance Transport Reason Code
CR105 Unit or Basis for Measurement Code
CR106 Quantity
CR109 Description
CR110 Description
SELECT 'CR101,CR102,CR103,CR104,CR105,CR106,CR109,CR110'
FROM DUAL;
SELECT pat.weight_uom_code
|| ','
|| pat.patient_weight
|| ','
|| pat.amblnc_transport_type_lkpcd
|| ','
|| pat.amblnc_transport_rsn_lkpcd
|| ','
|| pat.distance_uom_code
|| ','
|| pat.trnsprtn_distance
|| ','
|| pat.round_trip_purpose_desc
|| ','
|| pat.stretcher_purpose_desc
|| ','
FROM pa_request_ambulance_transport pat,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE pat.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- 2000F SV1
SV101-1 Product/Service ID Qualifier Change values
SV101-2 Product/Service ID Need to change label to Procedure Code.
SV101-3 Procedure Modifier No change required to PA screen
SV101-4 Procedure Modifier No change required to PA screen
SV101-5 Procedure Modifier No change required to PA screen
SV101-6 Procedure Modifier No change required to PA screen
SV101-7 Description No change required to PA screen. Will map to the Remarks field on the Screen
SV102 Monetary Amount
SV103 Unit or Basis for Measurement Code
SV104 Quantity
SV107 Composite Diagnosis Code Pointer
SV107-1 Diagnosis Code Pointer
SV107-2 Diagnosis Code Pointer
SV107-3 Diagnosis Code Pointer
SV107-4 Diagnosis Code Pointer
SV111 Yes/No Condition or Response Code
SV120 Level of Care Code
SELECT 'TRN_Reference_Identif,TRN_Originating_CMP_Identif,TRN_Reference_IdentiReference Identiff,Reference_Identif,Reference_Identification,Reference_Identif,Product/Service_ID_Qualifier,Product/Service_ID,Procedure_Modifier,Procedure_Modifier,Procedure_Modifier,Procedure_Modifier,Description'
FROM DUAL;
SELECT prp.srvc_trace_nmbr_1
|| ','
|| prp.trace_asgn_enty_adtnl_idntfr_1
|| ','
|| prp.trace_asgn_enty_adtnl_idntfr_1
|| ','
|| prp.trace_asgn_enty_adtnl_idntfr_2
|| ','
|| prp.prvs_rvw_athrztn_nmbr
|| ','
|| prp.prvs_administrative_rfrnc_nmbr
|| ','
|| prp.x12_code_list_qlfr_lkpcd
|| ','
|| prp.procedure_iid
|| ','
|| prp.mdfr_code
|| ','
|| prp.mdfr2_code
|| ','
|| prp.mdfr3_code
|| ','
|| prp.mdfr4_code
|| ','
|| prp.drug_desc
|| ','
FROM pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HHIPAA.165760000.20110518I001.278_GC04-3.dat';
SV2 SV201 Product/Service ID
SV2 SV202 Composite Medical Procedure Identifier
SV2 SV202-1 Product/Service ID Qualifier
SV2 SV202-2 Product/Service ID
SV2 SV202-3 Procedure Modifier
SV2 202-4 Procedure Modifier
SV2 SV202-5 Procedure Modifier
SV2 SV202-6 Procedure Modifier
SV2 SV202-7 Description
SV2 SV202-8 Product/Service ID
SV2 SV203 Monetary Amount
SV2 SV204 Unit or Basis for Measurement Code
SV2 SV205 Quantity
SV2 SV206 Unit Rate
SV2 SV209 Nursing Home Residential Status Code
SELECT 'Product/Service ID,Product/Service ID Qual,Product/Service ID,Product Modifier,Product Modifier,Product Modifier,Product Modifier,Description,Unit Rate,'
FROM DUAL;
SELECT prp.revenue_iid ||','||
prp.x12_code_list_qlfr_lkpcd ||','||
prp.procedure_iid ||','||
prp.mdfr_code ||','||
prp.mdfr2_code ||','||
prp.mdfr3_code ||','||
prp.mdfr4_code ||','|| prp.drug_desc ||','||
prp.srvc_line_rate ||','
FROM pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- 2000F SV3
SV301 Composite Medical Procedure Identifier
SV301-1 Product/Service ID Qualifier
SV301-2 Product/Service ID
SV301-3 Procedure Modifier
SV301-4 Procedure Modifier
SV301-5 Procedure Modifier
SV301-6 Procedure Modifier
SV301-7 Description
SV301-8 Product/Service ID
SV302 Monetary Amount
SV304 Oral Cavity Designation
SV304-1 Oral Cavity Designation Code
SV304-2 Oral Cavity Designation Code
SV304-3 Oral Cavity Designation Code
SV304-4 Oral Cavity Designation Code
SV304-5 Oral Cavity Designation Code
SV305 Prosthesis, Crown or Inlay Code
SV306 Quantity
SV307 Description
select 'Product/Service ID Qual,Product/Service ID,Procedure Modifier,Procedure Modifier,Procedure Modifier,Procedure Modifier,Description,Oral Cavity Designation Code,Oral Cavity Designation Code,Oral Cavity Designation Code,Oral Cavity Designation Code,ProsthesisCrown Inlay Code,Description' from dual;
SELECT prp.x12_code_list_qlfr_lkpcd
|| ','
|| prp.procedure_iid
|| ','
|| prp.mdfr_code
|| ','
|| prp.mdfr2_code
|| ','
|| prp.mdfr3_code
|| ','
|| prp.mdfr4_code
|| ','
|| prp.drug_desc
|| ','
|| prp.oral_cavity_dsgntn2_cid
|| ','
|| prp.oral_cavity_dsgntn3_cid
|| ','
|| prp.oral_cavity_dsgntn4_cid
|| ','
|| prp.oral_cavity_dsgntn5_cid
|| ','
|| prp.prosthesis_crown_inlay_code
|| ','
|| prp.remark
|| ','
FROM pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- 2000F TOO pending Prakash to Write
--Tooth Information
SELECT 'Industry Code,Tooth Surface,TOO03-1_Tooth Surface Code,TOO03-2_Tooth Surface Code,TOO03-3_Tooth Surface Code,TOO03-4_Tooth Surface Code,'
FROM DUAL;
SELECT prp.tooth_number_cid
|| ','
|| prp.tooth_surface_cid
|| ','
|| prp.tooth_surface2_cid
|| ','
|| prp.tooth_surface3_cid
|| ','
|| prp.tooth_surface4_cid
|| ','
|| prp.tooth_surface5_cid
|| ','
FROM pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
--tooth SItuational Information
select 'Monetary Amount,Quantity' from dual;
SELECT prpt.rqst_prcdr_amt||','|| prpt.rqst_prcdr_units||','
FROM pa_rqst_prcdr_transaction prpt,
pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prpt.pa_rqst_prcdr_sid = prp.pa_rqst_prcdr_sid
AND prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- pa_rqst_prcdr_x_prvdr_lctn
select 'pa_rqst_prcdr_sid,pa_rqst_x_prvdr_lctn_sid' from dual;
SELECT prppl.pa_rqst_prcdr_sid||','|| prppl.pa_rqst_x_prvdr_lctn_sid||','
FROM pa_rqst_prcdr_x_prvdr_lctn prppl,
pa_request_procedure prp,
pa_request_service prs,
pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf
WHERE prppl.pa_rqst_prcdr_sid = prp.pa_rqst_prcdr_sid
AND prp.pa_rqst_srvc_sid = prs.pa_rqst_srvc_sid
AND prs.pa_rqst_sid = pr.pa_rqst_sid
AND pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
-- error Details
--Header level error details
SELECT 'pa_rqst_sid,pa_error_nmbr,pa_error_sid,reject_reason_lkpcd,follow_up_action_lkpcd,aaa_segment_loop_nmbr,run_nmbr,'
FROM DUAL;
SELECT prre.pa_rqst_sid
|| ','
|| pe.pa_error_nmbr
|| ','
|| pe.pa_error_sid
|| ','
|| ped.reject_reason_lkpcd
|| ','
|| ped.follow_up_action_lkpcd
|| ','
|| ped.aaa_segment_loop_nmbr
|| ','
|| prre.run_nmbr
|| ','
FROM pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_run_error prre,
pa_error pe,
pa_error_detail ped
WHERE pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND pr.pa_rqst_sid = prre.pa_rqst_sid
AND prre.pa_error_sid = pe.pa_error_sid
AND pe.pa_error_sid = ped.pa_error_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
---Line Level Error Details
SELECT 'pa_rqst_sid,pa_error_nmbr,pa_error_sid,reject_reason_lkpcd,follow_up_action_lkpcd,aaa_segment_loop_nmbr'
FROM DUAL;
SELECT pr.pa_rqst_sid
|| ','
|| pe.pa_error_nmbr
|| ','
|| pe.pa_error_sid
|| ','
|| ped.reject_reason_lkpcd
|| ','
|| ped.follow_up_action_lkpcd
|| ','
|| ped.aaa_segment_loop_nmbr
|| ','
FROM pa_request pr,
pa_transaction_request ptr,
input_acknwldgmnt ia,
input_batch_file ibf,
pa_request_service prs,
pa_request_procedure prp,
pa_request_procedure_run_error prpre,
pa_error pe,
pa_error_detail ped
WHERE pr.pa_trnsctn_rqst_sid = ptr.pa_trnsctn_rqst_sid
AND ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND pr.pa_rqst_sid = prs.pa_rqst_sid
AND prs.pa_rqst_srvc_sid = prp.pa_rqst_srvc_sid
AND prp.pa_rqst_prcdr_sid = prpre.pa_rqst_prcdr_sid
AND prpre.pa_error_sid = pe.pa_error_sid
AND pe.pa_error_sid = ped.pa_error_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
SPOOL off
SET head on
SET feed on
SET termout on
SET pause on833560 wrote:
Hi all,
I am spooling 30 query results into one CSV file ,I am getting two empty rows for each query.
Can any one suggest me how to avoid these spaces.
Thanks & Regards,
P Prakash
this is the script i am using to generate csv file .
SET linesize 12000
SET pagesize 10000
SET pause off
SET termout off
SET feed off
SET head off
SPOOL c:\tes111.csv replace
SELECT 'ISA01,ISA02,ISA03,ISA04,ISA05,ISA06,ISA07,ISA08,ISA09,ISA11,ISA12,ISA13,ISA14,ISA15,ISA16,GS01,GS02,GS03,GS04,GS05,GS06,GS07,GS08,ST01,ST02,ST03,BHT01,BHT02,BHT03,BHT04,BHT06,trnsctn_segment_count,included_trnsctn_sets_count,included_fnctnl_groups_count,input_acknwldgmnt_sid'
FROM DUAL;
SELECT ptr.athrztn_infrmtn_qlfr
|| ','
|| ptr.athrztn_infrmtn
|| ','
|| ptr.scrty_infrmtn_qlfr
|| ','
|| ptr.scrty_infrmtn
|| ','
|| ptr.intrchng_sndr_idntfr_qlfr
|| ','
|| ptr.intrchng_sndr_idntfr
|| ','
|| ptr.intrchng_rcvr_idntfr_qlfr
|| ','
|| ptr.intrchng_rcvr_idntfr
|| ','
|| ptr.intrchng_date
|| ','
|| ptr.intrchng_cntrl_stndrds_idntfr
|| ','
|| ptr.intrchng_cntrl_vrsn_nmbr
|| ','
|| ptr.intrchng_cntrl_nmbr
|| ','
|| ptr.acknwldgmnt_rqstd_indctr
|| ','
|| ptr.usg_indctr
|| ','
|| ptr.cmpnt_elmnt_sprtr
|| ','
|| ptr.fnctnl_idntfr_code
|| ','
|| ptr.aplctn_sndr_code
|| ','
|| ptr.applctn_rcvr_code
|| ','
|| ptr.fnctnl_grp_crtn_date
|| ','
|| ptr.fnctnl_grp_crtn_date
|| ','
|| ptr.grp_cntrl_nmbr
|| ','
|| ptr.rspnsbl_agncy_code
|| ','
|| ptr.vrsn_rls_indstry_idntfr_code
|| ','
|| ptr.trnsctn_set_idntfr_code
|| ','
|| ptr.trnsctn_set_cntrl_nmbr
|| ','
|| ptr.implementation_guide_vrsn_name
|| ','
|| ptr.hierarchical_structure_code
|| ','
|| ptr.trnsctn_set_purpose_lkpcd
|| ','
|| ptr.sbmtr_trnsctn_idntfr
|| ','
|| ptr.trnsctn_set_creation_date
|| ','
|| ptr.trnsctn_type_code
|| ','
|| ptr.trnsctn_segment_count
|| ','
|| ptr.included_trnsctn_sets_count
|| ','
|| ptr.included_fnctnl_groups_count
|| ','
|| ia.input_acknwldgmnt_sid
|| ','
FROM pa_transaction_request ptr, input_acknwldgmnt ia, input_batch_file ibf
WHERE ptr.input_acknwldgmnt_sid = ia.input_acknwldgmnt_sid
AND ia.input_batch_file_sid = ibf.input_batch_file_sid
AND ibf.original_file_name =
'HIPAA.165760000.20110518I001.278_GC04-3.dat';
SELECT 'pa_rqst_sid,NM01,NM02,NM108,NM109'
FROM DUAL;
Hi ,
Save your query in a file and execute that file
SQL>@a.aqlHope this helps
Regards,
Achyut -
Problem in csv file generation
Hi ,
I am trying to spool a query in csv format using "col sep ,"
but it is giving problem for the text values like example i have a sql_text column which gives the sql text. in the csv file which was generated the select statement columns are going to next column where the comma is invoked.
is there any way to format while generating csv file that entire sql_text should come in one column
Thanks
Rakeshset echo OFF pages 50000 lin 32767 feed off heading ON verify off newpage none trimspool on
define datef=&1
define datet=&2
set colsep ','
spool querries.csv
SELECT s.parsing_schema_name,
p.instance_number instance_number,
s.sql_id sql_id,
x.sql_text sql_text,
p.snap_id snap_id,
TO_CHAR (p.begin_interval_time,'mm/dd/yyyy hh24:mi') begin_interval_time,
TO_CHAR (p.end_interval_time,'mm/dd/yyyy hh24:mi') end_interval_time,
s.elapsed_time_delta / DECODE (s.executions_delta, 0, 1, s.executions_delta) / 1000000 elapsed_time_per_exec,
s.elapsed_time_delta / 1000000 elapsed_time,
s.executions_delta executions, s.buffer_gets_delta buffer_gets,
s.buffer_gets_delta / DECODE (s.executions_delta, 0, 1, s.executions_delta) buffer_gets_per_exec,
module module
FROM dba_hist_sqlstat s, dba_hist_snapshot p, dba_hist_sqltext x
WHERE p.snap_id = s.snap_id
AND p.dbid = s.dbid
AND p.instance_number = s.instance_number
AND p.begin_interval_time >
TO_TIMESTAMP ('&datef','yyyymmddhh24miss')
AND p.begin_interval_time <
TO_TIMESTAMP ('&datet','yyyymmddhh24miss')
AND s.dbid = x.dbid
AND s.sql_id = x.sql_id
ORDER BY instance_number, elapsed_time_per_exec DESC ;
SPOOL OFF;
exit; -
SQL bulk copy from csv file - Encoding
Hi Experts
This is the first time I am creating a PowerShell script and it is almost working. I just have some problems with the actual bulk import to SQL encoding from the text file since it replaces
special characters with a question mark. I have set the encoding when creating the csv file but that does not seem to reflect on the actual bulk import. I have tried difference scenarios with the encoding part but I cannot find the proper solution for that.
To shortly outline what the script does:
Connect to Active Directory fetching all user - but excluding users in specific OU's
Export all users to a csv in unicode encoding
Strip double quote text identifiers (if there is another way of handling that it will be much appreciated)
Clear all records temporary SQL table
Import records from csv file to temporary SQL table (this is where the encoding is wrong)
Update existing records in another table based on the records in the temporary table and insert new record if not found.
The script looks as the following (any suggestions for optimizing the script are very welcome):
# CSV file variables
$path = Split-Path -parent "C:\Temp\ExportADUsers\*.*"
$filename = "AD_Users.csv"
$csvfile = $path + "\" + $filename
$csvdelimiter = ";"
$firstRowColumns = $true
# Active Directory variables
$searchbase = "OU=Users,DC=fabrikam,DC=com"
$ADServer = 'DC01'
# Database variables
$sqlserver = "DB02"
$database = "My Database"
$table = "tblADimport"
$tableEmployee = "tblEmployees"
# Initialize
Write-Host "Script started..."
$elapsed = [System.Diagnostics.Stopwatch]::StartNew()
# GET DATA FROM ACTIVE DIRECTORY
# Import the ActiveDirectory Module
Import-Module ActiveDirectory
# Get all AD users not in specified OU's
Write-Host "Retrieving users from Active Directory..."
$AllADUsers = Get-ADUser -server $ADServer `
-searchbase $searchbase -Filter * -Properties * |
?{$_.DistinguishedName -notmatch 'OU=MeetingRooms,OU=Users,DC=fabrikam,DC=com' `
-and $_.DistinguishedName -notmatch 'OU=FunctionalMailbox,OU=Users,DC=fabrikam,DC=com'}
Write-Host "Users retrieved in $($elapsed.Elapsed.ToString())."
# Define labels and get specific user fields
Write-Host "Generating CSV file..."
$AllADUsers |
Select-Object @{Label = "UNID";Expression = {$_.objectGuid}},
@{Label = "FirstName";Expression = {$_.GivenName}},
@{Label = "LastName";Expression = {$_.sn}},
@{Label = "EmployeeNo";Expression = {$_.EmployeeID}} |
# Export CSV file and remove text qualifiers
Export-Csv -NoTypeInformation $csvfile -Encoding Unicode -Delimiter $csvdelimiter
Write-Host "Removing text qualifiers..."
(Get-Content $csvfile) | foreach {$_ -replace '"'} | Set-Content $csvfile
Write-Host "CSV file created in $($elapsed.Elapsed.ToString())."
# DATABASE IMPORT
[void][Reflection.Assembly]::LoadWithPartialName("System.Data")
[void][Reflection.Assembly]::LoadWithPartialName("System.Data.SqlClient")
$batchsize = 50000
# Delete all records in AD import table
Write-Host "Clearing records in AD import table..."
Invoke-Sqlcmd -Query "DELETE FROM $table" -Database $database -ServerInstance $sqlserver
# Build the sqlbulkcopy connection, and set the timeout to infinite
$connectionstring = "Data Source=$sqlserver;Integrated Security=true;Initial Catalog=$database;"
$bulkcopy = New-Object Data.SqlClient.SqlBulkCopy($connectionstring, [System.Data.SqlClient.SqlBulkCopyOptions]::TableLock)
$bulkcopy.DestinationTableName = $table
$bulkcopy.bulkcopyTimeout = 0
$bulkcopy.batchsize = $batchsize
# Create the datatable and autogenerate the columns
$datatable = New-Object System.Data.DataTable
# Open the text file from disk
$reader = New-Object System.IO.StreamReader($csvfile)
$columns = (Get-Content $csvfile -First 1).Split($csvdelimiter)
if ($firstRowColumns -eq $true) { $null = $reader.readLine()}
Write-Host "Importing to database..."
foreach ($column in $columns) {
$null = $datatable.Columns.Add()
# Read in the data, line by line
while (($line = $reader.ReadLine()) -ne $null) {
$null = $datatable.Rows.Add($line.Split($csvdelimiter))
$i++; if (($i % $batchsize) -eq 0) {
$bulkcopy.WriteToServer($datatable)
Write-Host "$i rows have been inserted in $($elapsed.Elapsed.ToString())."
$datatable.Clear()
# Add in all the remaining rows since the last clear
if($datatable.Rows.Count -gt 0) {
$bulkcopy.WriteToServer($datatable)
$datatable.Clear()
# Clean Up
Write-Host "CSV file imported in $($elapsed.Elapsed.ToString())."
$reader.Close(); $reader.Dispose()
$bulkcopy.Close(); $bulkcopy.Dispose()
$datatable.Dispose()
# Sometimes the Garbage Collector takes too long to clear the huge datatable.
[System.GC]::Collect()
# Update tblEmployee with imported data
Write-Host "Updating employee data..."
$queryUpdateUsers = "UPDATE $($tableEmployee)
SET $($tableEmployee).EmployeeNumber = $($table).EmployeeNo,
$($tableEmployee).FirstName = $($table).FirstName,
$($tableEmployee).LastName = $($table).LastName,
FROM $($tableEmployee) INNER JOIN $($table) ON $($tableEmployee).UniqueNumber = $($table).UNID
IF @@ROWCOUNT=0
INSERT INTO $($tableEmployee) (EmployeeNumber, FirstName, LastName, UniqueNumber)
SELECT EmployeeNo, FirstName, LastName, UNID
FROM $($table)"
try
Invoke-Sqlcmd -ServerInstance $sqlserver -Database $database -Query $queryUpdateUsers
Write-Host "Table $($tableEmployee) updated in $($elapsed.Elapsed.ToString())."
catch
Write-Host "An error occured when updating $($tableEmployee) $($elapsed.Elapsed.ToString())."
Write-Host "Script completed in $($elapsed.Elapsed.ToString())."I can see that the Export-CSV exports into ANSI though the encoding has been set to UNICODE. Thanks for leading me in the right direction.
No - it exports as Unicode if set to.
Your export was wrong and is exporting nothing. Look closely at your code:
THis line exports nothing in Unicode"
Export-Csv -NoTypeInformation $csvfile -Encoding Unicode -Delimiter $csvdelimiter
There is no input object.
This line converts any file to ansi
(Get-Content $csvfile) | foreach {$_ -replace '"'} | Set-Content $csvfile
Set-Content defaults to ANSI so the output file is converted.
Since you are just dumping into a table by manually building a recorset why not just go direct. You do not need a CSV. Just dump theresults of the query to a datatable.
https://gallery.technet.microsoft.com/scriptcenter/4208a159-a52e-4b99-83d4-8048468d29dd
This script dumps to a datatable object which can now be used directly in a bulkcopy.
Here is an example of how easy this is using your script:
$AllADUsers = Get-ADUser -server $ADServer -searchbase $searchbase -Filter * -Properties GivenName,SN,EmployeeID,objectGUID |
Where{
$_.DistinguishedName -notmatch 'OU=MeetingRooms,OU=Users,DC=fabrikam,DC=com'
-and $_.DistinguishedName -notmatch 'OU=FunctionalMailbox,OU=Users,DC=fabrikam,DC=com'
} |
Select-Object @{N='UNID';E={$_.objectGuid}},
@{N='FirstName';Expression = {$_.GivenName}},
@{N='LastName';Expression = {$_.sn}},
@{N=/EmployeeNo;Expression = {$_.EmployeeID}} |
Out-DataTable
$AllDUsers is now a datatable. You can just upload it.
¯\_(ツ)_/¯ -
How to create .csv file from ABAP report
Hi
We have a requirement to generate .csv file from abap report.
Currently user saves data from abap report to spreadsheet(.xls format) in desktop. Then opens excel file and save as .csv format. Need option to save directly in .csv format instead of .xls format.
Please let me know, if there is any standard function module available to create .csv file.
Regards
UmaI tried with your code it's going to dump
REPORT ZTEMP101 message-id 00.
tables: lfa1.
types: begin of t_lfa1,
lifnr like lfa1-lifnr,
name1 like lfa1-name1,
end of t_lfa1.
data: i_lfa1 type standard table of t_lfa1,
wa_lfa1 type t_lfa1.
types truxs_t_text_data(4096) type c occurs 0.
data: csv_converted_table type table of TRUXS_T_TEXT_DATA.
select-options: s_lifnr for lfa1-lifnr.
select lifnr name1 from lfa1 into table i_lfa1
where lifnr in s_lifnr.
CALL FUNCTION 'SAP_CONVERT_TO_CSV_FORMAT'
EXPORTING
I_FIELD_SEPERATOR = ';'
I_LINE_HEADER =
I_FILENAME =
I_APPL_KEEP = ' '
TABLES
I_TAB_SAP_DATA = I_LFA1
CHANGING
I_TAB_CONVERTED_DATA = csv_converted_table
EXCEPTIONS
CONVERSION_FAILED = 1
OTHERS = 2
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
CALL FUNCTION 'WS_DOWNLOAD'
EXPORTING
BIN_FILESIZE = ' '
CODEPAGE = ' '
FILENAME =
'C:\Documents and Settings\ps12\Desktop\Test folder\exl.cvs'
FILETYPE = 'DAT'
MODE = ' '
WK1_N_FORMAT = ' '
WK1_N_SIZE = ' '
WK1_T_FORMAT = ' '
WK1_T_SIZE = ' '
COL_SELECT = ' '
COL_SELECTMASK = ' '
NO_AUTH_CHECK = ' '
IMPORTING
FILELENGTH =
TABLES
DATA_TAB = csv_converted_table
FIELDNAMES =
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10
IF SY-SUBRC <> 0.
MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
my version is 4.6c -
Open Hub Destination - csv files
Hi all,
I nedd to send information through Open Hub Destination from a cube to a File in a server. But the Open Hub Destination is originating 2 csv files instead of 1: one of the files has just the name of the fields that will be in the columns and the other file only the data in the columns.
There is an alternative to generate through Open Hub Destination, only one file, with the headers in the columns?
Can you explain to me how to do it?
There are other options to generate csv files, automaticaly from cubes,and send them to other applications?
Thanks a lot.
Regards.
Elisabete DuarteHi,
To send information from a cube to applicatin server thru OHS,
two files will be formed while using OHS. One which will be saved inthe aplication server and for other u should give the path. For downloadin the file to the applc server, in the destination tab, select applcn server option and transform it
Hopeit helps
Cheers
Raj
Message was edited by:
Raj
Maybe you are looking for
-
USB HD no longer available in Time Machine
I have had a USB HD attached to my Airport Extreme, in use as a backup disk for Time Machine. This has worked fine for over a year, but I recently noticed that backups were no longer being completed due a full disk. When I look in Time Machine and
-
Hello How can I modify this script to target all Users OU in one domain to move all disabled accounts from each Users OU to move to the Disabled Users OU? Search-ADAccount –AccountDisabled –UsersOnly –SearchBase "OU=Users,OU=test,OU=domain,DC=domain,
-
Needs steps, key points while undeploying & unclustering an bpm engine
Hi, We are using Oracle 10G 10.3.2 in a clustered weblogic version with clustered servers - each catering to different workflows. Need to undeploy a process which is currently deployed in one of the cluster engine What are the steps/ key things to be
-
HT1918 Change of id email......what a mess?
I have tried to change my id email. In doing so I ended up making a new account and can't get into my old one. When I sign in to the old one my security questions are wrong - even my date of birth, and no comfirmation email will send. I need the old
-
Where can I give feedback re: hidden x in tabs
Can someone point me to the Safari feedback form? Apple does a really good job of hiding the feedback pages. I can never find those doggone things; they always seem to be concealed deep down inside the support pages, or maybe I'm just not looking rig