Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
If you prefer to use scripts to create or edit views, stored procedures, functions, triggers, or user-defined-types, you can use the Transact-SQL Editor. The Transact-SQL Editor provides IntelliSense and other language support. For more information, see Use Transact-SQL Editor to edit and execute scripts.
The Transact-SQL Editor is invoked when you use the View Code contextual menu to open a database entity in a connected database or a project. It automatically opens when you use the New Query contextual menu from the SQL Server Object Explorer, or when you add a new script object to a database project. If you aren't connected to a database but want to execute a query against it, you can also use the New Query Connection dialog box by selecting Transact-SQL Editor menu from the SQL menu to connect to a database and launch the Transact-SQL Editor.
Create a new table using a Transact-SQL query
Right-click the
Tradedatabase node and select New Query.In the script pane, paste in this code:
CREATE TABLE [dbo].[Fruits] ( [Id] INT NOT NULL, [Perishable] BIT DEFAULT ((1)) NULL, PRIMARY KEY CLUSTERED ([Id] ASC), FOREIGN KEY ([Id]) REFERENCES [dbo].[Products] ([Id]) );Select the Execute Query button in the Transact-SQL Editor toolbar to run this query.
Right-click the
Tradedatabase in SQL Server Object Explorer and select Refresh. A newFruitstable has been added to the database.
Create a new function
Replace the code in the current Transact-SQL Editor with the following script:
CREATE FUNCTION [dbo].GetProductsBySupplier (@SupplierId INT) RETURNS @returntable TABLE ( [Id] INT NOT NULL, [Name] NVARCHAR (128) NOT NULL, [Shelflife] INT NOT NULL, [SupplierId] INT NOT NULL, [CustomerId] INT NOT NULL) AS BEGIN INSERT @returntable SELECT * FROM Products AS p WHERE p.SupplierId = @SupplierId; RETURN; ENDThis function returns all rows in the
Productstable whoseSupplierIdequals to the specified parameter. Select the Execute Query button in the Transact-SQL Editor toolbar to run this query.In SQL Server Object Explorer, under the
Tradenode, expand the Programmability and Functions nodes. You can find the new function you created under Table-valued Functions.
Create a new view
Replace the code in the current Transact-SQL Editor with the following. Then select the Execute Query button above the editor to run this query.
CREATE VIEW [dbo].PerishableFruits AS SELECT p.Id, p.Name FROM dbo.Products AS p INNER JOIN dbo.Fruits AS f ON f.Id = p.Id WHERE f.Perishable = 1;In SQL Server Object Explorer, under the
Tradenode, expand the View node to locate the new view you created.