| Author |
Topic |
|
DesiGal
Starting Member
31 Posts |
Posted - 2010-03-02 : 11:44:42
|
| --Consider I have two tablesCustomers(CustomerId,Name,VehicleData,.....)CustomerId is the Identity field(Primary Key,autonumber)Vehicle(CustomerId,Name,VehicleData,...)customerId column in null in this tableThis is just test example.(somewhat similar to my scenario)Here I am trying to insert all the rows from vehicle table into the Customers table,retrieve the auto populated customerId for each row from the customers table and update the CustomerId in the Vehicle table. The problem is ,there is no unique field to create a join on,hence i have to perform both the operations in the same transaction.BEGIN TRANSACTIONDECLARE @CustomerID intinsert into Customers(Name,VehicleData)select Name,VehicleData from VehicleSELECT @CustomerID = scope_identity()Update Vehicleset Vehicle.CustomerId=@CustomerIDCOMMITThe problem with this is ,for all the rows the Customer Id is the same,Last generated autonumber in the customers table |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:02:02
|
| [code]BEGIN TRANSACTIONDECLARE @INSERTED_CUSTOMERS table(Customer_ID int,VehicleData varchar(8000))INSERT Customers (Name,VehicleData)OUTPUT INSERTED.CustomerID,INSERTED.VehicleData INTO @INSERTED_CUSTOMERSSELECT Name,VehicleData from VehicleUPDATE vSET v.CustomerId= i.CustomerIDFROM Vehicle vINNER JOIN @INSERTED_CUSTOMERS iON i.VehicleData = v.VehicleData COMMIT[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rocknpop
Posting Yak Master
201 Posts |
Posted - 2010-03-02 : 12:04:56
|
| can't you do the following, remove the scope_identity select:Update Vset V.CustomerId=C.CustomerIDFrom Vehicle V Inner Join Customer COn V.Name=C.Name And V.VehicleData=C.VehicleDate--------------------Rock n Roll with SQL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:09:46
|
| I cant understand why you need to include vehicle information in your customer masters table though!------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DesiGal
Starting Member
31 Posts |
Posted - 2010-03-02 : 12:24:57
|
| @visakh16This is just a test example.My database is lot different. This is a simplified version of what I am looking for. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 12:28:52
|
| Ok . Otherwise it doesnt make any sense------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DesiGal
Starting Member
31 Posts |
Posted - 2010-03-02 : 13:04:25
|
| visakh16Yes I know.The problem with this query is thatVehicle(CustomerId,Name,VehicleData,...)There is no guarantee that (Name,VehicleData)columns in the Vehicle table are unique.It does insert the row in the Customers table even if the combination(Name,VehicleData) is repeated,but it does not update the correct customerID in this caseexampleBefore Insert the vehicle table isCustomerId Name VehicleDatanull abc testDatanull xyz testData2null xyz testData2Customers table is emptyAfter insert customer table containsCustomerId Name VehicleData1 abc testData2 xyz testData23 xyz testData2vehicle table should beCustomerId Name VehicleData1 abc testData2 xyz testData23 xyz testData2i get the following for the Vehicle tableCustomerId Name VehicleData1 abc testData2 xyz testData22 xyz testData2 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 13:06:14
|
| dont you have any unique valued column in vehicle table?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
DesiGal
Starting Member
31 Posts |
Posted - 2010-03-02 : 13:13:56
|
| nope,that's the problem.Every time I insert a customer...vehicle row gets inserted..What I am trying to do here is not normal data insertion I am basically trying to import data from other table and hence this trouble to sync the data |
 |
|
|
DesiGal
Starting Member
31 Posts |
Posted - 2010-03-02 : 13:45:43
|
| I am looking for something similar to foreach row added in Customers table from the vehicle table update its customerID(In the vehicle table) to the newly generated autonumber in the customers table |
 |
|
|
|