Subscribe to this feed

Navigation

Recent Posts

Archive

Popular Tags

Click tag to retrieve blogs
  1. Excel (9)
  2. Oracle (6)
  3. csv (5)
  4. batch files (5)
  5. PHP (3)
  6. SQLPLus (3)
  7. VBA (3)
  8. Apache (3)
  9. DOS (3)
  10. SQL*Plus (3)
  11. Macro (3)
  12. bat (3)
  13. data migration (2)
  14. System Implementation (2)
  15. MySQL (2)
  16. Microsoft (2)
  17. Windows (2)
  18. new systems (2)
  19. SQL (2)
  20. Updates (1)
  21. Prince2 (1)
  22. MSDOS (1)
  23. Import file (1)
  24. ISERR (1)
  25. testing (1)

Accept User Input in SQL*Plus

Saturday 05 May, 2007 - 17:11pm | 0 comments |

You can allow users to enter parameters in SQLPlus queries by using accept and prompt. An example of the syntax is

accept startdate prompt "Enter Start Date: "

startdate is the parameter value and the where condition would then include the parameter as follows

WHERE date >= '&startdate'

The example below I saved for use in a batch file. A user could then click the batch file which would open SQLPlus and prompt the user for the query parameters including a "Save File As" prompt, as the output was generating a csv file. The batch file icon can be changed in Windows to something other than the default, something more meaningful for the user.

The appearance might be very retro, but its a simple tool of the "cheap and cheerful" variety and the user is, after all, more interested in the output file.

The Retro SQL*Plus Screen

accept filesave prompt "Save File As: "
accept code prompt "Enter Carrier Code: "
accept startdate prompt "Enter Start Date: "
accept enddate prompt "Enter End Date: "
SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET WRAP OFF
SET LINESIZE 1000
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF
SPOOL C:\reports\'&filesave'.csv;
SELECT /* The purpose of this query is to ...... Always good policy to annotate queries */
carrier_id, carrier_name, calls, minutes, turnover FROM
carriers_data
WHERE
trunc(adj_start_time) >= '&startdate'
AND trunc(adj_start_time) < '&enddate'
AND carrier_id = '&code'

Tags:SQL*Plus |Oracle |CSV

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

Exit SQLPlus from Batch File

Saturday 05 May, 2007 - 12:57pm | 0 comments |

If you have a batch file set to run multiple SQLPlus queries and to then perform some other action such as merge the results together into a single file, or copy the files elsewhere you need to physically exit SQLPlus or the batch file will not continue to run and execute the commands.

To exit SQLPlus automatically skip a line after your last query and type exit as in

@C:\Queries\Query_1.sql
@C:\Queries\Query_2.sql
@C:\Queries\Query_3.sql
@C:\Queries\Query_4.sql

EXIT

Tags:SQLPlus |Oracle |DOS |bat |batch file

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

Run Multiple SQLPlus Queries via DOS batch file

Saturday 05 May, 2007 - 12:51pm | 5 comments |

To run multiple SQLPlus queries as part of a DOS batch file place all the queries in a separate SQL file. For Example

@C:\Queries\Query_1.sql
@C:\Queries\Query_2.sql
@C:\Queries\Query_3.sql
@C:\Queries\Query_4.sql

Save the file as runall.sql. Then use this file in your batch file

@Echo Off
sqlplus [login details] @C:\queries\runall.sql
EXIT

Tags:SQLPlus |bat |DOS |Oracle

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

Use Query Result as Variable in Batch File

Tuesday 01 May, 2007 - 10:47am | 0 comments |

You can use the result of a SQLPlus query as a variable in a windows batch file by spooling the output to a .bat file

SET NEWPAGE 0
SET SPACE 0
SET PAGESIZE 0
SET WRAP OFF
SET LINESIZE 1000
SET ECHO OFF
SET FEEDBACK OFF
SET VERIFY OFF
SET HEADING OFF
SET MARKUP HTML OFF
SPOOL c:cdr_count.bat;
select
'@echo off' from dual
UNION ALL
select 
'SET CDR='||''||count(ddi) from cdrs where trunc(sysdate) = trunc(adj_start_time);
SPOOL OFF;

This creates or replaces a batch file and outputs

@echo off
SET CDR=643950

I am then able to call that batch file from within another batch file and use the SQL query result as it is defined as a variable.

call c:cdr_count.bat
IF %CDR% EQU 0 GOTO END

Tags:SQLPLus |Oracle |DOS |Batch files

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

Oracle SQL*Plus Last Full Month

Saturday 24 Mar, 2007 - 18:15pm | 1 comments |

Although Crystal Reports has a built in LastFullMonth function SQL*Plus doesn't.

If you want to use SQL*Plus for your report and want the date parameter to be dynamic you can do so by manipulating sysdate and using some of the built in date functions.

Here is how

select * from db_table
where date_col between add_months((last_day(sysdate))+1,-2) AND add_months((last_day(sysdate))+1,-1)

Tags:Oracle |SQL*Plus |Dates |Crystal Reports

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

SQL*Plus Spool File

Saturday 24 Mar, 2007 - 12:40pm | 1 comments |

If you want to produce a csv file from a SQL select query in SQL*Plus use the following syntax.

set echo off
set heading off
set feedback off
set pagesize 0
spool c:\filename.csv
select col1||','||col2 from db_table;
spool off

This will produce a csv formatted file. Straight to excel, change csv to xls but bear in mind two potential issues (a) row limit in Excel, and (b) leading zeros in number fields will be lost in excel.

The leading zeros can be resolved if you instead spool the file as a txt as you will be forced to use excels conversion wizard to open the file.

Tags:Oracle |SQL |SQL*Plus |csv

Comment
 | Link | Back to top | del.icio.us digg it furl reddit

© Eriginal Ltd 2011, all rights reserved