T-SQL Tuesday #65: FMT_ONLY Replacements

So, it’s the second Tuesday of the month and you know what that means! That’s right; T-SQL Tuesday #65 is upon us. This month’s topic is “Teach Something New” and is brought to us by Mike Donnelly (B|T). This T-SQL Tuesday is uniquely special in that it falls during the new SQL blogger challenge started by Ed Leightn-Dick (B|T). Though this topic is straight-forward, it was by no means an easy decision. The beautiful thing about SQL Server is regardless of how long you have been working with it, there is always something new you can learn.

The first two arguments are fairly self-explanatory; the @tsql argument is where you specify the T-SQL statement, the @params argument is where you declare any parameters used in the statement, and the @include_browse_information or @browse_information_mode, depending on whether you are using the procedure or the DMV, is where you specify the depth of information you want returned. (I will use @include_browse_information from now on as they are interchangeable)

The @include_browse_information argument accepts three values: a value of zero will return basic information about the first result set including column ordinality, data type, length, etc.; a value of one returns everything that zero does along with base table information; and a value of two returns everything that the previous two values do along with any hidden rows used for computation. That’s not entirely correct. If the value of two is used and the query provided to the @tsql argument contains a view, then the view name will be returned instead of the base table information.

As you can see sp_describe_first_result_set and dm_exec_describe_first_result_set are very similar. The main difference between the two is that with the DMV, you can filter the results and specify the columns you want returned. This can provide greater flexibility in how you implement these.

The DMV dm_exec_describe_first_result_set_for_object is also similar to the procedure and DMV we just discussed. All three return the same result set, but dm_exec_describe_first_result_set_for_object takes two arguments, @object_id and @include_browse_information.

Transact-SQL

1

2

3

4

sys.dm_exec_describe_first_result_set_for_object

(@object_id,@include_browse_information)

However, unlike the previous DMV and procedure, this DMV can only be used with stored procedures and triggers otherwise an error will be returned.

Transact-SQL

1

2

3

4

5

6

7

8

9

10

11

12

13

14

USEAdventureWorks2012;

GO

SELECT*

FROMsys.[dm_exec_describe_first_result_set_for_object]

(OBJECT_ID('uspSearchCandidateResumes'),0);

GO

SELECT*

FROMsys.[dm_exec_describe_first_result_set_for_object]

(OBJECT_ID('uspSearchCandidateResumes'),1);

GO

The last procedure sp_describe_undeclared_parameters is, in my opinion, an aptly-named procedure. This procedure takes two arguments and returns metadata about any undeclared parameters.

Transact-SQL

1

2

3

4

5

sp_describe_undeclared_parameters

[@tsql=]'Transact-SQL_batch'

[,[@params=]N'parameters'datatype][,...n]

The @tsql argument is where you declare the parameterized tsql statement and the @params argument is where you declare the parameters in the statement. Any parameters in the @tsql argument that are not declared in the @params argument will have a row in the result set.

I have really enjoyed learning about these new DMVs and stored procedures, and I especially like that none these require any special privileges beyond the ability to execute the statement provided to the @tsql argument.