The SQL Server Data row cannot delete or copy the table structure.

Source: Internet
Author: User
In SQL Server, all rows of data are duplicated and cannot be updated or deleted. The problem is usually the table without a primary key. Error: Check whether the table has a primary key. If not, you have

In SQL server, all rows of data are duplicated and cannot be updated or deleted. The problem is usually the table without a primary key. Error: Check whether the table has a primary key. If not, you have

The SQL Server Data row cannot delete or copy the table structure.

[Date:] Source: Linux community Author: cjr15233851143 [Font:]

The following are the two problems I encountered when working on the system:

<1>. SQL server has duplicate rows of data and cannot be updated or deleted.

It is usually a table without a primary key.

Error:

Check whether the table has a primary key. If not, use a temporary table to delete duplicate rows.

Example:

Method 1:

Delete from tab_test2 where id = (select max (id) from tab_test2 group by id, uid having count (1)> 1)

Method 2: Note: # t is a temporary table, which can be found under the temporary table node in the tempdb database and will disappear after the SQL Server service is restarted.

Select id, uid into # t from tab_Test2 group by id, uid

Truncate table tab_Test2

Insert into tab_Test2 select * from # t

<2>.Copy the structure of other tables to a specified table.

For example:

There are three tables as follows::

Consider the following question: is the specified table empty?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.