When working with SQL Server, if we’re given a number that represents a date in the yyyymmdd format, we can use functions like CAST()
or CONVERT()
to convert that number to a valid date type. This will enable us to perform date operations against it that we might not be able to do when it’s still in numeric form.
Tag: mssql
Fix “Cannot drop a default constraint by DROP DEFAULT statement. Use ALTER TABLE to drop a constraint default.” in SQL Server
If you’re getting an error that reads something like “Cannot drop a default constraint by DROP DEFAULT statement. Use ALTER TABLE to drop a constraint default“, it’s because you’re trying to use DROP DEFAULT
to drop a DEFAULT
constraint.
The DROP DEFAULT
statement has been flagged for deletion from SQL Server, and Microsoft recommends that we use the ALTER TABLE
statement to drop DEFAULT
constraints.
Therefore, to fix this issue, use the ALTER TABLE
statement to drop the DEFAULT
constraint.
How to Specify the Data Type when Creating a Sequence in SQL Server
When creating a sequence object in SQL Server, the default data type is bigint. However, we can change this so that the sequence uses a data type of our choosing, as long as it’s an integer type (see below for accepted data types).
We can set the data type of a sequence by using the AS
argument when defining the sequence.
Fix Msg 3728 in SQL Server “… is not a constraint”
If you’re getting an error that reads something like “Msg 3728, Level 16, State 1, Line 1
‘DF__Dogs__DogId__6FE99F9F’ is not a constraint“, it’s probably because you’re trying to drop a constraint that isn’t in the database.
To fix this issue, check to make sure the constraint exists before dropping it. Alternatively, use the IF EXISTS
clause to drop the constraint only if it exists.
How to Create a Repeating Sequence in SQL Server
When we create a sequence object in SQL Server, we have the option of making it a repeating sequence or a nonrepeating sequence. By repeating I mean, we can have the sequence continually start again once the min/max value has been reached. In other words, we can have the sequence reiterate over and over again.
We can do this with the CYCLE
argument.
How to Include Elements that Contain NULL Values When Using FOR XML in SQL Server
When using FOR XML
in SQL Server, we can use the ELEMENTS
directive to include a column as an element instead of an attribute. However by default, if a column contains a NULL value, no element is produced for that column in the resulting XML document. This may or may not be what we want, depending on the requirements.
If we want such columns to be included in the XML even when they contain NULL values, all we need to do is include the XSINIL
option. This option specifies that any column that has a NULL value automatically gets an element with xsi:nil="true"
in the resulting XML.
The alternative is ABSENT
, which means columns with NULL values are excluded (this is the default behaviour).
How to Include Elements that Contain NULL Values When Using FOR XML EXPLICIT in SQL Server
When using FOR XML EXPLICIT
in SQL Server, we can use the ELEMENT
directive to include a column as an element instead of an attribute. However, this directive doesn’t allow for NULL values. What I mean is that if a column contains a NULL value, no element is produced for that column in the resulting XML document. This may or may not be what we want, depending on the requirements.
If we want such columns to be included in the XML even when they contain NULL values, we can use the ELEMENTXSINIL
directive instead of ELEMENT
.
Convert a Number to a Date in SQL Server
Trying to convert a number to a date in SQL Server can be tricky and may not always work. It all depends on the number and what exactly it is that we’re trying to do. That said, here are some scenarios where we can convert a number to a date value.
Continue readingSQL Server FOR XML EXPLICIT Examples
In SQL Server, the FOR XML
clause allows us to return the results of a query as an XML document. Simply by placing the FOR XML
clause at the end of the query will output the results in XML.
When we do this, we have the option of specifying RAW
, AUTO
, EXPLICIT
, or PATH
mode. These modes allow us to shape the resulting XML in different ways, and so the mode we choose will determine how the XML is generated.
Below are examples of using EXPLICIT
mode when generating XML from a SQL query.
SQL Server FOR XML RAW Examples
In SQL Server, the FOR XML
clause allows us to return the results of a query as an XML document. Simply by placing the FOR XML
clause at the end of the query will output the results in XML.
When we do this, we have the option of specifying RAW
, AUTO
, EXPLICIT
, or PATH
mode. These modes allow us to shape the resulting XML in different ways, and so the mode we choose will determine how the XML is generated.
Below are examples of using RAW
mode when generating XML from a SQL query.