About 298,000 results
Open links in new tab
  1. Add column to SQL Server - Stack Overflow

    Dec 19, 2022 · ALTER TABLE YourTable ADD Bar INT NOT NULL DEFAULT(0) /*Adds a new int column existing rows will be given the value zero*/ In SQL Server 2008 the first one is a metadata only change. The second will update all rows.

  2. How to add a column with a default value to an existing table in …

    Sep 18, 2008 · Here is another way to add a column to an existing database table with a default value. A much more thorough SQL script to add a column with a default value is below including checking if the column exists before adding it also checkin the constraint and dropping it …

  3. sql server - Altering SQL table to add column - Stack Overflow

    ALTER TABLE [dbo].[Case] ADD CaseName NVARCHAR(50) Better yet, you can check for the existance of the column first: if not exists (SELECT 1 FROM sysobjects INNER JOIN syscolumns ON sysobjects.id = syscolumns.id WHERE sysobjects.name = N'Case' AND syscolumns.name = N'CaseName') ALTER TABLE [dbo].[Case] ADD CaseName NVARCHAR(50)

  4. insert a NOT NULL column to an existing table - Stack Overflow

    Jan 4, 2013 · As an option you can initially create Null-able column, then update your table column with valid not null values and finally ALTER column to set NOT NULL constraint: ALTER TABLE MY_TABLE ADD STAGE INT NULL GO UPDATE MY_TABLE SET <a valid not null values for your column> GO ALTER TABLE MY_TABLE ALTER COLUMN STAGE INT NOT …

  5. Add a column to existing table and uniquely number them on MS …

    Sep 20, 2008 · Depends on the database as each database has a different way to add sequence numbers. I would alter the table to add the column then write a db script in groovy/python/etc to read in the data and update the id with a sequence. Once the data has been set, I would add a sequence to the table that starts after the top number.

  6. sql server - Add a column to a table, if it does not already exist ...

    Jan 15, 2012 · But the question is actually different and other solutions could be available (e.g. if SQL adds an IF NOT EXISTS clause to the ADD COLUMN syntax) – Brondahl Commented Sep 1, 2020 at 14:53

  7. SQL Query to add a new column after an existing column in SQL …

    Jul 25, 2018 · First add the new column to the old table through SSMStudio. Go to the database >> table >> columns. Right click on columns and choose new column. Follow the wizard. Then create the new table with the columns ordered as desired as follows: select * into my_new_table from ( select old_col1, my_new_col, old_col2, old_col3 from my_old_table ) as A ;

  8. SQL Server - Can you add field descriptions in CREATE TABLE?

    I don't believe the Create Table T-SQL statement supports this. However, if you are defining your tables via SSMS, you can easily enter table level and column level comments at the same time you create your table.

  9. sql - Adding an identity to an existing column - Stack Overflow

    Aug 23, 2011 · The new created column name is UserID. ALTER TABLE Users ADD UserID INT NOT NULL PRIMARY KEY IDENTITY(1,1) then Drop the Renamed Column. ALTER TABLE Table_Name DROP COLUMN Renamed_ColumnName Example for Drop renamed column. ALTER TABLE Users DROP COLUMN OldUserID Now we've adding a primarykey and …

  10. How to add a boolean datatype column to an existing table in sql?

    Oct 25, 2016 · ALTER TABLE person add [AdminApproved] BIT default 'FALSE'; Also there are other mistakes in your query. When you alter a table to add column no need to mention column keyword in alter statement . For adding default constraint no need to use SET keyword. Default value for a BIT column can be ('TRUE' or '1') / ('FALSE' or 0).

Refresh