I have 2 tables, let's say Table1 and Table2. Both the tables have more than 50 columns. I want to write a query which returns all the columns. After that I will create one model class and map the columns returned from query result which has around 60 columns from both the tables.
Below are the sample columns for 2 tables. I have not made any mappings in entities in java classes.
Table1
id
search_key
col3 and so on.. 50+ cols
Table2
id
t1_id
col3 and so on.. 50+ cols
I have written one native query as below:
#Query(value="SELECT * FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id = t2.t1_id WHERE t1.search_key IN ('12345')", nativeQuery=true)
But this native sql query is throwing NonUniqueDiscoveredSqlAliasException: Encountered a duplicated sql alias [id] during auto-discovery of a native sql query.
My Requirement:
I need to write a query either native or hql which returns all the columns from both the tables based on matching criteria. and then that returned object by query I will map the columns which I want (since I want around 60 columns from both the tables, I don't want to write each column in select statement)
Is there a way to achieve this???
Please let me know if you need more info in question.
Actually if you run your query on a SQL client you can see that it returns all columns (from both tables) so we can understand that the problem is that your query is Associated with an Entity or generally a Java class that you have created. If you run your query and save the result on a List<Object> and not on a List<YourCustomObject> in your code then you will see that every Object of your list would contain all rows from both tables. Your SQL query as a plain sql query returns all rows from both tables as it is.
Related
Imagine there is a SQL statement with 300+ columns
create table if not exists (
300+ columns
);
Insert into
select
300 columns
from
a inner join b
on a.key=b.key
;
It just keeps showing the error message
Invalid operation: INSERT has more expressions than target columns;
It is really hard to find which column is miss matching since there are too many columns.
Is there any way I can count the number of columns in a SELECT statement?
I know we can count the number of columns in information schema, but I want to count the number of columns/ items in a select statement, not an existing SQL table.
Well, you can use information_schema tables. For instance, you could use:
create table tempt as
select 300 columns
from a inner join
b
on a.key = b.key;
(I would add something like limit 1 because you may not care about the data.)
Then you can look in information_schema.columns to get the columns lists in order, with their types. You can even compare the columns to the original table, using SQL statements.
I have two identical tables in my database, T1 and T2. T1 is a local table while T2 is a linked table from a live SQL database. At the moment the two tables are identical.
In short, I would like to be able to run a query that will update T1 will all new records that have been added to T2. So once I have run the query, the two tables should be identical again. Is this at all possible? I need to have the data in T1 locally available as I need to be able to query that table even when the data in T2 is not available. The SQL database in question is off site so I will not always be able to run my queries as the link is unreliable.
Any assistance will be greatly appreciated.
If you do have a unique ID for every record and are sure records already entered will never be changed this can actually be quite simple:
INSERT INTO [TBL_INVOICES_LOCAL]
SELECT TBL_INVOICES.*
FROM [TBL_INVOICES_LOCAL] RIGHT JOIN TBL_INVOICES ON [TBL_INVOICES_LOCAL].InvoiceID = TBL_INVOICES.InvoiceID
WHERE ((([TBL_INVOICES_LOCAL].InvoiceID) Is Null));
All you need to do is join the two tables with the relationship being set to:
Include ALL records from LINKED_TABLE and only records from LOCAL_TABLE where the joined fields are equal.
Setting the criteria of the Local Tables ID field to "Is Null" will only show missing records. If you do this in an append query you can update your table in only one query as seen below:
I am working with SQL Server 2008 and doing data analysis by using different queries. In my database I have 70 columns each in two different tables in same schema. The data in those tables were entered twice. Now I am comparing data of each column and showing records which have differences. Below is my query.
SELECT
[NEEF_Entry].[dbo].[tbl_TOF].Student_Class4_15,
[NEEF_Entry].[dbo].[tbl_TOF_old].Student_Class4_15
FROM
[NEEF_Entry].[dbo].[tbl_TOF]
INNER JOIN
[NEEF_Entry].[dbo].[tbl_TOF_old] ON [NEEF_Entry].[dbo].[tbl_TOF].FormID = [NEEF_Entry].[dbo].[tbl_TOF_old].FormID
WHERE
[NEEF_Entry].[dbo].[tbl_TOF].Student_Class4_15 <> [NEEF_Entry].[dbo].[tbl_TOF_old].Student_Class4_15
The join is based in the form ID which is same in both the tables. Now the column here is Student_Class4_15 in table tbl_TOF and in table tbl_TOF_old which is being compared here and the output is here
It shows what is the difference when data was entered before and after. Now the problem with this is that I have to manually replace column names of 70 columns each time which is time consuming.
What I want is that SQL query should pick all columns and compare them and return results.
I would use except to compare two tables, If the query returns no rows then the data is the same.
SELECT *
FROM table1
EXCEPT
SELECT *
FROM table2;
In case table2 has an extra rows:
SELECT *
FROM table2
EXCEPT
SELECT *
FROM table1;
Using Microsoft SQL server 2017, I want to merge two tables into a 3rd table.
Is there a way to have it defined automatically, without a query?
If it is not possible, I would like to mention that both tables contain a large amount of columns, therefore I look for an efficient query that will not make me write each column name.
Demonstration:
Original tables are Table1,Table2, column structure is as below:
Table1:
(Column1,Column2,Column3)
Table2:
(Column4,Column5,Column6)
My goal is to create Table3. Table3 is based on Table1 LEFT JOIN Table2 ON Column1=Column4.
Table 3:
(Column1,Column2,Column3,Column4,Column5,Column6)
Create it as a view.
CREATE VIEW table3 AS
SELECT Column1,Column2,Column3,Column4,Column5,Column6
FROM Table1 LEFT JOIN Table2 ON Column1=Column4
You can then reference table3 in other queries exactly like an ordinary table.
I'm looking for a way to drop single columns from an extended query in DBVisualizer. I have a couple of tables with more than one hundred columns each. Using a number of subqueries I want to drop a single column from one of the tables, something like that:
select *
from table1 a
join table2 b
on a.key = b.key
drop b.key;
I defenitely do not want to write down all the single columns, because the query is supposed to be used for a long time and the tables get new columns to often to change the query each time.
Is there a way to do this?
Thanks
Felix