12-7. Saving Pieces of Code for Quick Access

Problem

You want to save a portion of code so that it can be made easily reusable by other PL/SQL programs.

Image Tip This recipe also works for frequently used bits of SQL.

Solution

Use the Snippets window to create the reusable piece of code and use it for access at a later time.

How It Works

The Snippets window can be accessed by selecting the View menu and then choosing the Snippets option. The Snippets window will open as a pane on the far-right side of the Oracle SQL Developer environment. The pane consists of a toolbar that includes a button used for creating a new snippet and a button for editing an existing snippet. There is also a drop-down menu that consists of several menu options that organize each of the snippets into a different category. Figure 12-15 shows the Snippets pane.

Image

Figure 12-15. Snippets

The snippet is used by dragging its text onto a SQL worksheet or script. Once dragged onto the worksheet, the actual code is displayed in a template fashion. In some cases, you will need to change a bit of the text to make it usable, but the reusable code that is provided by the snippet can greatly reduce development time.

You can add your own snippet by selecting the icon that resembles a piece of paper with a plus sign on it from the Snippets panel. This opens the Save Snippet window (as shown in Figure 12-16) that gives you the option of using one of the existing categories or typing a new one. You can also type a name and tooltip for the snippet. The name of the snippet will appear in the Snippets panel after it has been saved. The text of the snippet itself will be placed into the worksheet once you drag the name of your snippet to a worksheet or script.

Image

Figure 12-16. Save Snippet pane

The Edit Snippet icon (the one with the pencil through it) brings up another window that allows you to choose an existing snippet to edit, create a new snippet, or delete a snippet. Only those snippets that you have created are available for editing. Figure 12-17 displays the Edit Snippets window.

Image

Figure 12-17. Edit Snippets window

The snippets are actually saved within an XML file named UserSnippets.xml. This file is located in your user sqldeveloper directory. This file can be transported to another machine and placed into the sqldeveloper directory so that the snippets can be made available in more than one place. This can be useful if you have a group of developers who may want to share snippets. The ability to copy the UserSnippets.xml file into other user sqldeveloper directories and make the snippets available to other users can certainly be advantageous.

Snippets can be useful for saving the time of typing a SQL or PL/SQL construct. They can also be beneficial if you do not remember the exact syntax of a particular piece of code. They provide quick access to template-based solutions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.163.91