How can I transfer data from one table to another, overwriting old data? - sql-server

I need some help for transfering data from one table to another.
As you can see there are 2 databases.
I would like to transfer the table datas "PinterSet" located in the database Contrinex.GPO in the table "PrinterSet" located in the database Contrinex.GPOQA.
There are already datas in the table "PrinterSet" of Contrinex.GPOQA but I would overwrite and put the datas from "PrinterSet" of Contrinex.GPO.
So how can I do that ?

here is your code..
truncate table Contrinex.GPOQA.dbo.PrinterSet
go
insert into Contrinex.GPOQA.dbo.PrinterSet
select * from Contrinex.GPO.dbo.PrinterSet

TRUNCATE TABLE [Contrinex.GPOQA].dbo.PinterSet
GO
INSERT INTO [Contrinex.GPOQA].dbo.PinterSet (...)
SELECT ...
FROM [Contrinex.GPO].dbo.PinterSet

select data from first database table and insert it into the second database table as
INSERT INTO GPOQA.PrinterSet SELECT * from GPO.PrinterSet
if want some perticular columns then set column names as
INSERT INTO GPOQA.PrinterSet a SET a.column1=b.column1,.... SELECT column1,... from GPO.PrinterSet b

You can use Sql Server Export functianality, where you can transfer data from one table to another across Database.
Please refer the below link on using the SQL Server Export
http://searchsqlserver.techtarget.com/feature/The-SQL-Server-Import-and-Export-Wizard-how-to-guide

Related

Temporary tables in hana

it it possible to write script in hana that crate temporary table that is based
on existing table (with no need to define columns and columns types hard coded ):
create local temporary table #mytemp (id integer, name varchar(20));
create temporary table with the same columns definitions and contain the
same data ? if so ..i ill be glad to get some examples
i am searching the internet for 2 days and i couldn't find anything useful
thanks
Creating local temporary tables based on dynamic structure definition is not supported in SQLScript.
The question would be: for what do you want to use it?
Instead of a local temp. table you can use a table variable in most cases.
By querying sys.table_columns view, you can get the list and properties of source table and build a dynamic CREATE script then Execute to create the table.
You can find SQL codes for a sample case at Create Table Dynamically on HANA Database
For table columns read
select * from sys.table_columns where table_name = 'TABLENAME';
Seems to work in the hana version I have. I'm not sure how to find out what the version.
PROCEDURE "xxx.yyy.zzz::MY_TEST"(
OUT "OUT_COL" NVARCHAR(200)
)
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
AS
BEGIN
create LOCAL TEMPORARY TABLE #LOCALTEMPTABLE
as
(
SELECT distinct 'Cola' as out_col
FROM "SYNONYMS1"
);
select * from #LOCALTEMPTABLE ;
DROP TABLE #LOCALTEMPTABLE;
END
The newer HANA version (HANA 2 SPS 04 Patch 5 ( Build 4.4.17 )) supports your request:
create local temporary table #tempTableName' like "tableTypeName";
This should inherit the data types and all exact values from whatever query is in the parenthesis:
CREATE LOCAL COLUMN TEMPORARY TABLE #mytemp AS (
SELECT
"COLUMN1",
"COLUMN2",
"COLUMN3"
FROM MyTable
);
-- Now you can add the rest of your query here as such:
SELECT * FROM #mytemp
I suppose you can just write :
create column table #MyTempTable as ( select * from MySourceTable);
BR,

Enforce link tables

I have two tables, one called Season the other one Episode. Between them is a Link table that stores SeasonID and EpisodeID. How do I make sure that when a new episode is added the Link table will be updated as well?
Assuming that you are using SQL Service.
We can achieve with the help of trigger like this
Query
CREATE TRIGGER trig_Update_Episode
ON [Episode]
FOR INSERT
AS
Begin
IF NOT EXISTS (SELECT 1
FROM [dbo].[tblEpisodeSession] WITH (NOLOCK)
WHERE [EpisodeId] = [inserted.ID])
PRINT N'You must update an entry in tblSessionEpisode As well';
End
for both the table you should create a trigger like given above.
In example query you can replace message with your actual query which should actually create an entry in tblEpisodeSession.
Hope this helps.

Merge query using two tables in SQL server 2012

I am very new to SQL and SQL server, would appreciate any help with the following problem.
I am trying to update a share price table with new prices.
The table has three columns: share code, date, price.
The share code + date = PK
As you can imagine, if you have thousands of share codes and 10 years' data for each, the table can get very big. So I have created a separate table called a share ID table, and use a share ID instead in the first table (I was reliably informed this would speed up the query, as searching by integer is faster than string).
So, to summarise, I have two tables as follows:
Table 1 = Share_code_ID (int), Date, Price
Table 2 = Share_code_ID (int), Share_name (string)
So let's say I want to update the table/s with today's price for share ZZZ. I need to:
Look for the Share_code_ID corresponding to 'ZZZ' in table 2
If it is found, update table 1 with the new price for that date, using the Share_code_ID I just found
If the Share_code_ID is not found, update both tables
Let's ignore for now how the Share_code_ID is generated for a new code, I'll worry about that later.
I'm trying to use a merge query loosely based on the following structure, but have no idea what I am doing:
MERGE INTO [Table 1]
USING (VALUES (1,23-May-2013,1000)) AS SOURCE (Share_code_ID,Date,Price)
{ SEEMS LIKE THERE SHOULD BE AN INNER JOIN HERE OR SOMETHING }
ON Table 2 = 'ZZZ'
WHEN MATCHED THEN UPDATE SET Table 1.Price = 1000
WHEN NOT MATCHED THEN INSERT { TO BOTH TABLES }
Any help would be appreciated.
http://msdn.microsoft.com/library/bb510625(v=sql.100).aspx
You use Table1 for target table and Table2 for source table
You want to do action, when given ID is not found in Table2 - in the source table
In the documentation, that you had read already, that corresponds to the clause
WHEN NOT MATCHED BY SOURCE ... THEN <merge_matched>
and the latter corresponds to
<merge_matched>::=
{ UPDATE SET <set_clause> | DELETE }
Ergo, you cannot insert into source-table there.
You could use triggers for auto-insertion, when you insert something in Table1, but that will not be able to insert proper Shared_Name - trigger just won't know it.
So you have two options i guess.
1) make T-SQL code block - look for Stored Procedures. I think there also is a construct to execute anonymous code block in MS SQ, like EXECUTE BLOCK command in Firebird SQL Server, but i don't know it for sure.
2) create updatable SQL VIEW, joining Table1 and Table2 to show last most current date, so that when you insert a row in this view the view's on-insert trigger would actually insert rows to both tables. And when you would update the data in the view, the on-update trigger would modify the data.

how to export records/data from one database table to another database table?

How do you export records from one database table and import it into another database table?
(same table structure).
If the table have the exact same structure, and no autogenerated fields you can use:
insert into DestinationTable
select * from SourceTable
You can also use the
select *
into DestinationTable
from SourceTable
syntax, to create and fill the destination table on the fly.
If you also want to keep you identity colums same you can easily do it using code smith template. Download the templates from here and use ScriptTableData.cst template in them. Before that you will required to install code smith on you machine too.

SQL Server: importing from Excel, only want the new entries

The task is to have SQL Server read an Excel spreadsheet, and import only the new entries into a table. The entity is called Provider.
Consider an Excel spreadsheet like this:
Its target table is like this:
The task is to:
using 2008 Express toolset
import into an existing table in SQL Sever 2000
existing data in the table! Identity with increment is PK. This is used as FK in another table, with references made.
import only the new rows from the spreadsheet!
ignore rows who don't exist in spreadsheet
Question:
How can I use the SQL 2008 toolset (Import and Export wizard likely) to achieve this goal? I suspect I'll need to "Write a query to specify the data to transfer".
Problem being is that I cannot find the query as the tool would be generating to make fine adjustments.
What I'd probably do is bulk load the excel data into a separate staging table within the database and then run an INSERT on the main table to copy over the records that don't exist.
e.g.
INSERT MyRealTable (ID, FirstName, LastName,.....)
SELECT ID, FirstName, LastName,.....
FROM StagingTable s
LEFT JOIN MyRealTable r ON s.ID = r.ID
WHERE r.ID IS NULL
Then drop the staging table when you're done.
You can run some updates on that stage table before you load it, to clean it up, if you need to, as well. UPDATE SET NAME = RTROM(LTRIM(Name))
FROM YOUR.STAGE.TABlE for example

Resources