Query to Access Sharded Tables - sql

I am querying a vendors database that has data that is sharded between multiple tables. The tables are named Events_1, Events_2, Events_3, Events_4 etc.
It appears that a new table is automatically created when the table hits 10,000 records. I am looking to write a query that would be able union the results from all the tables (without having to manually add the tables as they are created) as well as a query to only look at the 2 newest tables.
Any suggestions on the best way to go about this?
The database is Microsoft SQL Server 2008

You can do this with dynamic SQL such that you query sysobjects for table name Events_* and build a SELECT UNION from the results.
Better, though perhaps more complicated, would be a DDL trigger that would dynamically update you UNION VIEW to append the new table whenever a table Events_* was created.

Related

Nothing from a table shows up in SQL

I have several table created by recently i can get anything to come from one of them.
The table is called REP and i'm currently using Live SQL from Oracle as i'm quite new to SQL
The select command i'm trying to use.:
select * from REP
I go to my schema and the table is definitely there. No commands for this table work.:
If anyone could help i'd very much appreciate it. Don't be too hard on me i'm quite new to SQL.
Your table is empty. First insert the values into this table via
INSERT INTO REP VALUES(...)
and
then use your query Select * from rep
As in your image, the table is there thats the ddl view meaning table's structure not data i.e. you have only columns but no data.

Is there a difference between selecting from a view or from a table?

I have some C# code which is trying to pull in 1000+ different entries through an SQL query (think, select col1 from table1 where id = x)
at each iteration of the loop x changes to present a new entry.
Is there a difference between running this query on a view or on a table?
or is there any other way to optimize this procedure?
A table contains data, a view is just a SELECT statement which has been saved in the database (more or less, depending on your database).
The advantage of a view is that it can join data from several tables thus creating a new view of it. Say you have a database with salaries and you need to do some complex statistical queries on it.
Instead of sending the complex query to the database all the time, you can save the query as a view and then SELECT * FROM view

Update 200 tables in database

I have two databases with a couple hundred tables in them each, in SQL Server. The tables in the two databases are 90% the same, with about 20 different tables in each. I'm working on a stored procedure to update database2 with the data from the tables it shares in database1.
I‌'m thinking truncate the tables and then insert the records from the tables in the other database like:
t‌runcate table database2.dbo.table2
s‌elect *
into data‌‌‌base2.dbo.table2
from database1.dbo.table1
I‌s this the best way to do this, and is there a better way to do it than writing a couple hundred of these statements?‌
This may give an error because the table already exists in Database(As per your truncate command). Given query will create a new table.
"s‌elect *
into data‌‌‌base2.dbo.table2 ---Create new table
from database1.dbo.table1"
If you want the same table structure and Data then you should generate scripts for Schema and data and run that scripts on another database(DB2)
right click the database and select tasks --> generate scripts
Next --> select the requested table/tables (select required tables)
next --> click advanced --> types of data to script = schema and data
also, change check for Existence--True
next and finish

Oracle SQL merge tables without specifying columns

I have a table people with less than 100,000 records and I have taken a backup of this table using the following:
create table people_backup as select * from people
I add some new records to my people table over time, but eventually I want to merge the records from my backup table into people. Unfortunately I cannot simply DROP my table as my new records will be lost!
So I want to update the records in my people table using the records from people_backup, based on their primary key id and I have found 2 ways to do this:
MERGE the tables together
use some sort of fancy correlated update
Great! However, both of these methods use SET and make me specify what columns I want to update. Unfortunately I am lazy and the structure of people may change over time and while my CTAS statement doesn't need to be updated, my update/merge script will need changes, which feels like unnecessary work for me.
Is there a way merge entire rows without having to specify columns? I see here that not specifying columns during an INSERT will direct SQL to insert values by order, can the same methodology be applied here, is this safe?
NB: The structure of the table will not change between backups
Given that your table is small, you could simply
DELETE FROM table t
WHERE EXISTS( SELECT 1
FROM backup b
WHERE t.key = b.key );
INSERT INTO table
SELECT *
FROM backup;
That is slow and not particularly elegant (particularly if most of the data from the backup hasn't changed) but assuming the columns in the two tables match, it does allow you to not list out the columns. Personally, I'd much prefer writing out the column names (presumably those don't change all that often) so that I could do an update.

How to merge Access DB table into a SQL Server table?

I have a SQL Server table that has a unique field which matches a unique field in an Access DB table.
I need to get the Access DB table into the SQL Server table based on that field.
How do I go about doing this?
I want to say a JOIN, but a JOIN is just a temporary query, isn't it? I want this to be permanent. I want the Access DB table data merged into SQL Server table based on this unique field.
What's the best way to do this?
Thanks.
The reason I need this separate is that a software is reading this SQL Server data, so I need it all in the same table.
B,
I believe what you would be looking for is Union
With the two tables being
HumanResources.Employees and HumanResources.Employees2
SELECT ID, Name FROM HumanResources.Employees
UNION
SELECT ID, Name FROM HumanResources.Employees2