Modify Data Through a View - SQL Server (2024)

  • Article

Applies to: Modify Data Through a View - SQL Server (1) SQL Server Modify Data Through a View - SQL Server (2) Azure SQL Database Modify Data Through a View - SQL Server (3) Azure SQL Managed Instance Modify Data Through a View - SQL Server (4) Azure Synapse Analytics Modify Data Through a View - SQL Server (5) Analytics Platform System (PDW)

You can modify the data of an underlying base table in SQL Server by using SQL Server Management Studio or Transact-SQL.

Before You Begin

Limitations and Restrictions

Permissions

Requires UPDATE, INSERT, or DELETE permissions on the target table, depending on the action being performed.

Using SQL Server Management Studio

To modify table data through a view

  1. In Object Explorer, expand the database that contains the view and then expand Views.

  2. Right-click the view and select Edit Top 200 Rows.

  3. You may need to modify the SELECT statement in the SQL pane to return the rows to be modified.

  4. In the Results pane, locate the row to be changed or deleted. To delete the row, right-click the row and select Delete. To change data in one or more columns, modify the data in the column.

    Important

    You cannot delete a row if the view references more than one base table. You can only update columns that belong to a single base table.

  5. To insert a row, scroll down to the end of the rows and insert the new values.

    Important

    You cannot insert a row if the view references more than one base table.

Using Transact-SQL

To update table data through a view

  1. In Object Explorer, connect to an instance of Database Engine.

  2. On the Standard bar, click New Query.

  3. Copy and paste the following example into the query window and click Execute. This example changes the value in the StartDate and EndDate columns for a specific employee by referencing columns in the view HumanResources.vEmployeeDepartmentHistory. This view returns values from two tables. This statement succeeds because the columns being modified are from only one of the base tables.

    USE AdventureWorks2022; GO UPDATE HumanResources.vEmployeeDepartmentHistory SET StartDate = '20110203', EndDate = GETDATE() WHERE LastName = N'Smith' AND FirstName = 'Samantha'; GO 

For more information, see UPDATE (Transact-SQL).

To insert table data through a view

  1. In Object Explorer, connect to an instance of Database Engine.

  2. On the Standard bar, click New Query.

  3. Copy and paste the following example into the query window and click Execute. The example inserts a new row into the base table HumanResouces.Department by specifying the relevant columns from the view HumanResources.vEmployeeDepartmentHistory. The statement succeeds because only columns from a single base table are specified and the other columns in the base table have default values.

    USE AdventureWorks2022; GO INSERT INTO HumanResources.vEmployeeDepartmentHistory (Department, GroupName) VALUES ('MyDepartment', 'MyGroup'); GO 

For more information, see INSERT (Transact-SQL).

Feedback

Was this page helpful?

Feedback

Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see: https://aka.ms/ContentUserFeedback.

Submit and view feedback for

Modify Data Through a View - SQL Server (2024)
Top Articles
Latest Posts
Article information

Author: Francesca Jacobs Ret

Last Updated:

Views: 6012

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Francesca Jacobs Ret

Birthday: 1996-12-09

Address: Apt. 141 1406 Mitch Summit, New Teganshire, UT 82655-0699

Phone: +2296092334654

Job: Technology Architect

Hobby: Snowboarding, Scouting, Foreign language learning, Dowsing, Baton twirling, Sculpting, Cabaret

Introduction: My name is Francesca Jacobs Ret, I am a innocent, super, beautiful, charming, lucky, gentle, clever person who loves writing and wants to share my knowledge and understanding with you.