Posted on July 18, 2011, 12:15 pm, by Rhys, under
T-SQL.
The APP_NAME function returns the name of the application for the current database connection if the application has set it. Run the following in SSMS; ?View Code TSQLSELECT APP_NAME(); This will return; Microsoft SQL Server Management Studio – Query There’s no method of setting this value within TSQL. To set this value you need to [...]
Posted on July 4, 2011, 2:54 pm, by Rhys, under
DBA,
T-SQL.
I’m moving the backup jobs we run onto specific users and need to assign the db_backupoperator role to the user for each database. Very tedious to do in SSMS so here’s a quick script I knocked up. The script will assign a specific database role to all databases on a SQL Server. The user will [...]
Posted on May 9, 2011, 9:33 pm, by Rhys, under
T-SQL.
A while ago Thomas LaRock (blog | twitter) posted a script that used sysusers and the sp_helpusers proc to audit user groups setup in your database. The original post is here. I’m busy documenting my environment and thought this would be a great addition to the info I collect. The only issue I had with [...]
Posted on March 23, 2011, 5:11 pm, by Rhys, under
DBA,
T-SQL.
We’re currently busy reviewing some of the historical database design decisions taken in our organisation. We’ve noticed quite a lot of columns, that are specified as nullable, but do not actually contain any nulls. Obviously this fact makes the column a possible candidate for changing to NOT NULL. I wanted to make this task a [...]
Posted on March 20, 2011, 4:46 pm, by Rhys, under
DBA,
T-SQL.
In order to automate testing of backups it’s useful to be able to query backup files to access various bits of meta-data. We can do this with the RESTORE FILELISTONLY TSQL command. In the simplest format the command is as follows; ?View Code TSQLRESTORE FILELISTONLY FROM DISK = ‘c:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\Backup\AdventureWorks_20110320.bak’; This doesn’t help [...]
Posted on March 1, 2011, 9:14 pm, by Rhys, under
T-SQL.
I was Googling around the other day for a bit of TSQL to quickly grant a user permission to use all procedures and functions and came across the following post. We have a few table -valued functions in our system so this script buckled with the following error; Msg 4606, Level 16, State 1, Line [...]
Posted on October 29, 2010, 11:47 am, by Rhys, under
T-SQL.
I’ve been spending the past week or so changing a colleagues scripts into SSRS reports so we can automate things a bit better. During some QA checks I noticed that I was coming out with higher counts on one section of the report. Much to my frustration the two queries looked identical and I had [...]
Posted on September 23, 2010, 10:22 pm, by Rhys, under
DBA,
T-SQL.
I’ve noticed people before struggling using sp_rename with tables that aren’t in the default schema. Many people don’t use schemas, so there’s often confusion, when they finally do come across the need to rename a table belonging to another schema. Assuming the below ‘Suppliers’ table is in the users default schema (usually dbo) then the [...]
Posted on September 1, 2010, 6:02 pm, by Rhys, under
T-SQL.
I was recently given a du-duping task which was much more difficult than I anticipated and taxed my SQL brain to its limits. I thought of using a CTE to do this but all of the examples I could find for deleting records with a CTE wouldn’t have worked in my situation. Essentially the table [...]
Posted on August 2, 2010, 9:38 pm, by Rhys, under
T-SQL.
Today a colleague was running into some difficulties matching football team names containing accented characters. For example Olympique Alès and Olympique Ales were not matching when he wanted them to. The issue here is all to do with collations. We can use the Latin1_General_CI_AI collation in our queries to force the comparison to ignore accents [...]