Link to home
Start Free TrialLog in
Avatar of cheryl9063
cheryl9063Flag for United States of America

asked on

T-SQL Best Practice Insert Statement

I'm looking for the BEST WAY based on performance to reinsert all records from one table to another table only changing the values of one column.. My initial thought is to create a temp table and put the results of my query in that and then update the column and then insert the data back into the original table.. Would this be the best way?
Avatar of chapmandew
chapmandew
Flag of United States of America image

Here's an epic article on the INSERT statement:  http://www.sqlservernation.com/home/sql-101-the-insert-statement.html

basically, you can just use a SELECT statemetn to do the insert so you don't have to do the insert into temp table and update.
You have a confused question

...to reinsert all records from one table to another table...

... insert the data back into the original table..

So does it end up in the original, or another table??

If it ends up in the original table, isn't that just UPDATE?

update tbl set col = 'X'

If it is to another table

insert target (col, col2, colC) select scol, 'X', scolC from source

(here 'X' is substituted for the value of col2)
Avatar of cheryl9063

ASKER

That article does not address my issue and it's not about how to do an insert... I don't think I explained myself well which is always my problem.. Below is the code to create the table I'm lookin at.. Below that is what I need it to look like.. This is just test data in reality it's much much bigger..
INSERT INTO [CORE].[tblMatrix]
([nProjectID], [nSiteID], [nRoleID], [nTeamID], [nIssueID], [nActionID], [nEditModeID], [dSLANumHours], [bBundleAction], [nPriorityID])

SELECT 3, 1, 6, 1, 282, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 433, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 433, 3, 0.00, 0, 2


-----------------------------------------------------------------

INSERT INTO [CORE].[tblMatrix]
([nProjectID], [nSiteID], [nRoleID], [nTeamID], [nIssueID], [nActionID], [nEditModeID], [dSLANumHours], [bBundleAction], [nPriorityID])
SELECT 3, 1, 6, 1, 282, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 282, 433, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 3, 1, 6, 1, 287, 433, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 282, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 282, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 282, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 282, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 282, 433, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 287, 4, 1, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 287, 5, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 287, 8, 1, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 287, 39, 3, 0.00, 0, 2 UNION ALL
SELECT 5, 1, 6, 1, 287, 433, 3, 0.00, 0, 2

Open in new window

It goes into the same table.. It's not an update becuase I need the old data in the same table AND the new data.. There are about 500 rows.. I just need to do an insert BUT I want to use the existing rows and only change 1 column value from 3 to 5 for all 500 rows.. I would then have 1000 rows that look alike except for 1 column..
ASKER CERTIFIED SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That was easy.. Thanks!!