Mahen dran Mahen dran - 1 month ago 6
SQL Question

How to update the same value for new updated column in SQL Server for all rows?

I have insert 10,000 rows into table which contains 3 columns.

Now i need to add the new column into table.And also need to store the value to updated column which is to be same for all 10,000 rows.

For example:

my table like below..,

No Name ID
1 raj 1000
2 ravi 1001
3 git 1002
..
.
.
10000 dat 10,000


Now i need to add the new column "date"
Then data like this..,

No Name ID Date
1 raj 1001
2 ravi 1002
3 git 1003


I have using below query to add new column

ALTER TABLE table_name
ADD Date date


but i need to know how to store the same data into all rows in table like below.

No Name ID Date
1 raj 1001 10.12.2020
2 ravi 1002 10.12.2020
3 git 1003 10.12.2020
.
.
.
.
10,000 dat 10000 10.12.2020


How can i achieve above requirement?

I am little bit know about sql server.

Can anyone please help me to solve this?

Answer
        ALTER TABLE table_name 
        ADD CONSTRAINT DF_date DEFAULT N'10.10.2020' FOR [date];

    or
    ALTER TABLE [dbo].table_name
            ADD CONSTRAINT DF_table_name_column_name DEFAULT ('10.10.2020') FOR column_name

 iam giving one example

CREATE TABLE #TEST(PART VARCHAR(10),LASTTIME DATETIME)
GO

ALTER TABLE [DBO].#TEST
        ADD CONSTRAINT DF_#TEST_LASTTIME DEFAULT ('10.10.2020') FOR LASTTIME
INSERT INTO #TEST (PART )
VALUES('A')

INSERT INTO #TEST (PART )
VALUES('B')

INSERT INTO #TEST (PART )
VALUES('AA')

INSERT INTO #TEST (PART )
VALUES('BA')


GO