SQL Server Data Tools in Visual Studio 2. Safe Refactoring. Refactoring existing database objects using SSDT. Refactoring existing database objects is just as easy as adding new. Examining our SQL Server database. ![]() With Visual Studio Database Projects (VSDB), you entered into Safe Refactoring mode through the Schema View window. But in SSDT, the Schema View window no longer exists. So how the heck do you do it?? Well first off, let’s define safe refactoring for those who may not have been familiar with the feature in VSDB. It allowed you to right click on a column name, and pick rename.
You could then enter a new name, and hit the preview button. Visual Studio would comb through your source code and find every occurrence of that column name for that table and show it to you. If you clicked OK, Visual Studio would then go through and make the change everywhere for you, ensuring you didn’t miss anything. Before we can do a safe refactor, we need to have something to refactor, and to test with. For this demo, we’ll create a simple view to read the dbo. Are there any formal techniques for refactoring SQL similar to. I've mostly done SQL Server work and.Arcane. Code table we created in the previous post. Since there is no folder in the dbo schema for views, we’ll first have to create it. Still working with our Adv. Works sample, open the dbo folder. Right click on it, and pick Add, Folder. Name the folder Views. Let me interject that strictly speaking, this isn’t a requirement. We could have placed the new view source file we’re about to create anywhere. But keeping a file/folder structure consistent with this and other SSDT projects will make maintenance far easier. OK, once the folder is there, right click on it, again pick add, only this time pick a new View. Name the view dbo. Arcane. Code. Now return to the table designer for the new Arcane. Code table. Let’s decide we don’t like the name Blog. Url, and we’d rather call it Blog. Site. Well, first we could try putting our cursor into the T- SQL area, and just typing over Blog. Url with Blog. Site. Well, as you can see above, the designer on the upper half changes, but the column name in Create Index did not update. Not nearly what we wanted. Restore the name in the T- SQL area back to Blog. Url. Now go into the grid at the top, and change it there. Once we tabbed out of the Name column (or clicked elsewhere) it changed the name in all the locations in the T- SQL area. Just what we wanted! Or is it? Remember, we also used this column in the vw. Arcane. Code. Go take a look at its code. As you can see, it remained unchanged. Even worse, there’s now a big red squiggly under the Blog. Url name, indicating we have now created an error in our project. Go back to the table designer, and reset the name back to Blog. Url. Now we’re finally ready to do this the right way. Put your cursor somewhere in the column name in the T- SQL area, then right click on the column name. Note, this will not work if you try it in the grid area at the top! From the menu, pick Refactor, Rename. You’ll now see a dialog appear, giving you a place to enter a new name. Change the name to Blog. Site, and make sure the Preview Changes is checked on, then click OK. Once you do, a new window will appear. It will show you everywhere the change would be made. Looking at the window, the upper part shows you all the files and the line of code in the file for which a change would be made. If you click on a line, the new version of the code will appear in the lower window. Ahh, there’s the view, and you can see it is being shown with the new change that will be made. Note that no change has occurred as of yet. You must click the Apply button for anything to actually change. You can also click Cancel to abandon the change. If you need to know everywhere a column name is used, but don’t want to change it, there’s an easy way to do that too. When we right clicked on the column name and picked Refactor, well a few menu options down was another option called “Find All References”. Picking that will populate a window in your Visual Studio environment. When you click on a line in the above window, your central display in Visual Studio changes to now show the file you clicked on in this window. There you go, you can now not only safely refactor column names in Visual Studio 2. SSDT, but you can also find all references to that column within your project.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
September 2017
Categories |