I'm pretty new to ABAP Coding. I got the task to realize a report that lists all materials, which don't have a specific language key.
For example, the user enters in the selection screen "EN" as a language key. As a result, all materials, which don't have the language key "EN" should be displayed.
There are two tables: MARA (material data) and MAKT (language key = SPRAS), which are connected via the primary key (MATNR).
I tried it this way, but it doesn't work:
SELECT * FROM MARA INNER JOIN MAKT ON MARA~MATNR EQ MAKT~MATNR WHERE MAKT~SPRAS NOT IN S_SPRAS
I assume, that S_SPRAS is select-option with multi-choice.
In that case if you need to display materials which have none of specified languages, use this:
SELECT * INTO TABLE lt_mara
FROM MARA
WHERE NOT EXISTS (
SELECT * FROM MAKT WHERE MAKT~MATNR = MARA~MATNR AND MAKT~SPRAS IN S_SPRAS )
If you need to display materials which don't have at least one of specified languages, it is just more complex and depends on your runtime version - higher 7.50 or lower.
You can also try following SQL statement with LEFT JOIN
select mara~matnr
into table #data(gt_matnr)
from mara
left JOIN makt
on MAKT~MATNR = MARA~MATNR and
MAKT~SPRAS = #s_spras-low
where MAKT~MATNR IS null.
Related
What Im looking to do is select data from a postgres table, which does not appear in another. Both tables have identical columns, bar the use of boolean over Varchar(1) but the issue is that the data in those columns do not match up.
I know I can do this with a SELECT EXCEPT SELECT statement, which I have implemented and is working.
What I would like to do is find a method to flag the columns that do not match up. As an idea, I have thought to append a character to the end of the data in the fields that do not match.
For example if the updateflag is different in one table to the other, I would be returned '* f' instead of 'f'
SELECT id, number, "updateflag" from dbc.person
EXCEPT
SELECT id, number, "updateflag":bool from dbg.person;
Should I be joining the two tables together, post executing this statement to identify the differences, from whats returned?
I have tried to research methods to implement this but have no found anything on the topic
I prefer a full outer join for this
select *
from dbc.person p1
full join dbg.person p2 on p1.id = p2.id
where p1 is distinct from p2;
The id column is assumed the primary key column that "links" the two tables together.
This will only return rows where at least one column is different.
If you want to see the differences, you could use a hstore feature
select hstore(p1) - hstore(p2) as columns_diff_p1,
hstore(p2) - hstore(p1) as columns_diff_p2
from dbc.person p1
full join dbg.person p2 on p1.id = p2.id
where p1 is distinct from p2;
I'm not sure how to do this without using a JOIN (which ODB doesn't have, of course). In "generic" SQL, you might do something like this:
Select * FROM table
INNER JOIN
(SELECT max(field) AS max_of_field, key FROM table GROUP BY key) sub
ON table.field = sub.max_of_field AND table.key = sub.key
Is there an efficient way to do this in ODB, using SELECT and/or MATCH?
I've searched through StackOverflow and Google for a while and didn't find anything too similar, so here's my problem:
I'm currently writing a stored procedure to check that every every column in a database named 'Sequence' has an associated constraint ensuring the value is >=1. However, my current method returns all objects containing 'Sequence', not just tables (ie. get/set/delete stored procedures that contain 'Sequence').
Here is my current code, which works, but I feel is a dirty solution:
SELECT DISTINCT
'The Sequence column of the ' + CAST(OBJECT_NAME([AC].[object_id]) AS NVARCHAR(255)) + ' table is missing a Sequence>=1 constraint.' AS MESSAGE
FROM [sys].[all_columns] AC
LEFT JOIN [sys].[check_constraints] CC
ON [CC].[parent_object_id] = [AC].[object_id]
AND [CC].[name] LIKE '%Sequence'
AND [CC].[definition] LIKE '%Sequence]>=(1))'
WHERE [AC].[name] = 'Sequence'
AND [CC].[name] IS NULL
AND OBJECT_NAME([AC].[object_id]) NOT LIKE '%Get%'
AND OBJECT_NAME([AC].[object_id]) NOT LIKE '%Set%'
AND OBJECT_NAME([AC].[object_id]) NOT LIKE '%Delete%'
Specifically, my question is: Given [sys].[all_columns].[object_id], is there an easy way to check if the given object is a table versus a stored procedure?
Any help or advice on this would be greatly appreciated! Also general code cleanup here, I'm relatively new to tSQL so this is probably not the most efficient way to go about it.
Thanks,
Andrew
You may refer tables using sys.tables view and search within constraints associated to Sequence column solely:
select quotename(schema_name(t.schema_id)) + '.' + quotename(t.name)
from sys.tables t
join sys.columns c on c.object_id = t.object_id
left join sys.check_constraints cs on cs.parent_object_id = t.object_id and cs.parent_column_id = c.column_id
and cs.definition like '%Sequence]>=(1))'
where c.name = 'Sequence' and cs.object_id is NULL
This should give you tables having Sequence column, but having no constraint on it or having constraint which is not defined according to the rule specified.
I want to
SELECT * FROM table1 WHERE name=petter
Now if there is many type of petter in table like PETTER , Petter And petter.
Want all this three (PETTER, Petter, petter) to be considered which command is for this in cognos report studio?
Or in DB2 without using 'IN' function.
I think you want UPPER (or LOWER, the effect should be the same):
SELECT *
FROM table1
WHERE UPPER(name) = 'PETTER'
Remember, though, if you have an index on name, then this won't be able to use that index. You can create (at least if you're on z/OS) an index with that function. On other platforms, you can create a generated column and create index on that.
I have the need to look at two tables that share two variables and get a list of the data from one table that does not have matching data in the other table. Example:
Table A
xName
Date
Place
xAmount
Table B
yName
Date
Place
yAmount
I need to be able to write a query that will check Table A and find entries that have no corresponding entry in Table B. If it was a one variable issue I could use not in statement but I can't think of a way to do that with two variables. A left join also does not appear like you could do it. Since looking at it by a specific date or place name would not work since we are talking about thousands of dates and hundreds of place names.
Thanks in advance to anyone who can help out.
SELECT TableA.Date,
TableA.Place,
TableA.xName,
TableA.xAmount,
TableB.yName,
TableB.yAmount
FROM TableA
LEFT OUTER JOIN TableB
ON TableA.Date = TableB.Date
AND TableA.Place = TableB.Place
WHERE TableB.yName IS NULL
OR TableB.yAmount IS NULL
SELECT * FROM A WHERE NOT EXISTS
(SELECT 1 FROM B
WHERE A.xName = B.yName AND A.Date = B.Date AND A.Place = B.Place AND A.xAmount = B.yAmount)
in ORACLE:
select xName , xAmount from tableA
MINUS
select yName , yAmount from tableB