snowflake move table from one schema to another - snowflake-cloud-data-platform

I have two databases:
DB1:
SCHEMA1:
-- TABLE1
-- TABLE2
DB2:
SCHEMA2:
I want to move TABLE1 to DB2::SCHEMA2
i.e
DB1:
SCHEMA1:
-- TABLE2
DB2:
SCHEMA2:
-- TABLE1
also note in future i may delete DB1

I have to correct myself because Snowflake starts supporting renaming a table to another database/schema.
https://docs.snowflake.com/en/sql-reference/sql/alter-table.html#parameters

I could do this
alter table if exists "DB1"."SCHEMA1"."TABLE1" RENAME TO "DB2"."SCHEMA2"."TABLE1";

Related

snowflake: after cloning if i delete the oringal table what will happen

In snowflake i have the following structure
DB1
-- schema1
-- schema2
-- table2
i want to create a copy of table2 in schema1
so i am thinking to do
CREATE or replace TABLE "SCHEMA1"."TABLE2_CLONE_FROM_SCHEMA1" CLONE "SCHEMA2"."TABLE2";
now i will see
DB1
-- schema1
-- table2_clone_from_schema1
-- schema2
-- table2
now if i delete table2 in schem2 will i loose table2_clone_from_schema1
Nothing will happen. Your new table stays there. When a table is deleted, Snowflake keeps the micro-partitions of this table if they are used by any cloned tables.
https://docs.snowflake.com/en/user-guide/tables-storage-considerations.html#cloning-tables-schemas-and-databases

In SQL Server / Liquibase, does INSERT query create a table, automatically without CREATE schema, if it does not exist?

I am using Liquibase for managing SQL Server scripts (create, update, delete, alters etc.).
My requirement was to create a backup table (say old_table_a) before I could drop two columns (column_1, column_2) from the original table (table_a).
The new backup table does not need a primary key, so it will just have two columns as shown below
old_table_a
column_1 (from original table_a)
column_2 (from original table_a)
If I just write INSERT query as shown below, without having a CREATE TABLE old_table_a
INSERT INTO old_table_a (column_1, column_2)
SELECT column_1, column_2
FROM table_a
I had read this somewhere on some blog, but cannot find this.
Please provide some information if this is possible.
Otherwise I know that the usual way to do this is to create the new backup table and then populate the new table with values from the original.
This can be done with SELECT * INTO:
SELECT * INTO [NEWTABLE] FROM [OLDTABLE]
INSERT tableName1 (ColumName)
(select (ColumName ) from TableName2)

Clone Sql table automatically from sql

I have two exactly same tables. My question: is there any way when i'm inserting something in first table automatically sql server to copy this row into another table.
I know that i can do it manually
select * into table1 from table2 where table2ID=#table2ID
But i'm wondering if i can create a table dynamically and set it when a row is inserted, copy row's data into another table also.
So with this way i dont need to run extra code to do that, sql will do this automatically
You can use a TRIGGER which is an object that you link to a table with a particular operation (INSERT, UPDATE, DELETE or any combination of those). The trigger's code will execute on each operation done to the linked table.
Basic example:
CREATE TRIGGER dbo.CopyToTable2 ON Table1
AFTER INSERT -- The trigger will execute after any insert done to Table1
AS
BEGIN
SET NOCOUNT ON
INSERT INTO Table2 (
Column1,
-- OtherColumns
)
SELECT
I.Column1
-- OtherColumns
FROM
inserted AS I -- "inserted" is a special table that references the trigger's tracking table for new or updated records
END
The use of a trigger is appropriate here. It might look something like this:
CREATE TRIGGER SomeTriggerName ON theSourceTable
FOR INSERT
AS
INSERT INTO DestinationTable
(column1, someothercolumn)
SELECT (column1, someothercolumn)
FROM inserted

How to create a table in sql server like other table?

I want to create a table like another table in SQL Server.
In Postgres I used to create it with this request:
CREATE TABLE IF NOT EXISTS table2 (LIKE table1 INCLUDING ALL) INHERITS (table1)
Something like this should work:
IF OBJECT_ID('table1', 'U') IS NULL BEGIN
SELECT TOP 0 * INTO table1 FROM table2
END
This will clone the column definition of table2 in table1 without inserting any data (because of the top 0).
Then you have to create the PK, UK, FK for the table1. For this purpose you can check: https://www.mssqltips.com/sqlservertip/3443/script-all-primary-keys-unique-constraints-and-foreign-keys-in-a-sql-server-database-using-tsql/
With this resource you can generate an ALTER TABLE script to add all PK, UK, FK in a variable and run it like:
DECLARE #sqlCommand nvarchar(1000)
EXECUTE sp_executesql #sqlCommand
SELECT *
INTO table2
FROM table1 where 1=2
SELECT * = select all columns
INTO= place of set
FROM = place to lookup,
WHERE 1=2 = filter criteria
select top 0 * into x from y
will give you an empty shell of a table with no data exactly defined as the original table minus all the indexes , constraints etc

SQL Server 2008: How to Copy Data from Table in DB1 to Table in DB2?

I have two identical databases on the same server. During a deployment process, I have data in tables in database A that need copied over to the tables in database B. What is the easiest way to programmatically accomplish this task?
EDIT:
Tables do have identity columns.
There are tables with foreign key constraints, so insert order is important.
All of the rows will need to be copied. As far as I'm aware, this will always be the case.
Assuming that the tables don't have identity columns and belongs to the default (dbo) schema, try the TSQL insert query below;
Insert Into DatabaseB.dbo.DestinationTable
Select * From DatabaseA.dbo.SourceTable
If you have an identity column then execute statements below
SET IDENTITY_INSERT DatabaseB.dbo.DestinationTable ON
GO
Insert Into DatabaseB.dbo.DestinationTable
Select * From DatabaseA.dbo.SourceTable
GO
SET IDENTITY_INSERT DatabaseB.dbo.DestinationTable OFF
GO
If the databases are in different servers:
exec sp_addlinkedserver ServerA
Insert Into DatabaseB.dbo.DestinationTable
Select * From ServerA.DatabaseA.dbo.SourceTable

Resources