Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-09-26 : 16:05:21
|
HiI have two tables: one is actual customers table and other is staging tableThe customers table has a RowID field and when i import data from Customer_Staging i would like to check the max(rowid) and start from the next number(max(rowID)+1)so on ..if the table is null it should start at 1 and then so on......How can i achieve this when i am using simple insert statement..Thank you |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
|
DonAtWork
Master Smack Fu Yak Hacker
2167 Posts |
Posted - 2012-09-27 : 06:33:03
|
Even better, change your ID to an identity field, and you will never have to mess with it!How to ask: http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-09-27 : 10:47:11
|
Thank You Webfred for you repsonse..but i am having a strange problem..May i was not clear in my question ..but now my sql statement looks like thisMERGE Customers AS CUSING NewCustomers AS NCON C.LastName = NC. LastNameWHEN MATCHED THEN UPDATE SET C.FirstName = NC.FirstNameWHEN NOT MATCHED THEN INSERT (LastName, FirstName) VALUES (NC.LastName,NC.FirstName);My customers table has RowID..how can i insert the Rowid here for each row when updating data from newcustomersThank you..This what i tried but does not work Says 'Windowed functions can only appear in the SELECT or ORDER BY clauses.'DECLARE @offset INT select @offset=isnull(max(RowID),0) from CustomersMERGE Customers AS CUSING NewCustomers AS NCON C.LastName = NC. LastNameWHEN MATCHED THEN UPDATE SET C.FirstName = NC.FirstNameWHEN NOT MATCHED THEN INSERT (LastName, FirstName,RowID) VALUES (NC.LastName,NC.FirstName,row_number() over(order by id)+@offset); |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-27 : 10:57:14
|
as suggested before, why cant you've RowID as identity in which case it will get values by itself------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
akpaga
Constraint Violating Yak Guru
331 Posts |
Posted - 2012-09-27 : 11:26:44
|
The Row Id field nor the table is not owned by me ..I just have permission to insert it in the following way...Thank you |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-09-27 : 11:33:17
|
then try thisDECLARE @offset INT select @offset=isnull(max(RowID),0) from CustomersMERGE Customers AS CUSING (SELECT ROW_NUMBER() OVER ORDER BY ID) AS Seq,*FROM NewCustomers) AS NCON C.LastName = NC. LastNameWHEN MATCHED THENUPDATE SET C.FirstName = NC.FirstNameWHEN NOT MATCHED THENINSERT (LastName, FirstName,RowID) VALUES (NC.LastName,NC.FirstName,NC.Seq +@offset); ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|