SQL Server’s Database Mail has a stored procedure called sp_send_dbmail
that you can use to send emails from SQL Server.
You can use this stored procedure to send query results formatted into an HTML table.
Continue readingSQL Server’s Database Mail has a stored procedure called sp_send_dbmail
that you can use to send emails from SQL Server.
You can use this stored procedure to send query results formatted into an HTML table.
Continue readingWhen you use the sp_send_dbmail
stored procedure to email the results of a query, the column headers are included by default.
You can include or exclude the column headers with the @query_result_header
argument. To remove the column headers, use @query_result_header = 0
.
Error message 110 is a commonly encountered error in SQL Server when inserting data into a table. The full error looks like this:
Msg 110, Level 15, State 1, Line 1 There are fewer columns in the INSERT statement than values specified in the VALUES clause. The number of values in the VALUES clause must match the number of columns specified in the INSERT statement.
This happens when you specify less columns in the INSERT
statement than the number of values that you’re trying to insert with the VALUES
clause.
This will occur if you accidentally omit one or more columns from the INSERT
statement.
You’d get a similar (but technically different) error if you tried to do the opposite – specify more columns in the INSERT
statement than you try to insert.
In SQL Server, you can create DML triggers that execute code only when a specific column is updated.
The trigger still fires, but you can test whether or not a specific column was updated, and then run code only if that column was updated.
You can do this by using the UPDATE()
function inside your trigger. This function accepts the column name as its argument. It returns a boolean.
SQL Server has a database mail option that you can use to send mail from the database server.
For example, you can get notifications when SQL Server Agent jobs finish running or fail, or when there’s a high-severity error, etc.
Continue readingIf you find yourself needing to send an email automatically upon certain events occurring in SQL Server, you can do this via a trigger.
For example, you could automatically send an email when somebody deletes or updates a record from a table, etc.
To do this, you need to create a trigger that includes code for sending the email upon the required event.
Continue readingWhen you create a trigger in SQL Server, you have the option of firing it in conjunction with the triggering statement (i.e. the SQL statement that fired the trigger), or firing it instead of that statement.
To fire the trigger instead of the triggering statement, use INSTEAD OF
argument.
This is in contrast to using the FOR
or AFTER
arguments. When you use those arguments, the trigger fires only when all operations specified in the triggering SQL statement have launched successfully.
In SQL Server, you can use the CREATE TRIGGER
statement to create a trigger.
A trigger is a special type of stored procedure that automatically runs when an event occurs in the database server.
You can create a DML trigger, a DDL trigger, or a logon trigger.
This article provides an example of creating a DML trigger.
Continue readingSQL Server supports partitioned tables and indexes. When a partitioned table or index is partitioned, its data is divided into units that can be spread across more than one filegroup.
Therefore, to create a partitioned table in SQL Server, you first need to create the filegroup/s that will hold each partition. You also need to create a partition function and a partition scheme.
So it goes like this:
Below is an example of using these steps to create a table with four partitions.
Continue readingError message 213 is a common error that happens when you try to insert values into a table without explicitly specifying the column names.
The error looks like this:
Msg 213, Level 16, State 1, Line 1 Column name or number of supplied values does not match table definition.
It occurs when you specify the wrong number of values for that table. In other words, the number of values you provide doesn’t match the number of columns in the table.
Continue reading