get ERROR "Internal tables cannot be used as work areas" inside of method - sql

I am new with ABAP. I asked a similar, but different, question to this one yesterday.
I duplicate a table (= table) to a local table (= localTable) and remove all duplicates in it, this works fine (first 3 code lines)
Now I want to loop over this local table and send all matching data into an structure with INTO CORRESPONDING FIELDS OF - unfortunately I always get the following error:
Internal tables cannot be used as work areas.
INFO: I'm working inside of a method!
Here is my code where I'm working with:
DATA localTable TYPE STANDARD TABLE OF table.
SELECT columnName FROM table INTO TABLE localTable.
DELETE ADJACENT DUPLICATES FROM localTable COMPARING columnName.
LOOP AT localTable ASSIGNING FIELD-SYMBOL(<fs_table>).
SELECT * FROM anotherTable as p
WHERE p~CN1 = #localVariable
AND p~CN2 = #<fs_table>-columnName
INTO CORRESPONDING FIELDS OF #exportStructure "<-- Here I always get my error
ENDSELECT.
ENDLOOP.

First: I've read that I have to sort my internal table before using command DELETE ADJACENT DUPLICATES FROM localTable COMPARING columnName. so I've added following code line in between:
SORT localTable BY columnName ASCENDING.
Second: Instead of using INTO CORRESPONDING FIELDS OF TABLE I've used APPENDING CORRESPONDING FIELDS OF TABLE because INTO overwrites every line with itself, so in total I have only one line in my exported structure.
APPENDING adds a new line every time my statements are true.

Related

JOIN of DB table and internal table produces "is not defined in the ABAP Dictionary"

First: I'm working on a existing code and want to add some new stuff to it and I'm really new to ABAP
My goal: I want to duplicate an existing table and remove all values that occur multiple times. That - at least I think - works. Afterwards I want to INNER JOIN this new created table with another already existing table, but unfortunately I always get the following error:
Method MethodName "newCreatedTable" is not defined in the ABAP Dictionary as a table, projection view, or database view
Additional Info: As you can see I'm working inside of a method!
Here is my code what I've done so far:
creating new table and delete all duplicates
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName
FROM existingTable INTO TABLE newCreatedTable.
DELETE ADJACENT DUPLICATES
FROM newCreatedTable COMPARING columnName.
here is where the error happens
SELECT *
FROM anotherExistingTable as p
INNER JOIN newCreatedTable as t on t~columnName = p~columnName
...
I hope someone can help me out in this case! Thank You in advance!
If I'm not wrong your code looks like this:
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName FROM existingTable INTO TABLE newCreatedTable.
DELETE ADJACENT DUPLICATES
FROM newCreatedTable COMPARING columnName.
SELECT *
FROM anotherExistingTable
INNER JOIN newCreatedTable as t on t~columnName = p~columnName
If this is the case then you cannot make a SELECT on an internal table. You can only make this operation on a table that exists in the ABAP dictionary.
Your code should look like this:
DATA newCreatedTable TYPE STANDARD TABLE OF existingTable.
SELECT columnName
FROM existingTable INTO TABLE #newCreatedTable.
DELETE ADJACENT DUPLICATES FROM newCreatedTable COMPARING columnName.
SELECT *
FROM anotherExistingTable
FOR ALL ENTRIES IN #newCreatedTable " <-------------- Use FOR ALL ENTRIES
WHERE columnName = #newCreatedTable-columnName. " <-- in your code

how to SAFE_CONVERT_BYTES_TO_STRING and REPLACE?

i got a table with three columns whose type is BYTES
i succeed to make a SELECT SAFE_CONVERT_BYTES_TO_STRING
but now, i wish i could REPLACE my BYTES values by their corresponding STRING in my original table
Replacing the value would imply changing the column type, which is not supported.
My recommendation would be to save your query results to a different destination table then drop the current table and copy the destination table to rename it as the original one.

Dynamic Way to Insert Data into SQLite Table When Column Counts Change

I am working on a script using SQLite where there is a flux in the number of columns that are available to be inserted into a table I am creating to later do a join on.
The table I am created to insert the data into has 97 columns, the data coming in from my feed can range from around 80 all the way up to that 97th column.
The error I get is SQLITE_ERROR: table allPositionsTable has 97 columns but 80 values were supplied and is the one I am trying to avoid by figuring out a way where this doesn't happen.
Are there any workarounds or tricks I can use to have SQLite function so that it will always include the columns where there is no data for them or dynamically not include them so the error goes away?
The error I get is SQLITE_ERROR: table allPositionsTable has 97
columns but 80 values were supplied and is the one I am trying to
avoid by figuring out a way where this doesn't happen.
This happens because you are using the default column list (i.e. by not specifying the columns into which the values are to be placed)
That is you would be coding the equivalent of INSERT INTO your_table VALUES(.......
so in the absence of a list of columns you are saying that you will provide a value for all columns in the table and hence the message when a value or values are not present.
What you want to do is use INSERT INTO your_table_name (your_comma_separated_list_of_columns_to_be_inserted) VALUES(.......
where your_table_name and your_comma_separated_list_of_columns_to_be_inserted would be replaced with the appropriate values.
See the highlighted section of the INSERT syntax that can be found at SQL As Understood By SQLite - INSERT
and the respective section from the above link is :-
The first form (with the "VALUES" keyword) creates one or more new
rows in an existing table.
If the column-name list after table-name is
omitted then the number of values inserted into each row must be the
same as the number of columns in the table.
In this case the result of
evaluating the left-most expression from each term of the VALUES list
is inserted into the left-most column of each new row, and so forth
for each subsequent expression.
If a column-name list is specified,
then the number of values in each term of the VALUE list must match
the number of specified columns.
Each of the named columns of the new
row is populated with the results of evaluating the corresponding
VALUES expression.
Table columns that do not appear in the column list
are populated with the default column value (specified as part of the
CREATE TABLE statement), or with NULL if no default value is
specified.

Get values based on newly inserted value using SQL

I want to make filtration on a column after selecting a specific value of another column in the same table, I tried to use #... special character followed by the column's name to get the address of this value.
My SQL statement is like the following :
SELECT ATTRIBUTE FROM TABLE WHERE FIELD = '#FIELDNAME';
If I used a specific value instead of #FIELDNAME, it will work properly but it will be static but I need it to be dynamic based on the selected value.
Create another table which will have the list of values that are in the FIELDNAME and give each record a unique id ,then retrieve the value depending on what you have selected by the name of the new table's field preceded by '#...'
I don't know if that what are you looking for, please let me know.
If no triggers are allowed, do you have any date/time column in the table? Is it possible to have that extra column anyway to see the time of a newly inserted row?
You may have to check the lastest row entered, save its field value into a variable. Then do the select based on the variable value.
Based on the vague last row id you could try the following (it's not pretty). But again, if you have date/time that's more accurate.
select attribute from table
where field = (select field from table
where rowid =(select max(rowid) from table))
;
upate
Do you have the priviledge to set up your insert command as below:
insert into table (id, col1, col2,...) values (1,'something', 'something',...)
returning id into variable; -- you may either save field or id depending on your table
Then you may use this variable to select the records you want.

SQL: I need to take two fields I get as a result of a SELECT COUNT statement and populate a temp table with them

So I have a table which has a bunch of information and a bunch of records. But there will be one field in particular I care about, in this case #BegAttField# where only a subset of records have it populated. Many of them have the same value as one another as well.
What I need to do is get a count (minus 1) of all duplicates, then populate the first record in the bunch with that count value in a new field. I have another field I call BegProd that will match #BegAttField# for each "first" record.
I'm just stuck as to how to make this happen. I may have been on the right path, but who knows. The SELECT statement gets me two fields and as many records as their are unique #BegAttField#'s. But once I have them, I haven't been able to work with them.
Here's my whole set of code, trying to use a temporary table and SELECT INTO to try and populate it. (Note: the fields with # around the names are variables for this 3rd party app)
CREATE TABLE #temp (AttCount int, BegProd varchar(255))
SELECT COUNT(d.[#BegAttField#])-1 AS AttCount, d.[#BegAttField#] AS BegProd
INTO [#temp] FROM [Document] d
WHERE d.[#BegAttField#] IS NOT NULL GROUP BY [#BegAttField#]
UPDATE [Document] d SET d.[#NumAttach#] =
SELECT t.[AttCount] FROM [#temp] t INNER JOIN [Document] d1
WHERE t.[BegProd] = d1.[#BegAttField#]
DROP TABLE #temp
Unfortunately I'm running this script through a 3rd party database application that uses SQL as its back-end. So the errors I get are simply: "There is already an object named '#temp' in the database. Incorrect syntax near the keyword 'WHERE'. "
Comment out the CREATE TABLE statement. The SELECT INTO creates that #temp table.