Lesson 2: Editing and executing a SQL script

In this lesson, you will learn how to enter SQL statements in the script, add or remove comments, execute the script, and view the results. Then you will use the Visual SQL wizard to modify the script, then execute it again with the modifications.

  1. In the SQL Editor where the play.sql file is open, type select  (the word “select,” followed by a space). Several things occur:

  2. Continue typing until the line reads:

    select * from store_shelf
    

    which means “select everything from the store_shelf table in the Sybase WorkSpace tutorial sample database.”

    As you type:

    Now you can execute the script; however, you must first start and connect to the tutorial database.

  3. If the tutorial database is already running, go to step 4.

    If the tutorial database is not running, in the WorkSpace Navigator, expand the folder MySybStore_Tutorials/Setup/Database. Right-click startMySybStore.bat and select Open With|System Editor to start the tutorial database.

    You should see the Adaptive Server Anywhere icon in your Windows system tray.

  4. If a connection to the SybStore tutorial database is already established, skip to step 5. When there is a successful connection, you see a database icon below the SybStore connection profile.

    If you are not connected, in the Enterprise Explorer, right-click the MySybStore connection profile, which you created in the tutorial setup, and select Connect to connect Sybase WorkSpace to the tutorial database.

  5. Execute the SQL statement by right-clicking in the editor and selecting Execute All.

    NoteWhen a SQL file is open, right-click in the editor and choose Execute All to run all SQL statements in a file. Choose Execute Selected Text to run only the selected text in a SQL file. You can also run all statements in a file after the file has been saved by right-clicking the file name in the WorkSpace Navigator and selecting Execute SQL File.

    You see the SQL Results view with the outcome of the script execution.

    Now you will use the Visual SQL wizard to add a SQL statement.

  6. In the editor, place the cursor at the end of the first line in the play.sql file, then press Enter twice. The cursor should be on line 3 of the file, which is where you want to add the new SQL statement.

  7. Right-click in the editor and select Visual SQL|Select from the context menu. The Visual SQL wizard opens.

  8. In the Select Tables window, select warehouse_inventory and click Open. You see three main views in the wizard:

  9. Highlight item_num and item_desc in the Table Layout view. These columns are added to Selection List bar in the Table Layout view and display the Preview view.

  10. To close the Visual SQL wizard and add the new statement to the SQL file, click OK.

  11. Comment out the first line in the SQL file. Place the cursor anywhere on line 1, right-click, and select Toggle Comment from the context menu.

    NoteYou can also uncomment the line or comment and uncomment multiple lines.

    • To uncomment one line, right-click anywhere on line 1 and select Toggle Comment from the context menu, or place the cursor at the beginning of the line and press Ctrl / (Control + Backslash).

    • To comment or uncomment multiple lines, select the lines, right-click on your selection, then select Toggle Comment from the context menu.

  12. Right-click in the editor and select Execute All from the context menu. Look at the SQL Results view to see the outcome of executing the modified script.

  13. Select the Result tab in the SQL Results view to see items selected from the table based on the executed statement.

  14. To save the SQL file, select File|Save from the Sybase WorkSpace main menu.

  15. Close the editor and Results views by clicking the “X” next to the view’s title. You now know how to create a SQL file, perform basic edits on that file, execute the file, and view the results.