Posted on November 10, 2011, 1:20 pm, by Rhys, under
T-SQL.
I needed to generate a range of data about dates for a lookup table. There’s an elegant solution using a recursive cte that does the job; ?View Code TSQLWITH daysCte ( d ) AS ( SELECT CONVERT(DATETIME, ’1 January 2011′) AS d — starting date UNION ALL SELECT DATEADD(D, 1, d) FROM daysCte WHERE DATEPART(yyyy, d) [...]
I’ve been reading a bit about VLFs (Virtual Log Files) this week. I’ve found quite a few interesting links, especially this one, informing us that there’s such a thing as too few or too many VLFs. We can view details about VLFs using the DBCC LOGINFO TSQL command. This only works against the current database [...]
Posted on August 10, 2011, 1:27 pm, by Rhys, under
T-SQL.
This function is used to determine the current nest level or number of triggers that fired the current one. This could be used to prevent triggers from firing when fired by others. Here’s an example that does that; we have two tables with triggers, that fire AFTER INSERT, and insert into the other table. The [...]
Posted on August 8, 2011, 12:53 pm, by Rhys, under
T-SQL.
The CONNECTIONPROPERTY function can be used to obtain information about the current connection. The information available is similar to the sys.dm_exec_connections system view. ?View Code TSQLSELECT ‘Net Transport’ AS Property, CONNECTIONPROPERTY(’net_transport’) AS Value UNION ALL SELECT ‘Protocol type’, CONNECTIONPROPERTY(’protocol_type’) UNION ALL SELECT ‘Auth scheme’, CONNECTIONPROPERTY(’auth_scheme’) UNION ALL SELECT ‘Local net address’, CONNECTIONPROPERTY(’local_net_address’) UNION ALL SELECT [...]
Posted on August 3, 2011, 2:42 pm, by Rhys, under
T-SQL.
SQL Server has a bunch of encryption functionality at its disposal. The EncryptByPassphrase allows us to quickly encrypt data using a password. This function uses the Triple DES algorithm to protect data from prying eyes. To encrypt a section of text we supply a password and the text to the function; ?View Code TSQLSELECT ENCRYPTBYPASSPHRASE(’secret’, [...]
Posted on August 1, 2011, 2:28 pm, by Rhys, under
DBA,
T-SQL.
The PWDCOMPARE function is really handy for further securing your SQL Servers by checking for a range of blank or common passwords. If you google for common password list you’ll probably recognise several if you’ve been working in IT for any reasonable amount of time. Fortunately you can use this function, in conjunction with the [...]
Posted on July 28, 2011, 1:55 pm, by Rhys, under
T-SQL.
TSQL has a bunch of statistical system functions that can be used to return information about the system. This includes details about the number of connection attempts, cpu time, and total reads and writes and more. Many of these functions will be useful for performance monitoring. All of these functions return values that indicate cumulative [...]
Posted on July 27, 2011, 1:15 pm, by Rhys, under
T-SQL.
The NTILE is used to assigned records into the desired number of groups. NTILE assigns a number to each record indicating the group it belongs to. The number of records in each group will be the same if possible, otherwise some groups will have less than the others. This function may be useful for assigning [...]
Posted on July 25, 2011, 12:57 pm, by Rhys, under
T-SQL.
The GROUPING_ID function computes the level of grouping in a resultset. It can be used in the SELECT, HAVING or ORDER BY clauses when used along with GROUP BY. The expression used in this function must match what has been used in the GROUP BY clause. This function can be usefully deployed to order a [...]
Posted on July 20, 2011, 12:43 pm, by Rhys, under
T-SQL.
You can use the GROUPING function to indicate if a column in a resultset has been aggregated or not. A value of 1 will be returned if the result is aggregated, otherwise 0 is returned. It is best used to identify the additional rows returned when a query uses the ROLLUP, CUBE or GROUPING_SETS clauses. [...]