Creation of External Tables - Automation in Oracle - sql

I was wondering if you help me with the following question and give me some guidelines. Before telling me that it is more of a google question, I would like to say that I am new to this topic which makes googling a bit hard.
Question:
I was wondering if it is possible to automate the creation of an external table by reading the first row of a txt file? I was reading something about UTL_FILE however I am not sure if it is correct approach. I am interested into this as sometimes I have txt files with 50+ columns and the manual creation of such an external table would take pretty long.
Any help or guidelines would be appreciated!
Thank you in advance!

You could create an external table that reads the whole line in one long VARCHAR2. Then SELECT from this table, read the first line, and you get a string with the column names. Stick this string inside a 'CREATE ...' statement and execute it.

Related

Error "not mutually convertible in Unicode program" when adding line to table

I'm trying to add data from a internal table to a custom one.
DATA: BEGIN OF TMP_CTRYGRP_T OCCURS 1000,
CTYGR TYPE /SAPSLL/CTYGR,
TEXT1 TYPE /SAPSLL/TEXT60,
END OF TMP_CTRYGRP_T.
SELECT ctygr, text1 FROM /SAPSLL/CTYGPT INTO TABLE #DATA(lt_countryGroupsTable)
LOOP AT lt_countryGroupsTable ASSIGNING FIELD-SYMBOL(<ls_countryGroups>).
APPEND <ls_countryGroups> TO TMP_CTRYGRP_T.
ENDLOOP.
Then I want to add the line in a custom Table Type ZZ_T_TAB
So I've tried to create a field-symbol of this table, creating an internal table from it, but none of the solutions I've tried was permitting me to add lines in that Custom table (even if the one in the program had the lines).
The problem I mainly encountered was:
are not mutually convertible in a Unicode program.
So my questions are:
Why does that error happen? Googling it didn't provide me an understandable answer
For the moment I'm using an internal table limited to 1000 rows. But I don't really know by advance the number of lines the search could provide. Is there any way to improve that?
How to add lines from any solution to my ZZ_T_TAB then? And afterwards how could I add other fields in the same table, for the rows already existing?
As some of you maybe understood, I'm quite a rookie in ABAP.
So if there's any useful link to understand all of that I would be happy if you can share it with me.
Why don't you directly select into the table?
Don't use OCCURS as it is declared obsolete and already forbidden in classes.
Declare your own structure as type and mark your custom internal table as TYPE STANDARD TABLE OF struct_type. This way, there will be no upper bounds
TYPES:
BEGIN OF struct_type,
CTYGR TYPE /SAPSLL/CTYGR,
TEXT1 TYPE /SAPSLL/TEXT60,
END OF struct_type.
DATA tmp_ctrygrp_t TYPE STANDARD TABLE OF struct_type WITH EMPTY KEY.
Why does that error happen? Googling it didn't provide me an
understandable answer
You cannot use APPEND with non-identical structures. You have to "convert" it before. Look up for the command MOVE-CORRESPODING in ABAP help (F1 on command in editor).
For the moment I'm using an internal table limited to 1000 rows. But I
don't really know by advance the number of lines the search could
provide. Is there any way to improve that?
Do not use OCCURS extension it is deprecated (as lausek wrote), old syntax.
How then to add lines from any solution to my ZZ_T_TAB ? And
afterwards how could I add other fields in the same table, for the
rows already existing?
You can modify a DB table various ways.:
1, Use UPDATE statement to directly update a field value.
2, Use MODIFY statement to modify field values from a (for example) pre-selected
structure.
Look up the UPDATE and MODIFY command in ABAP help, there are really helpful code examples.

.Net Parsing Fixed Width Data... From a Concatenated, Single, Fixed-Width Column

I was bored and looking at old code that runs like molasses on a cold day. I found that a group of tables in our accounting system - each with 500,000 records of ~20 datapoints - that use a single column of concatenated, fixed-width values instead of separate columns. (Fixing the tables isn't an option.) An old .net ETL project is grabbing all records, doing a bunch of substrings on each record to set an object's corresponding attributes, then sending the object to merge with production data via a stored proc.
The way it is working is fine. It works. And, to be perfectly honest, I doubt I'll be given the go-ahead to fix it even if I come up with a better solution, but I was curious to see if anyone knew of a better way of doing this, because it's not entirely unlikely that I'll face a situation like this in the future.
I was thinking that if there was a way to use the TextFieldParser to parse a static string instead of a file/stream that might be a valid idea. Or, instead, I could write the entire table to a text file and then use the TextFieldParser to send data to the SProc. http://www.dotnetperls.com/textfieldparser does show that TextFieldParser is quite a bit faster than split, which I would assume is tantamount to the string manipulation our project is currently doing with substring. So there may be something to that idea.
Or perhaps the whole, old project should be dumped for a shiny new SSIS project. Would it also have to write the records to a flat file before importing into SQL? Or can it import directly from the table?
Thank you in advance!

Oracle SQL: converting LONG data type into something readable in a DB with no write access

I have a table which contains a field of type LONG. It can contain large blocks of text. I can extract data from this field as follows:
SELECT
NOTES.NOTE_TEXT
FROM
NOTES
WHERE
NOTES.ID = 1
The problem, however, is that I am unable to search for text within the field. If I try the following:
SELECT
NOTES.NOTE_TEXT
FROM
NOTES
WHERE
NOTES.NOTE_TEXT LIKE '%hello%'
... then I get an error: "Exception: DBD, ORA-00932: Inconsistent datatypes: expected NUMBER got LONG".
Of course, I Googled this problem to find many articles about how LONG is very limited, and that you could not search text within it. As to a solution, in several places it was suggested that the LONG could be converted to another data type. CAST AS CLOB was suggested.
I tried:
WHERE
CAST(NOTES.NOTE_TEXT AS CLOB) LIKE '%hello%'
... however this also returned an error.
I also saw some other more complex solutions involving creating functions in PL/SQL. However I do not have access to this. I am using the "Business Objects" application, which is solely a reporting tool for a read-only database. I have no access to create tables or any other kind of write operation.
(Note: the above paragraph explains why the solutions elsewhere on SO do not answer my question, as they require write access to the database. I did not find any other similar question asking exactly what I need, which is to be able to do it in a read-only query.)
Does anyone know of a way in which I can convert LONG to something usable, within a read-only query in Oracle? Thanks.

Convert table data into flat format using of Excel VBA

I have the following input data range
and the following desired output
The first block with Compals will be always the same - so 5 rows. Base Unit Current and Base Unit Later blocks will be variable - sometimes eight options and sometimes more than eight options.
I'm very new in excel vba so unfortunately I have no idea how to do that. Please, could anyone help me or give me some advice. Many thanks in advance.
Wonder you accept this kind of answer of not?
Instead of table, I think this kind of pivot may suitable your need:
I reform the table
Create the pivot table

Script to compare two tables in database, from user input

I am very new to VBA and SQL and am trying to learn. I have a MS Access project that requires a VBA script that prompts the user to input two table names and numerous field names and create a SQL query utilizing those the names.
The specific SQL query I'm trying to use is below.
SELECT
A.user_index, A.input1, B.input1, A.input2, B.input2, A.input3, B.input3, B.input4,
A.input4, A.input5, B.input5
FROM
table1 AS A
LEFT JOIN
table2 AS B ON A.user_index = B.user_index
WHERE
(((A.input1) <> [B].[input1)) OR
(((A.input2) <> [B].[input2])) or
(((A.input4) <> [B].[input4]));
The overall purpose of this is to have a script that will be able to list fields for comparison that is applicable with any database. I know this is probably a relatively easy solution. However, I have no idea where to start.
My first instinct is to say "What have you tried so far?", but as you said, you don't know where to start.
It sounds like you need to first prompt the user for several field and table names, then build a query based on those values. I recommend first outlining exactly what you want your script to do. Maybe something like:
Declare variables to hold the values.
Prompt the user for each of the values and store them in the variables.
2a. After the user enters a value, make sure it is valid. If not, do something accordingly.
Declare a variable to hold your SQL query.
Construct the query.
Run the query.
This is obviously just an example. Break down each step into "baby steps" as much as possible.
It's a good idea to ask yourself how unique these baby steps are to your particular situation (hint: they almost certainly are not unique). If they aren't, then they have probably been solved tens of thousands of times already, so you have a very good chance of googling your questions.
If you still can't find an answer to how to do a particular step, feel free to ask here. Just remember to include your code even if it is broken :)