SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


code sample


code sample

Author
Message
pedro.ribeiro
pedro.ribeiro
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 982
Hi- I have a Dought.



I have 2 tables, they are:

Table 1 with 2 fields and Table 2 that has 3 fields.


Table 1 has this 2 fields:

Name and address.


Table 2 has this 3 fields:


Number , Name and address.



This table 1 has 20000 records (for example) and I need to put this records (insert ) into table 2.


Two of the fields in this two tables are exactly of the same type and name.
But table 2 has one more field (that is not an identity, and it will not became one identity field) , that is numeric (int).


Table 2 has already same records and I need to insert now , all the data from table 1 to table 2.


Suppose my table 2 data is this:














1 Paul Street1
2 Marc Street2


How can I insert (code sample) the data of table1 into table 2 and the field of type number of table 2 is incremented in 1 every time a new record is placed on table2 ?

I only know how to do this by using cursors, but that’s not good.

Can someone help me please?

In this case , the first record to be insert into table2 would be inserted with the number 3.

Tks,

Pedro
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10154 Visits: 13351

INSERT INTO Table2 (Number, Name, Address)
SELECT ROW_NUMBER() OVER(ORDER BY Name, Address) AS counter,
Name, Address
FROM Table1



--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
pedro.ribeiro
pedro.ribeiro
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 982
tks old hand for reply so quick.

i will trie and will report to you the result

will this code start the counter from number 3? since number 1 and number 2 are already in the table 2?
Steve Jones
Steve Jones
SSC Guru
SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)SSC Guru (65K reputation)

Group: Administrators
Points: 65165 Visits: 19118
This will insert based on the result set, which will start from 1.

Also, please note your name and the poster's name are in the same position relative to the post. "Old Hand" is a level or title given based on posts. The poster's name is above that, in bold.

Follow me on Twitter: @way0utwest
Forum Etiquette: How to post data/code on a forum to get the best help
My Blog: www.voiceofthedba.com
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10154 Visits: 13351
I'm sorry, I didn't see you needed to start from 3.


INSERT INTO Table2 (Number, Name, Address)
SELECT ROW_NUMBER() OVER(ORDER BY Name, Address) + 2 AS counter,
Name, Address
FROM Table1



Sorry also for not putting my name in the reply.
I have such a few time to dedicate to SSC that sometimes I tend to be too concise.

Regards
Gianluca

--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
pedro.ribeiro
pedro.ribeiro
SSC Veteran
SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)SSC Veteran (239 reputation)

Group: General Forum Members
Points: 239 Visits: 982
tks once again for help.

But in this case i think you did not understud my question , i don't need to always start from 3.
I need to start from the last number in table 2 , in this case is 3, but it could be 10.


tks, again,

Pedro
Gianluca Sartori
Gianluca Sartori
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10154 Visits: 13351
Ok, so I guess you need something like this:


INSERT INTO Table2 (Number, Name, Address)
SELECT ROW_NUMBER() OVER(ORDER BY Name, Address) + (SELECT MAX(Number) FROM Table2) AS counter,
Name, Address
FROM Table1



--Gianluca Sartori

How to post T-SQL questions
spaghettidba.com
@spaghettidba
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search