Click here to Skip to main content
16,020,673 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Dear Friends,

I have already created a table name as Student, now I have 10 records in that table. Unfortunately I forgot that to set a default value for Result column, now I have tried to set default value for Result column but it shows the error on set. The bellow query I have to used.


SQL Quey: alter table Student alter Result set default 'Pass'


Thanks in advance.
Posted
Comments
MuhammadUSman1 11-Sep-14 2:46am    
Check solution

Try this,
ALTER TABLE STUDENT ADD CONSTRAINT CONS_DF_RESULT DEFAULT N'Pass' FOR Result;
 
Share this answer
 
Comments
Member 10021658 11-Sep-14 2:51am    
Thanks for reply Syed. The query is execute successfully, but its not updated for already existing records.
_Asif_ 11-Sep-14 2:54am    
A simple Update is required to update existing records.

UPDATE STUDENT
SET Result = 'Pass'
WHERE RESULT IS NULL;
Member 10021658 11-Sep-14 2:59am    
(0 row(s) affected)
_Asif_ 11-Sep-14 3:02am    
What value is set in Result for existing records?
Member 10021658 11-Sep-14 3:05am    
datatype is varchar(50) and Allow Nulls is checked
Try:
SQL
ALTER TABLE Student
ALTER COLUMN Result SET DEFAULT 'Pass'
 
Share this answer
 
Comments
Member 10021658 11-Sep-14 2:55am    
Thanks for reply Griff. Whenever I have tried this one ALTER TABLE Student ALTER COLUMN Result SET DEFAULT 'Pass', I got the error message like Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'SET'.
use this
SQL
ALTER TABLE Student 
ALTER COLUMN Result set default 'Pass'
-- you was missing column
 
Share this answer
 
v2
Comments
Member 10021658 11-Sep-14 3:01am    
I think it is working on empty table only.
Manikandan MAC 11-Sep-14 7:43am    
Its not working, shows error,

Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'set'.

I hope that Solution 1 & 3, will not work.

Solution 2 is working good,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900