SQL Server Delete data from the column using a delete query without removing the entire row? I don't want to drop the column [closed]

xdyibdwo  于 2023-06-04  发布在  其他
关注(0)|答案(2)|浏览(167)

Closed. This question needs details or clarity . It is not currently accepting answers.

Want to improve this question? Add details and clarify the problem by editing this post .

Closed 2 days ago.
Improve this question

Is it possible to delete all data from a specific column without affecting data in other columns within the same row, without using an update query? I don't want to drop the column.

1tuwyuhd

1tuwyuhd1#

UPDATE your_table SET your_column = NULL;
fumotvh3

fumotvh32#

If you want to "delete" the values in a column in all rows then you need to UPDATE the table; DELETEdeletes rows not values in a specific column. So, in pseudo-SQL this would be:

UPDATE YourSchema.YourTable
SET YourColumn = <New Value>;

What your new value is is up to you; it might be NULL , a zero length string ( '' ), 0 , etc.

Such an operation will be an offline operation though, so for a large table this could result in a lengthy lock. If so, and you're on Enterprise/Developer, you might want to consider DROP ing and re ADD ing the column, provided that nothing else is dependent on said column. This is because adding a column (with a default value) is an online process in Enterprise, as it's a metadata change:

ALTER TABLE YourSchema.YourTable DROP COLUMN YourColumn;
GO
ALTER TABLE YourSchema.YourTable ADD YourColumn varchar(10) NULL CONSTRAINT DF_YourTable_YourColumn DEFAULT '' WITH VALUES;

If you want NULL / NOT NULL a DEFAULTCONSTRAINT , WITH VALUES , etc, is dependant on your needs.

For transparency, I VTC'd after posting this answer, as from the comments on the question, it appears that the question asked and the question the OP wants to ask are not the same. This answer addresses the question asked; it is unclear what the OP is really asking.

相关问题