How do I transfer some rows from one table to another in the easiest possible manner? This is a one time thing. I was hoping I could use VS2010 copy and paste function in Server Explorer, but it doesn't allow me to paste in rows.
As the table schema is the same then you can use copy and paste using Sql Server Management Studio rather than VS2010
You could also use a T-SQL statement using SSMS
Insert Into dbo.TableB (ColumnA, ColumnB, ColumnC ...)
Select ColumnA, ColumnB, ColumnC ...
From dbo.TableA
select * into <destination table> from <source table>
Example:
select * into employee_backup from employee
You can add WHERE clauses and specify columns as needed.
As per http://cavemansblog.wordpress.com/2009/06/09/sql-server-how-to-copy-a-table/
Related
In MySQL you can do SELECT CREATE TABLE .... and it will return the CREATE statement that was used to create this table. I need to do the same in SQL Server.
Is there any similar functionality in SQL Server? I have a table name test_table.
I need to run a SELECT statement that would return the CREATE TABLE string that was used to create this table. I tried this but it didn't work. How can I achieve this result in SQL Server?
You could try SELECT INTO and specify the name of a temporary table (prefixed with #). It also works with physical tables (although I've not found a use for this). Something like this
select '123' as colName into #newTable;
select * from #newTable;
colName
123
I'm trying to move a table in sql server to another table without the use of create. Is there any way to accomplish this.
I'm trying to use the select * from table
You can use SELECT . . INTO :
SELECT s.*
INTO <destination>
FROM <source> s;
This will create <DESTINATION> table with auto fill data.
Note : This will copy the table definition only includes (column/datatype).
Use simple insert query
insert into destination(columns)
select *
from source
where<condition>;
And delete existing record if you want
delete from source
where <condition>
commit;
The SELECT INTO statement creates a new table and inserts rows from the query into it.
Please refer this for more information.
SELECT
select_list
INTO
destination
FROM
source
[WHERE condition]
I have a SQL query that I have to run against multiple (15) tables in SQL Server Management Studio.
Is it possible to save the result of multiple queries into a file? (.txt, excel sheet?)
Using union is not possible because not all tables have an equal amount of columns.
The queries look somewhat like this
select *
from tableA
where main_id in (select id from maintable where date is null and status ='new')
select *
from tableB
where main_id in (select id from maintable where date is null and status ='new')
select *
from tableC
where main_id in (select id from maintable where date is null and status ='new')
select *
from tableD
where main_id in (select id from maintable where date is null and status ='new')
select *
from tableE
where main_id in (select id from maintable where date is null and status ='new')
Try below:-
Open SQL Server Management Studio
Go to Tools > Options > Query Results > SQL Server > Results To Text
Then on right hand side, change output format to comma delimited.
Run your query and then right click on results and click save results to file.
Once done rename the file from .rpt to .csv
Go to the Query menu > "Results to"... and then pick "to file" or whichever you want.
Change rpt extention to csv.
Be sure to re-run your queries.
If you want multiple queries result to a single file, you can follow below steps:
Create view for every SQL query you have got. This is just for loading purpose.
You can use Import-Export wizard in SQL Server Management Studio. Right click database > Tasks > Export Data.
In the Wizard choose SQL Server database as source and Excel file as destination.
Choose export multiple tables and select views as the source, and in the target excel, a separate sheet will be mentioned as the destination in the excel file.
Go to next steps in the wizard and Finish the wizard
Now the views data will be loaded to separate sheets in the target excel
Now, you can remove the views, as you dont need them
all the above things can be done inside SSMS.
There are many other options to load data from Multiple ways to export data from SSMS into separate files
I am writing an SSIS package to run on SQL Server 2008. How do you do an UPSERT in SSIS?
IF KEY NOT EXISTS
INSERT
ELSE
IF DATA CHANGED
UPDATE
ENDIF
ENDIF
See SQL Server 2008 - Using Merge From SSIS. I've implemented something like this, and it was very easy. Just using the BOL page Inserting, Updating, and Deleting Data using MERGE was enough to get me going.
Apart from T-SQL based solutions (and this is not even tagged as sql/tsql), you can use an SSIS Data Flow Task with a Merge Join as described here (and elsewhere).
The crucial part is the Full Outer Join in the Merger Join (if you only want to insert/update and not delete a Left Outer Join works as well) of your sorted sources.
followed by a Conditional Split to know what to do next: Insert into the destination (which is also my source here), update it (via SQL Command), or delete from it (again via SQL Command).
INSERT: If the gid is found only on the source (left)
UPDATE If the gid exists on both the source and destination
DELETE: If the gid is not found in the source but exists in the destination (right)
I would suggest you to have a look at Mat Stephen's weblog on SQL Server's upsert.
SQL 2005 - UPSERT: In nature but not by name; but at last!
Another way to create an upsert in sql (if you have pre-stage or stage tables):
--Insert Portion
INSERT INTO FinalTable
( Colums )
SELECT T.TempColumns
FROM TempTable T
WHERE
(
SELECT 'Bam'
FROM FinalTable F
WHERE F.Key(s) = T.Key(s)
) IS NULL
--Update Portion
UPDATE FinalTable
SET NonKeyColumn(s) = T.TempNonKeyColumn(s)
FROM TempTable T
WHERE FinalTable.Key(s) = T.Key(s)
AND CHECKSUM(FinalTable.NonKeyColumn(s)) <> CHECKSUM(T.NonKeyColumn(s))
The basic Data Manipulation Language (DML) commands that have been in use over the years are Update, Insert and Delete. They do exactly what you expect: Insert adds new records, Update modifies existing records and Delete removes records.
UPSERT statement modifies existing records, if a records is not present it INSERTS new records.
The functionality of UPSERT statment can be acheived by two new set of TSQL operators. These are the two new ones
EXCEPT
INTERSECT
Except:-
Returns any distinct values from the query to the left of the EXCEPT operand that are not also returned from the right query
Intersect:-
Returns any distinct values that are returned by both the query on the left and right sides of the INTERSECT operand.
Example:- Lets say we have two tables Table 1 and Table 2
Table_1 column name(Number, datatype int)
----------
1
2
3
4
5
Table_2 column name(Number, datatype int)
----------
1
2
5
SELECT * FROM TABLE_1 EXCEPT SELECT * FROM TABLE_2
will return 3,4 as it is present in Table_1 not in Table_2
SELECT * FROM TABLE_1 INTERSECT SELECT * FROM TABLE_2
will return 1,2,5 as they are present in both tables Table_1 and Table_2.
All the pains of Complex joins are now eliminated :-)
To use this functionality in SSIS, all you need to do add an "Execute SQL" task and put the code in there.
I usually prefer to let SSIS engine to manage delta merge. Only new items are inserted and changed are updated.
If your destination Server does not have enough resources to manage heavy query, this method allow to use resources of your SSIS server.
We can use slowly changing dimension component in SSIS to upsert.
https://learn.microsoft.com/en-us/sql/integration-services/data-flow/transformations/configure-outputs-using-the-slowly-changing-dimension-wizard?view=sql-server-ver15
I would use the 'slow changing dimension' task
I have a user table. I want to insert data into my user table.
I have a statement:
SELECT columna, columnb,
INTO my_table
FROM my_other_table
WHERE (... conditions ...)
I get the following error:
SQL Server Error on (myserver) Error:2714 at Line:1 Message:There is already an object named 'my_table' in the database.
Yes, thanks Sybase. I know this. I know the table exists. I want to insert data into it.
Why is Sybase not playing nicely? :(
(Sybase isn't my forte, Oracle is. This may just be an understanding issue, or lack there of. This would never happen in Oracle...)
SELECT ... INTO is for creating new tables.
Use INSERT ... SELECT for existing tables. eg:
INSERT INTO my_table
SELECT columna, columnb,
FROM my_other_table
WHERE (... conditions ...)
Have you tried it this way around?
Insert INTO my_table
SELECT columna, columnb,
FROM my_other_table
WHERE (... conditions ...)
It appears that it is trying to implicitly create a new table for you called my_table.
Not sure of SYBASE but in DB2 this works for me
INSERT INTO my_table
(
columna,
columnb
)
SELECT
columna,
columnb
FROM
my_other_table
WHERE
(... conditions...)
I think its safer to specify the columns in the insert statement as well rather than assume they'll be in the same order as the select.
Use 'existing' keyword after 'into' to insert in existing table.
SELECT [COLUMN LIST] INTO EXISTING [TABLE NAME]