Check if column X exists in table Y - abap

I have two string variables:
lv_table_name contains a table name
lv_column_name contains a column name
Is there a way to check if there is a table (or view) which has the given column?

You can find table and view definitions in table DD03L. If you can access the table with your mentioned combination table/column it will be noticably faster.
REPORT.
DATA: lv_column_name TYPE string VALUE 'MY_FIELD'.
"this will tell you which tables/views exist containing the column 'MY_FIELD'
SELECT tabname
FROM dd03l INTO TABLE #DATA(lt_tables)
WHERE fieldname EQ #lv_column_name.

For Netweaver 7.5, you can use a simple OPEN SQL select on DD03L
https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abensql_expr_literal_abexa.htm
SELECT SINGLE #abap_true
FROM DD03L
WHERE tabname EQ #lv_table_name AND fieldname EQ #lv_column_name
INTO #DATA(lv_exists).

Related

Get column names stored in a different table

I have a table that has just a code for the individual column name like "A1G", "Z8H" etc.
This table is a really huge table. Therefore it would not help to manually add the alias name in the SELECT Statement like.
The description for each column code is stored in a different table:
Now I would like to SELECT * from the first table but with the right column header name.
This is stored in the second table within the filters Schema = 'ABC' and Table = 'A'.
That would be desired output:
How would you do that in SQL?
How can I change the column name?
You would be better off just creating a view with all the columns aliased to your preferred names. Once that's done you can select from the view and get the data back with the headings you want.
Look into Inner Join
Or Left Join.

AWS Athena create table from select query

I am trying to create table using the query below. If I do not create table, but just run the part from SELECT *, the query can be run.
(SELECT *
FROM "MyDatabase"."2007" A
WHERE A."column name a" NOT IN ('U','A+','A','A-')
AND A."column name b" NOT IN ('SHH','CTP')
AND NOT EXISTS
(SELECT *
FROM "MyDatabase"."2008" B
WHERE (B."column name a" = A."column name a"
AND B."column name b" = A."column name b"
AND B."column name c" = A."column name c")))
The error message is "GENERIC_INTERNAL_ERROR: field ended by ';': expected ';' but got 'partOfAColName' at line 1:..."
From google search, space in column names seems to be the problem. But I am not sure. I have space in column names. The column names are automatically detected by Glue Crawler. So I am not sure if I can do anything about it. I have around 20 columns, all having space in the middle though. Could someone suggest a fix? Thanks.
When you execute CREATE TABLE AS...you are telling Athena to create a table with the same column names in your SELECT, but in this case those column names contain spaces, and Athena won't allow you to create a column name with a space. To avoid this you can create the table with column names that adhere to the Athena's specifications then populate that table with INSERT INTO SELECT...FROM

SQL: is it normal to use column name with schema name?

Lets suppose I have two schemas in one DB: public and private. In both schemas I have the same table - my_table with the same columns. So is it normal to do the following:
SELECT public.my_table.my_col FROM public.my_table?
I am trying to do it with H2 but get exception in ResultSet - column not found. Is it not normal or it's not normal for H2?
You should write:
SELECT my_col FROM public.my_table
since column names are already evaluated in the table(s) specified in the query.

Query to find and replace a specific text in SQL Database (All tables and All Columns)

I have a DB with +100 tables, most tables have a column that saves a URL but with different "Column Name" and URL is different as well. e.g.
In one table its
usa3030.Development.com/portal/mybook/chapter1/page1/line1
and in another one its
usa3030.Development.com/portal/mybook/chapter1
and so on, but for sure each URL have "usa3030.Development.com/portal/mybook/chapter1" in them
So I want a query that Will find each column that have text within column data (with "*" I think we call wildcard)
"usa3030.Development.com/portal/mybook/chapter1"
and replace it with
usa3030.Development.com/MyWorld/Chp1
Answer
Search Or replace Stored Procedure
Here I guess you have to go through the following steps:
DECLARE #string varchar(max)
For each table in sys.Objects (where type='U')
For each column of the above table
IF EXISTS(select * from tablename where colname like '%'+#string+'%')
update table tablename SET colnmae=REPLACE(colname,'old','new') where colname like '%'+#string+'%')

In sqlite How to add column in table if same column is not exists in table

How can I add a column in an SQLite table if and only if the same column does not exist in the table?
Using ALTER TABLE I am able to create a new column but want to know how to check whether that column already exists in the table or not?
SQLite returns an error like "no such column: foo" if the table doesn't contain the column:
select foo from yourTable limit 1
Also you can get the create-table statement:
select sql from sqlite_master where tbl_name = 'YourTableName'
and then parse the result, looking for the column-name. I don't know of an elegant way to query the list of columns for a specified table, though one may exist.
Also if you attempt to do this:
alter table YourTable add column foo {column-def whatever it is}
you get an error from SQLite if the column already exists. You could trap that error too.
Finally you could do this:
select sql from sqlite_master
where tbl_name = 'YOURTABLE' and sql like '%"foo" CHAR%'; -- or whatever type
and if the specified table contains the column which is surrounded by double-quotes in the query, and with the type you have specified, you will get a result, otherwise an empty set. Specifying the datatype ensures that your LIKE substring match occurs on a column-name.
There's no way (that I know of) to do it all in a single SQLite query. You must use application code to manage the If/Elseness.
Check if table exists or not:
select count(*) from sqlite_master where type = 'table' and name = MyTable';
Check if column exists in table or now
pragma table_info(thumbnail);
However, a better approach may be explicit database schema updates based on schema versions your application maintains (e.g. specific alter table statement to go from schema version 1 to 2):
pragma user_version;
It seems like that it is impossible to do checking if the column not exists and addindg the new column in one command, because Sqlite don't support "IF NOT EXISTS" for column. "IF NOT EXISTS" works only on table.
Here is what I will do:
rev = ExecuteStatement("SELECT columnNamexx FROM tableNamexx limit 1;");
if(rev != SQLITE_OK){ // add col to table
ExecuteStatement("ALTER TABLE tableNamexx ADD COLUMN columnNamexx INTEGER DEFAULT 0;");
}
You can view the table columns by using '.schema tableName'