Inheritance Problem in SQL Server 2005 - sql-server-2005

I am having a problem whose solution I can't seem to figure out at the moment. Say I have a table
CREATE TABLE #Test
(
SomeValue1 INT,
SomeValue2 INT
)
Now, I have two other tables, say TableA and TableB, in which TableB inherits from TableA by sharing its primary key. I want to insert SomeValue1 into TableA and SomeValue2 into TableB, but in a way such that when you join TableA and TableB by their primary key you get #Test, but I can't figure out how to do it. Can anyone please help me?

SELECT SomeValue1, SomeValue2 FROM TableA A JOIN TableB B ON A.PrimaryKey=B.PrimaryKey
That sounds like what you're asking for, from what I can tell. Maybe you're also saying you want to create a View on the two tables of existing data?

If TableB inherits I assume TableB has the foreign key to an IDENTITY column in TableA
INSERT TableA (SomeValue) VALUES (SomeValue1)
INSERT TableB (SomeValue) VALUES (SCOPE_IDENTITY(), SomeValue2)
Otherwise, you can adapt the OUTPUT clause from your other recent questions: Output to Temporary Table in SQL Server 2005
Note:
How does this relate to Iterating Through Table in High-Performance Code? What is your actual end to end requirement... you are asking very specific questions that probably don't really help

Related

Most efficient way to query SQL Server large table almost complete result set

I have a table in SQL Server 2014 with a large number of rows. Let's call it TableA.
I need to query its PK (an AUTOINCREMENT ID, CLUSTERED KEY) for almost all the rows (let's say, 97% of the rows) and this result set is usually in join with another table (TableB) via foreign key (let's call it FK_A).
The query looks like:
SELECT
TableB.someColumnNotFKNorPK
FROM
TableB
INNER JOIN
TableA ON TableB.FK_A = TableA.ID
WHERE
TableA.LowSparseColumn = 100
The problem is that TableA has 97% of the rows with LowSparseColumn = 100, therefore this yields to row spools etc. because SQL Server needs to stash the partial result
Do you know how to deal with such issue?
Any help is really appreciated!
Thanks!
If you have an index on TableB(fk_A) (or better yet (TableB(fk_A, someColumnNotFKNorPK) and your table statistics are up-to-date, then the optimizer should do its job. It should read TableA and do a join to TableB without spooling.
You could rewrite the query as:
SELECT TableB.someColumnNotFKNorPK
FROM TableB
WHERE EXISTS (SELECT 1
FROM TableA
WHERE TableB.FK_A = TableA.ID AND
TableA.LowSparseColumn = 100
);
This should make optimal use of an index on TableA(ID, LowSparseColumn) (although that index is not necessary if ID is a primary key).

What does it mean to INNER JOIN before an INSERT?

I have the following case where I'm doing an insert into a table, however, before I can do that, I to grab a foreign key ID that's associated with another table. That foreign key ID is not a simply look up, but rather requires an INNER JOIN of two other tables to be able to get that ID.
So, what I'm currently doing is the following:
Inner joining A, B and grabbing the ID that I need.
Once I resolve the value from above, I insert into table C with
the foreign key that I got from step 1.
Now, I was wondering if there is a better way for doing this. Could I do the join of table A and B and insert into table C all in one statement? This is where I was getting confused on what it means to INNER JOIN across tables and then INSERT. Are you potentially inserting into multiple tables?
You can use the insert-select syntax to insert the results of a query (which may or may not involve a join) to another table. E.g.:
INSERT INTO C
SELECT col_from_a, col_from_b
FROM a
JOIN b ON a.id = b.id

Postgres: Update all columns from another table

I need to update a table from another one, and I need to update all columns, My questions is that, besides list every column in the SET, is there a way to update them all like this:
update tableA
set * = tableB.*
from tableB where tableA.id = tableB.id
I tried in psql, it doesn't work.. I have to list every column like this:
update tableA
set c1 = tableB.c1, c2 = tableB.c2, ...
from tableB where tableA.id = tableB.id
tableB is created use create..like tableA. So they are basically identical. And the reason I'm doing it is that I need load .csv data to temp table tableB and then update tableA based on the new data in tableB. tableA needs to be locked as little as possible and tableA needs to keep integrity. I'm not sure 'delete then insert' would be a good option?
Thanks!
You could delete and re-insert, if the two tables have the same columns in the same order. Assuming that all records in tableB match tableA:
delete from tableA
where id in (select id from tableB)
insert into tableA
select *
from tableB;
(If all records do not match, you could use a temporary table to keep the necessary ids.)
Generally, I oppose doing insert without a column list. In some cases, it is tolerable -- such as when tableB was created as a subset from tableB using *.
Depends on your use case and will cause locks but of a different type. Approach that works well for some scenarios.
BEGIN;
DROP TABLE IF EXISTS tableA_old;
ALTER tableA RENAME TO tableA_old;
ALTER tableB RENAME TO tableA;
COMMIT;

SQL - Insert Into Select query with lookup of value from another table

Morning all, I need some help please.
I have a database with several tables. I'm trying to write an INSERT INTO and SELECT query that copies all values from one Table (TableA) into another Table (TableD) but substitutes one value with a value it looks up in another table (Table B).
Table A with various fields including TableBRef
Table B includes various fields starting with TableBRef and also includes a field TableCRef
I want to copy all of TableA into Table D but replace The TableBRef with the TableCRef ie I know TableBRef, I need to search for it in TableB and return the associated value from the TableCRef field.
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT TableA.ARef, TableB.CRef, TableA.AData1, TableA.AData2
FROM TableD AS TableD_1 CROSS JOIN
TableC CROSS JOIN
TableA INNER JOIN
TableB ON TableA.BRef = TableB.BRef
Sorry, I thought that calling them generic table names may help but it's actually a little confusing :-)
I don't understand how you relate each table but see if this query will work for you
INSERT INTO TableD
(DRef, CRef, DData1, DData2)
SELECT a.ARef, c.CRef, a.AData1, a.AData2
from tableA a
left outer join tableB b on a.ARef = b.ARef
inner join tableC c on b.CRef = c.CRef

SQL: Performance comparison for exclusion (Join vs Not in)

I am curious on the most efficient way to query exclusion on sql. E.g. There are 2 tables (tableA and tableB) which can be joined on 1 column (col1). I want to display the data of tableA for all the rows which col1 does not exist in tableB.
(So, in other words, tableB contains a subset of col1 of tableA. And I want to display tableA without the data that exists in tableB)
Let's say tableB has 100 rows while tableA is gigantic (more than 1M rows). I know 'Not in (not exists)' can be used but perhaps there are more efficient ways (less comp. time) to do it.? I don't maybe with outer joins?
Code snippets and comments are much appreciated.
Depends on the RDBMS. For Microsoft SQL Server NOT EXISTS is preferred to the OUTER JOIN as it can use the more efficient Anti-Semi join.
For Oracle Minus is apparently preferred to NOT EXISTS (where suitable)
You would need to look at the execution plans and decide.
I prefer to use
Select a.Col1
From TableA a
Left Join TableB b on a.Col1 = b.Col1
Where b.Col1 Is Null
I believe this will be quicker as you are utilising the FK constraint (providing you have
them of course)
Sample data:
create table #a
(
Col1 int
)
Create table #b
(
col1 int
)
insert into #a
Values (1)
insert into #a
Values (2)
insert into #a
Values (3)
insert into #a
Values (4)
insert into #b
Values (1)
insert into #b
Values (2)
Select a.Col1
From #a a
Left Join #b b on a.col1 = b.Col1
Where b.Col1 is null
The questions has been asked several times. The often fastest way is to do this:
SELECT * FROM table1
WHERE id in (SELECT id FROM table1 EXCEPT SELECT id FROM table2)
As the whole joining can be done on indexes, where using NOT IN it generally cannot.
There is no correct answer to this question. Every RDBMS has query optimizer that will determine best execution plan based on available indices, table statistics (number of rows, index selectivity), join condition, query condition, ...
When you have relatively simple query like in your question, there is often several ways you can get results in SQL. Every self respecting RDBMS will recognize your intention and will create same execution plan, no matter which syntax you use (subqueries with IN or EXISTS operator, query with JOIN, ...)
So, best solution here is to write simplest query that works and then check execution plan.
If that solution is not acceptable then you should try to find better query.