4 Ways to Find Out What Columns will be Returned by a Stored Procedure in SQL Server

If you ever find yourself in the situation where you’re about to execute a stored procedure, but you suddenly catch yourself. You wonder “How many columns does this thing return? Which tables? Does it query a remote server?”

The good news is there are several ways to get this information before you run the query. Let’s look at them.

Continue reading

ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator (SQL Server)

If you’re running a query in SQL Server, and you get the following error…

Msg 104, Level 16, State 1, Line 8
ORDER BY items must appear in the select list if the statement contains a UNION, INTERSECT or EXCEPT operator.

…you should check your SQL statement – you’ve probably omitted a column from your SELECT list.

As the error message implies, you’ll probably only see this error if you’re running a query that contains a UNION, INTERSECT or EXCEPT operator.

Simply adding the column to your SELECT list should fix the problem.

Continue reading

List of Date Formats Available with CONVERT() in SQL Server

The following table contains a list of the date formats that you can provide to the CONVERT() function when you convert a date/time value to a string.

These formats are provided as an optional third argument when calling the CONVERT() function. They’re provided as an integer expression that specifies how the CONVERT() function will format the date. 

Continue reading

Create a SQL Server Agent Schedule with T-SQL

When using T-SQL, you can use the sp_add_schedule stored procedure to add a SQL Server Agent schedule.

This procedure creates the schedule, but it doesn’t attach it to any jobs. To attach it to a job, you must use the sp_attach_schedule procedure.

You can also use sp_add_jobschedule to create the schedule and attach it all with the same procedure.

Continue reading

3 Ways to Return a List of SQL Server Agent Jobs (T-SQL)

Often the quickest way to get a list of SQL Server Agent jobs is to simply expand the SQL Server Agent node in the SSMS Object Explorer.

But that’s not the only way. And depending on your environment, it might not even be an option. If you don’t have SSMS or a database tool that allows you to see the SQL Server Agent jobs, you may need to use T-SQL instead.

In this article, I present three options for returning a list of SQL Server Agent jobs when using T-SQL.

Continue reading