Menu

#733 [MSSQL] Incorrect code generated tor adding default value to existing column.

4.7.4
closed-fixed
nobody
None
5
2025-02-22
2020-01-11
AJas
No

To add default value to column in MSSQL you have to use statement:

ALTER TABLE [Schema].[Table] ADD CONSTRAINT <name_of_default> DEFAULT "<defaul_value>" FOR [Column];

or you can omit "CONSTRAINT <name_of_default>" - than name of default will be generated automatically. </name_of_default>

Instead of that, Adminer is generating code:

ALTER TABLE [Table] ALTER COLUMN [Column] <type> DEFAULT '<defaul_value>';

There is also another problem. If you edit your table/column and you want to change two things at once - for instance you want to add default value and want to change column to be not nullable anymore, you getting one line of code :

ALTER TABLE [Table] ALTER COLUMN [Column] <type> <not/nullable> DEFAULT '<defaul_value>';

It can't work because one thing must be done by ALTER COLUMN, second must be done by ADD CONSTRAINT.

What else is important - any of these problems doesn't appear when you add columns. Adding columns works as it is.

Discussion

  • Jakub Vrána

    Jakub Vrána - 2021-02-06
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -16,6 +16,6 @@
     ~~~
     ALTER TABLE [Table] ALTER COLUMN [Column] &lt;type&gt; &lt;not/nullable&gt; DEFAULT &#39;&lt;defaul_value&gt;&#39;;
     ~~~
    -It can&#39;t work because one thing must be done by ALTER COLUMN, second must be done by ADD DEFAULT. 
    +It can&#39;t work because one thing must be done by ALTER COLUMN, second must be done by ADD CONSTRAINT. 
    
     What else is important - any of these problems doesn&#39;t appear when you add columns.  Adding columns works as it is. 
    
     
  • Jakub Vrána

    Jakub Vrána - 2025-02-22
    • status: open --> closed-fixed
     
  • Jakub Vrána

    Jakub Vrána - 2025-02-22

    Fixed by 8456779e.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.