Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WITH VALUES TSQL

I have been trying to understand what the WITH VALUES statement does?

I cant seem to find any documentation that explains it properly.

ALTER TABLE Table1  ADD newGuidId UniqueIdentifier NULL CONSTRAINT DF_Guid Default newid() with values 
like image 577
TheWommies Avatar asked Sep 08 '13 06:09

TheWommies


People also ask

What is the with clause in SQL?

The WITH clause in SQL was introduced in standard SQL to simplify complex long queries, especially those with JOINs and subqueries. Often interchangeably called CTE or subquery refactoring, a WITH clause defines a temporary data set whose output is available to be referenced in subsequent queries.

What is use of && in SQL?

Returns true if both expressions are true ; otherwise, false or NULL .

What does .value do in SQL?

The VALUE function returns the value of the first non-null expression. The schema is SYSIBM. The VALUE function is identical to the COALESCE function. COALESCE should be used for conformance to SQL standard.


2 Answers

When you add a nullable column with a default constraint to a table, then all existing rows will get the new column with a NULL as its value. The defined default values will only be applied to new rows being inserted (if they don't have a value for that column in their INSERT statement).

When you specify WITH VALUES, then all existing rows will get that defined default value instead of NULL

If the column you're adding to your new table is non-nullable and has a default constraint, then that default value is applied to all existing rows in the table automatically (no need for WITH VALUES because the column must have a value other than NULL)

like image 81
marc_s Avatar answered Sep 26 '22 00:09

marc_s


WITH VALUES applies default values to the null fields

http://technet.microsoft.com/en-us/library/ms187742.aspx and a short blog post about it.

like image 36
zedfoxus Avatar answered Sep 26 '22 00:09

zedfoxus