Author |
Topic |
Abid
110 Posts |
Posted - 2014-03-03 : 06:27:28
|
Hi. It is first time that I'm having this problem; I had developed an application which has been deployed on client's computer. Now he requested for some changes in application for which i have to change the tables structure as well as lot of coding too. What i am worry about is that if i make changes to the structure of tables so how i will incorporate the old data (the client has started working upon the application)into this newly structure tables because this newly structured tables will have more or less columns than the previous one. Please guide me that how to solve it.My table for the time being is:SaleID (Primary Key)SaleTotalAmountSalePaidAmountSaleDiscountSaleNetBalanceSaleDate And fields have to incorporate are:SaleTotalProfitSaleTotalLoss I ran the alter query and it worked fine for me. But the problem is that If i give this new DB to client, so how i will incorporate the old data into this newly altered DB?Let me briefly explain my application. I developed a standalone PC application where both DB and application are installed on a single PC. |
|
senthil_nagore
Master Smack Fu Yak Hacker
1007 Posts |
Posted - 2014-03-03 : 06:53:32
|
Simple way is, prepare an Alter Script (Adding new column) and execute in client DB to change the table structure.Senthil Kumar C------------------------------------------------------MCITP - Database Administration SQL SERVER 2008MCTS - Database Development SQL SERVER 2008 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2014-03-03 : 13:03:07
|
quote: Originally posted by Abid Hi. It is first time that I'm having this problem; I had developed an application which has been deployed on client's computer. Now he requested for some changes in application for which i have to change the tables structure as well as lot of coding too. What i am worry about is that if i make changes to the structure of tables so how i will incorporate the old data (the client has started working upon the application)into this newly structure tables because this newly structured tables will have more or less columns than the previous one. Please guide me that how to solve it.My table for the time being is:SaleID (Primary Key)SaleTotalAmountSalePaidAmountSaleDiscountSaleNetBalanceSaleDate And fields have to incorporate are:SaleTotalProfitSaleTotalLoss I ran the alter query and it worked fine for me. But the problem is that If i give this new DB to client, so how i will incorporate the old data into this newly altered DB?Let me briefly explain my application. I developed a standalone PC application where both DB and application are installed on a single PC.
If new tables have more columns its not an issue. The existing columns will have data preserved whereas new columns will only have data for subsequent rowsIf its opposite ie table having less columns, then if any of dropped columns are used in clients application, application will break after this change. So you need to check dependency before removing exusting columns------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
maunishq
Yak Posting Veteran
71 Posts |
Posted - 2014-03-03 : 13:04:36
|
Since you have altered the old table, now the old table contains these two new columns which are null/empty at the moment.Run the UPDATE query for both new columns and you are good to go.!_(M)_! |
|
|
Abid
110 Posts |
Posted - 2014-03-06 : 11:43:51
|
@Visakh, Yes, i have to add columns, not to remove. I've added 1 column below, with alter statement. Column is added successfully, but you know that this will be empty and new records will be fully inserted with data. But the problem is that how to update the newly entered empty Column with suitable data. The older table was:ProductBasicInfoProdId (Primary Key)ProdNameDescriptionManufacturerStoreThe newly inserted field is ProdPrice, which is currently null for all the records. Surely the new records will be properly inserted but i have to fill the empty records. Please guide me now that how to update the empty records. Is my problem understand to you sir?@maunishqHow to run the suitable update command. |
|
|
Abid
110 Posts |
Posted - 2014-03-06 : 11:45:23
|
My update query is:Try updateTable("ProductBasicInfo", "ProdName = '" & txtProdName.Text.Trim & "', Description = '" & txtProdDesc.Text.Trim & "', Manufacturer = '" & txtProdManuf.Text.Trim & "', Store = " & txtStore.Text.Trim & ", ProdPrice = " & txtProdPrice.Text.Trim & "", " ProdName = '" & txtOldValue.Text.Trim & "' and Description = '" & txtOldDesc.Text.Trim & "' and Manufacturer = '" & txtOldManuf.Text.Trim & "' and Store = " & txtOldStore.Text.Trim & " and ProdPrice = " & txtOldProdPrice.Text.Trim & "") MessageBox.Show("Record updated successfully") btnUpdate.Enabled = False btnProdSubmit.Enabled = Enabled txtProdName.Clear() txtProdDesc.Clear() txtProdManuf.Clear() txtStore.Clear() txtProdPrice.Clear() Catch ex As Exception MessageBox.Show(ex.Message) Finally cnSql.Close() End Try Where updateTable is a function in a separate module:#Region "Updating" Public Sub updateTable(ByVal tblName As String, ByVal parameter As String, ByVal criteria As String) Try cmdSql1.CommandText = " update " & tblName & " set " & parameter & " where " & criteria & "" cmdSql1.Connection = Connect() daSql.UpdateCommand = cmdSql1 daSql.Fill(dsSql, tblName) Catch ex As Exception MessageBox.Show(ex.Message) End Try End Sub#End Region |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-06 : 12:21:46
|
Not familiar with the code you've provided :) So the issue is to update the new field with appropriate data, since by the time you alter it it won't be having any information for the records already exists. You may use two approaches 1) explicitly update: after you add these fields and do appropriate coding in the application to populate it. It would turn out to be scenario that for all new records the information would be available, except for the ones that were added before these new fields. So provide the old data to the user/client and request them to fill it out for you with their desired values. once they provide it update the new fields accordingly via SQL. 2) Add as Computed fields: the new fields sounds more like computed information. Considering that you know the pre-req of a computed column, and with that, iff this is the case and all the fields are available on that table to fulfill the cumputation, add them as computed column. CheersMIK |
|
|
maunishq
Yak Posting Veteran
71 Posts |
Posted - 2014-03-06 : 14:32:36
|
So, You want to update SaleTotalProfit and SaleTotalLoss columns of your newly altered table.UPDATE new_upd_tblSET SaleTotalProfit = ...calculated formula..., SaleTotalLoss = ...calculated formula...=======================Not an Expert, Just a learner.!_(M)_! |
|
|
Abid
110 Posts |
Posted - 2014-03-07 : 08:17:07
|
@ MIK, explicit updating, means that i update each and ever record one by one, is it what you want me to do? |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-07 : 09:28:46
|
that's not what I want you to do.. but if the requirements are such that you need to go through that approach then you've to do it that way .. :) If there are thousands of record already exist in the table, how come you know what should be the profit and loss for each record, unless there is a formula which can be used to derive this information, so you need to get this information from client. Generally in such cases an excel sheet is provided where the client/requester fill it up with the desired information and give it back to the IT programmer / DBA/ Developer. Who then use that information and update the corresponding records. This update could be using one by one UPDATE Table (DML) commands or by and advance way like reading the excel file using openrowset etc functions, and joining it back to the extent table using the PK information and simply updating the two new fields. On the other hand if there is a generic rule for computing profit and loss information then you can use computed field e.g. We have a table that has the following fields and are already having huge amount of data. Product, OriginalPrice, Price_SoldNow you need to add two new fields, Profit and Loss to this table. And the concept behind Profit is: if Price_Sold - OriginalPrice is "positive number" that means its a profit and if the same turns out to be "negative number" means its a loss. So you can add them as computed field to the table. Its all about the approach (more precisely "correct" approach) so solve the issue.CheersMIK |
|
|
Abid
110 Posts |
Posted - 2014-03-07 : 11:24:44
|
Let's have a simple one table, of the same project, before solving this complex one of Sale.Hi. I have a table called ProductInfo having the fields:ProdID (Primary Key)ProdNameDescriptionManufacturerStore later on, on client demand i altered the table and added a new column called:ProductPrice. As the new column in table is entirely empty (null), so i need to update it withsuitable values (price) against each product record in a table.My code for updating a record is:Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.ClickTryupdateTable("ProductBasicInfo", "ProdName = '" & txtProdName.Text.Trim & "', Description = '" & txtProdDesc.Text.Trim & "', Manufacturer = '" & txtProdManuf.Text.Trim & "', Store = " & txtStore.Text.Trim & ", ProdPrice = " & txtProdPrice.Text.Trim & "", " ProdID = " & txtProdID.Text.Trim & "")MessageBox.Show("Record updated successfully")Catch ex As ExceptionMessageBox.Show(ex.Message)FinallycnSql.Close()End TryEnd Sub UpdateTable is actually a function at module level in vb.net module.This query only updates individual record at a time, but in actual scenario i have hundreds of records to be updated, So Isn't there any way by which i update all records with single attempt. |
|
|
MIK_2008
Master Smack Fu Yak Hacker
1054 Posts |
Posted - 2014-03-07 : 12:11:10
|
if you want how to do it via VB code I dont know .. neither this forum is for VB coding. But if you want to update via SQL, I think I've already given you the answer for this. For hundreds of records you need to have hundreds of values for each record of the ProductInfo table. Once you have this information. Create a temporary table (ProdID, Price) and load it with required information and then use following query to get the ProductInfo table updated in one goUPDATE ASET Price=tempTable.Price FROM ProductInfo A INNER JOIN tempTable on tempTable.ProdID=A.ProdIDCheersMIK |
|
|
Abid
110 Posts |
Posted - 2014-03-07 : 21:00:34
|
Dear MIK, let me clear my self. You want me that the client will give me all the prices (list/ information) and i will put it in a separate table (e.g. PriceTable) and then i will update my ProductInfo table with that newly created table? Did I pick you right sir? |
|
|
Abid
110 Posts |
Posted - 2014-03-08 : 22:47:57
|
quote: Originally posted by MIK_2008 if you want how to do it via VB code I dont know .. neither this forum is for VB coding. But if you want to update via SQL, I think I've already given you the answer for this. For hundreds of records you need to have hundreds of values for each record of the ProductInfo table. Once you have this information. Create a temporary table (ProdID, Price) and load it with required information and then use following query to get the ProductInfo table updated in one goUPDATE ASET Price=tempTable.Price FROM ProductInfo A INNER JOIN tempTable on tempTable.ProdID=A.ProdIDCheersMIK
What is A MIK, in the query? |
|
|
|