The other day a co-worker was trying to figure out how to help a client update a lot of individual fields in a SQL table. He did not want to grant them direct access to the entire backend of the SQL server table but he also did not want to have to spend a lot of time “developing” code to do this update. This was a one-time update and using SSIS to create a package seemed like overkill. He also wanted to have control over which fields got update. He was trying to find an easy way of doing this. His client also has some experience using spreadsheets. I told him of a way to accomplish these requirements while at the same time giving his client the ability to “mass update” fields as needed.
Many people will take the an excel document and save it as a CSV or some other time and then use bulk load or some other tool to insert the records. The problem with this is you may not want all fields or records updated. I will explain a solution that many people forget about. You can use formulas in excel to be the solution. You can even use stored procedures to accomplish this. I will give you screen shots for better clarity.
Let’s start with an example. Suppose you want to update 900 customer records but you do not want all the fields updated only certain last names.
1. Open SQL Server Management Studio
2. Connect to the Database
3. Create a Select that returns all of the fields that you want to provide the user with (even the fields that you DO NOT want them to change) (It is important to export the unique key with this data)
4. When the results appear on the screen then right click on the results and ‘save results as’
5. Name the file and close SQL Server Management Studio.
6. Launch Excel and open the csv file.
7. The ‘text import Wizard -Step 1 of 3′ should appear (Figure 1)
8. Hit Next and select Comma (make the setting from Figure 2). If you have other delimiters than mark those instead.
9. Change the column format (if necessary) and then hit Finish (Figure 3)
10. You should have something that looks like Figure 4.
11. From here you can modify or lock down any of the column or cells you need to. Once you are done then give it to the client to make changes. Once they are done and give it back to you then the magic begins (Figure 5) NOTE: I have set the text to red for those changes necessary. (I will explain later)
12. In column D you will build your formula. This formula is nothing more than an update sql statement. (Figure 6)
13. Once you have the right formula then copy the formula down the page.
14. Once you have done that then copy and paste the formula into SQL Server Management Studio (Figure 7)
15. Execute it. –You are done.
Although I showed you a very simple example you could actually improve this like my co-worker did. He actually told the users to highlight the cells that they changes and he then ran a macro to catch each style change and then use that to create the update formula.
This solution can be iterative as well. If you use the same worksheet then you could update the data – send it back for more changes – and when you get it back all of your formulas are already there. All you have to do is copy the formulas and execute them in SQL Server.
Another use is that you could actually EXECUTE a stored procedure and pass in the parameters. Something like.
=”EXEC Update_Customers ” & A3 & “, ‘” & B3 & “‘”
The possibilities are endless.
Looking for quality web hosting? Look no further than Arvixe Web Hosting!