Save and share your work by creating views, persisted SQL statements, for future use and for sharing with other users.
- We have come a long way building this query, from several things we've learned, early on. We've learned about a list of columns. We have learned about aggregate functions. We have learned about GROUP BUY and how we specified the order that we would like the results to be returned in. We've even limited our results to the top five. So, this amount of work will happen for many queries. And thus, we might want to save the query itself for later use or for inclusion in even larger queries.
So let's save our work, in SQL Server Management Studio. Rename the file. However, what we just saved is a text file in SQL Server Management Studio. This does not store our query somewhere in SQL Server itself. For that we can take one additional step. Add a new file. And we can copy and paste. Going to go back to the previous file.
Going to copy the select statement, Ctrl+C. Go to our new file, Ctrl+V. Click Execute and make sure it works. There's the top five. We've designed our query, this is what we want. We can save our query inside SQL Server itself by saying CREATE VIEW, and this would be TopFiveWaterfallsUSA. And we're saying creating a view AS that statement.
So the VIEW, this name, will be that entire statement. Click Execute, Command completed successfully, but no results. Because what happened is this name is now a saved query inside the SQL Server database engine itself. So copy the name, go to the end of the file. And say SELECT * FROM that name. So the name encompasses the query that we've previously defined; and SQL Server knows that.
So when we say SELECT * FROM TopFiveWaterfallsUSA, click Execute, and you get the same result as before. So now it is simpler to say SELECT * FROM TopFiveWaterfallsUSA WHERE Region = 'OR'. Select that line, click Execute, and you get the one result you're looking for. So this is a way to simplify combinations of queries. This query at the bottom is fairly simple, but it encompasses the first query.
SELECT TOP 5, GROUP BY, ORDER BY, all of that is done in the background and all we see in our new query is a fairly simple SELECT * FROM statement. So this is your ability to save your queries, inside SQL Server for later use. And SQL Server will remember all the hard work you've done, building up the query in the first place. And we save the file, rename.
- Creating a new Azure SQL database
- Using basic SQL statements such as SELECT and INSERT
- Using SQL statements to update, modify, and delete data
- Adding functions to your SELECT statement to modify how numbers are displayed
- Filtering and formatting results
- Saving and sharing your work by creating views
- Sharing access to your database with other users