Need SQL Query to compare the same table name should have the view created in a different database - sql-server

We have a central database, and there were other databases with have year naming convention. We move the tables periodically to another database. After moving the table, we create a view with the same name in the source database.
For Example
Have Database name: ABC (which has the table’s daily load)
Have another database name: ABC2023Q1 (The tables we move from ABC to this database, and we create a view in ABC table with the same table name)
Is it the right approach to check with information_schema.tables and information_schema. Views with left join? Please let me know if you have any other approach.

Related

view created table in microsoft sql server

I am running part of a query in Microsoft SQL server management studio
Select Table1.Column1
into #Table2
from Table1
now it has created the table but I actually want to view this table with my eyes but I cannot seem to find where the table is stored. Please could someone help me find it?
That is a temporary table. It will be created in the tempdb system database and you can see it by going to tempdb -> Temporary Tables.
Any tables where its name start with # is a Temporary Table. Exactly as the name suggests, it's temporary, and only exists for the same time the connection that created it does (or it is dropped).
If you want to view the data from a temporary table, you would do so like any other table SELECT * FROM #Table2;. .
I imagine what your really after is to not use a temporary table, so drop the # from the name, and the new table will be created in the database you are connected to.

How to update multiple tables from excel file in SQL server

Hi I am new to SQL server.
I need help to update two tables from excel file.
Excel file includes all the data in 1 sheet and that data need to update on SQL server tables.
example data on the Excel file
Employeename, Deparment
SQL tables are
employer details and departments
I know how to add to the table but I want to know for the multiple tables
Use the following steps,
Create a temporary table with the same structure as excel.
Import data from excel to temp. table using SSIS
package/Export/Import Wizard/ Just Copy & Paste
Insert into department table by selecting distinct departments from
temp. table.
Insert into employee table by selecting employee details, department
pk can be selected from department table by department name.
Remove temporary table.
Create and use a SSIS package to do your task. You can alternatively use Sql Server Export/Import Wizard .

Moving schema to another database

I have to move an schema from one database to a new database to keep a centralized schema into the same server. The problem is that I already have many stored procedures that use some of these tables from the schema that I need to move.
Is there any workaround to do this and change all the objects that use this tables to be able to pointed to the new database? Can I use synonyms or link server?
I'm working on SQL Server 2008 R2
Thank you.
yep; synonyms are the way to go (stole this example from http://msdn.microsoft.com/en-us/library/ms177544.aspx):
USE tempdb;
GO
-- Create a synonym for the Product table in AdventureWorks2012.
CREATE SYNONYM MyProduct
FOR AdventureWorks2012.Production.Product;
GO
-- Query the Product table by using the synonym.
USE tempdb;
GO
SELECT ProductID, Name
FROM MyProduct
WHERE ProductID < 5;
GO
You could pretty easily generate synonym statements by searching the sys.tables view and identifying the tables which belong to the schema you want to move.

SQL Server - Create Copy of Multiple Tables in Same Database

I need to duplicate several tables in a single database. Basically we have an old dot net nuke portal that needs to be split and our host limits us to a single database. So we just want to take the existing tables but copy them with an exact duplicate but different table prefix name (e.g. EEPTL_Users to JBPTL_Users).
I can use 'script to - create' to make the proper table structure but how do I do the proper inserts when I need to specify the primary keys but want the column to be an auto-incrementing identiy?
Is there a tool for doing what I need?
SET IDENTITY_INSERT [table name] ON
INSERTS
SET IDENTITY_INSERT [table name] OFf

What is the equivalent of 'CREATE TABLE ... LIKE ..." in SQL Server

I am working with SQL Server (I am a SQL Server noob) and trying to alter a table. I want to CREATE TABLE LIKE to safely store data while I drop keys and constraints and all the other rigamorole that SQL Server seems to require when altering on the original table but I have not been able to find a match to that command...
you want to recreate the same structure?
how about this
SELECT *
into test
FROM myRealTable
where 0=1
no data will be inserted into the new table
You can do
SELECT * INTO #MyTable_tmp FROM MyTable
Then modify your MyTable, and copy your data back in. Other approaches I've seen is to create a new table call it Mytable_Tmp (Not a temp table), which will be your new table.
Then copy your data doing any migrations you need. Then you will drop the original table and do a rename on Mytable.
Or you can get one of the many excellant tools that compare databases and generate difference scripts or VSTS DB Edition (Comes with developer) and you can do a diff script from a project file to a DB.
Edit
When you run SELECT * INTO #MyTable FROM MyTable, SQL Server creates a new temporary table called #MyTable that matches each column and data type from your select clause. In this case we are selecting * so it will match MyTable. This only creates the columns it doesn't copy defaults, constraints indexes or anything else.

Resources