Posts Tagged ‘t-sql’

Removing < CR >< LF > During a Query

Tuesday, March 22nd, 2011

As you may have noticed, or not noticed, I have not done anything blog-worthy in quite a while. Well, actually that is not true, I have done a few things in Windows PowerShell that I think are kinda cool but the blog post is still swimming around in my mind and I have not sat down at typed it out, yet. But anyway, I did something this morning that I bet will pop up again and I think I should put it somewhere what I can find it later. So you get to hear about it also. I was building a fairly simple SSIS package. Select data from a database and write it to a text file. They don’t get much easier in my experience. I was using < CR >< LF > as my new row indicator. That seemed to work and I was going on to my next project. The customer called me. It seems that embedded in two of the columns (a text field) the users could and did use < CR >< LF > to format the text in the field. Well that was really messing with my extract. Never fear, dear readers, there is a solution. My good friend Mr. Google (or maybe Mr. Bing) let me know that char(13) was a carriage return and char(10) was a line feed. Well that made it easy. I edited my select statement to look something like this:

SELECT dbo.table.column1
,dbo.table.column2
,replace(replace(dbo.table.column3,char(13),’’),char(10),’’) as ‘column3’
,replace(replace(dbo.table.column4,char(13),’’),char(10),’’) as ‘column4’
FROM dbo.table
ORDER BY dbo.table.column1

I no longer had < CR >< LF > inside the column impacting my extract. Everyone was happy.

Restoring Last Full Backup

Tuesday, August 31st, 2010

I ran into a small problem where I needed to automate a restore of my last full backup to a different database.  Since my full backup file names include a date/time stamp I needed to query msdb for the backup name.  Since I figured it out, I thought I’d post it so I would know where of find it the next time I need it, and maybe help someone else out in the process.

declare @bksource varchar(100)
select @bksource=(
select a.physical_device_name
from [msdb].[dbo].[backupmediafamily] a join [msdb].[dbo].[backupset] b on b.media_set_id = a.media_set_id
where b.backup_start_date =
(
  select max( backup_start_date ) from [msdb].[dbo].[backupset]
   where database_name = b.database_name and type = ‘D’
)
and database_name = ‘<database_name>’
)

– print @bksource

restore database [<Database_Name2>] from disk = @bksource with replace, stats = 10,
MOVE ‘<Database_Name>’ TO ‘i:\mssql\data\test\<Database_Name2>.MDF’,
MOVE ‘<Database_Name>_log’ TO ‘j:\mssql\xlog\test\<Database_Name2>_log.LDF’
GO

Replace anything inside the <> with the proper names and set the correct drive letters.

Find locked SQL accounts on SQL 2005

Wednesday, September 16th, 2009

We are in the process of upgrading a Microsoft product that just hasn’t gotten around to integrating with AD and uses SQL accounts.  It also has a really bad habit of locking SQL accounts if your AD policy for failed login attempts is set low.  So I got tired of searching through SSMS to unlock accounts, so I wrote a quick script to list the locked accounts and the command to unlock it.  I didn’t go as far as to auto-unlock the locked accounts but that would be a simple edit.  Here’s the script.

Declare @UserID varchar(25), @status nvarchar(5), @Command varchar(255), @command1 varchar(255)
Declare Account_cursor Cursor
for select name from sys.syslogins where name not like ‘<DOMAIN>\%’
open Account_cursor
FETCH NEXT FROM Account_cursor
INTO @UserID
WHILE @@FETCH_STATUS = 0
BEGIN
select @status= convert(nvarchar,(loginproperty(@userID,N’IsLocked’)))
if @status = ’1′
BEGIN
print ‘– ‘ + @userID + ‘ is locked’
select @command = ‘ALTER LOGIN [' + @userID + '] WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=OFF, NO CREDENTIAL’
select @command1 = ‘ALTER LOGIN [' + @userID + '] WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[us_english], CHECK_EXPIRATION=OFF, CHECK_POLICY=ON, NO CREDENTIAL’
print @command
print @command1
– exec (@command)
– exec (@command1)
END
fetch next from Account_Cursor into @userID
END
Close Account_cursor
Deallocate Account_cursor

Thank you @unclebiguns for pointing me toward the function that makes this all possible.

I am guessing that this is not the easiest or most efficient way to do this, so if anyone would like to educate me on a better way, I am very open to learning your techniques.

Find all Triggers

Tuesday, August 18th, 2009

I was asked to find all tables in a database that had a DML trigger so a developer could ensure that all the old functionality was available in the new database.  So once I built my query to give me the information, I thought I’d save it here for future use.

select a.name as ‘Trigger’, b.name as ‘Table’
from sysobjects a
join sysobjects b
on a.parent_obj=b.id
where a.xtype = ‘TR’

This gave me a nice little result set of each trigger and what table it was associated.  Maybe I won’t have to re-invent the wheel the next time they ask.

Stored Procedure with Execute as

Tuesday, June 2nd, 2009

We have a database that holds information that is fairly personal, so much that the application team that supports it may only have read access to the database.  There came a need to occasionally lock all the administrators out of the system for updates.  These updates will occur every few months.  So I created a SQL user and gave that user select and update rights to the necessary table.  Then I created a couple of stored procs, one to disable the logins and one to enable the logins.

CREATE PROCEDURE [dbo].[LockOutAdministrators]
WITH EXECUTE AS ‘EXECUTIONER’
AS
BEGIN
BLAH, BLAH, BLAH
END

I then granted execute to the stored procedures to the application group.  So the application team can only update the table via the stored procedure that does only what it needs to do.  They are happy they have an easy way to do this without me becoming the bottle neck.  If this was a more sensitive system, I’d probably add some auditing, but it is not necessary at this time.

Cleaning up the disk

Monday, April 6th, 2009

When I became our companies MSSQL DBA I inherited an smooth running machine.  My predecessors were extremely talented and their scripts did everything we needed done.  But I can’t just live off their hard work, I need to leave my mark on the environment.  So I am tweaking some of their scripts to get rid of what annoys me.  The current task is old backup files.  The scripts they wrote do this awesome thing that archives a backup if the archive bit has not been cleared.  This is so cool.  If NetBackup has a problem, the old backup file that hasn’t gone to tape/disk is moved to an archive folder and then the new backup file is created.  But what happens when after the file is backed up and the archive bit is cleared.  It just sits on the disk and is backed up again with every full backup that occurs.  That makes the backups bigger and take longer.  So I added a job that deletes all *.bak files that have their archive bit cleared.  That was so much easier than I expected it to be.

exec xp_cmdshell ‘del d:\mssql\backup\*.bak /A:-A /S /F’

I run this command 15 minutes before I do the nightly backup.  It frees up space so I get less alerts of full volumes.  It decreases the NetBackUp full backup time and more backups can happen within their windows.  Unfortunately the DBAs before me decided that transaction log backups and database backups should have an extension of .bak.  Since I want to treat transaction logs differently I have to go back and change the backup extension.  So I’m slowly working through the databases to make that change.  So once that is done, I can start cleaning up my backup volumes.

Find the Windows Server Name

Thursday, February 19th, 2009

I was working with SQL2000 on a windows cluster.  I needed to write the results of a query to a test file on the local drive.  I was using @@servername to get the server name to write the file.  Unfortunately that appends the instance name and gave me a problem.  So I went looking for how to remove the instance name and I found a pretty neat trick.

I ran the command: select @@servername and received ‘server\instance’

I ran the command: select @@servicename and received ‘instance’

So I thought there must be a way to subtract @@servicename from @@server name, then I tried this:

select replace(@@servername,’\’ + @@servicename,”)

My result was: ‘server’

That I can work with.

Finding databases that have changed to Simple Recovery Mode

Friday, January 2nd, 2009

As I am beginning our journey into SQL 2008, I am taking this time to go through all our scripts and try to resolve some of the issues I get alerted on that are due to how we do things.  One of the things we do is programatically list all the databases, determine which are prod, test, & system.  Then the Prod databases have xlog backups and the others do not.  Occasionally, some of our prod databases will change from Full Recovery Mode to Simple Recovery Mode.  How I find out about this is that the Xlog backup job will fail.  The first database that should have been in Full but was in Simple would fail the whole job and transaction logs would not be backed up.  Failing jobs is a horrible way to find out that a recovery mode changed.  So I re-wrote our backup job to query sys.databases and do xlog backups to any database in full recovery mode.  No more xlog failures for wrong setting.  Life is good.  But wait, what if a database does change modes how will I know?  Sure the xlogs of databases that are in full recovery mode backed up, but that may not be good enough.  So I sat back down and started wrote an new script.  This script looks at yesterday’s list of Simple Recovery Mode servers and compares it to Today’s list.  If there is a new database in Simple recovery mode it sends me an e-mail to investigate it.  If there is no change, it just ends.  Here’s the code.

declare @NewSimple int, @subject varchar(256)

select name into maintenance..backupsimple from sys.databases where recovery_model = 3

SELECT name into ##backupchange
FROM
(
SELECT name
FROM maintenance..backupsimple
EXCEPT
SELECT name
FROM maintenance..backupsimpleold
) tmp
GROUP BY name
HAVING COUNT(*) = 1
ORDER BY name

select @NewSimple = COUNT(name) from ##backupchange
if @NewSimple >0
BEGIN
select @subject = ‘New Simple Mode Databases on server ‘ + replace(@@servername, ‘\’, ‘_’)
exec msdb..sp_send_dbmail
@profile_name = ‘SQL Server’,
@recipients = ‘<My E-mail address>’,
@importance   = N’HIGH’,
@subject = @subject,
@body = ‘Please verify the following databases should be in Simple Recovery Mode.  They were not in Simple Recovery mode yesterday.  Remember to perform a full backup if you change the model to full recovery mode:’,
@query = ‘select name from ##backupchange’
END
drop table maintenance..backupsimpleold
select name into maintenance..backupsimpleold from maintenance..backupsimple
drop table maintenance..backupsimple
drop table ##backupchange

I have this running once a day for now.  Maybe in the future it will need to run more often.

Getdate() Formatting

Tuesday, December 30th, 2008

I get the occasional question from our Oracle DBAs on how do you do this in MSSQL.  Usually I have little to no idea, so I do what all new DBAs do:  Look it up.  Well, this situation came up today on how can I write a procedure that will query the table and give me all the enteries for today.  The column the date is stored in is varchar in this format 11/14/2008.  I found the answer is select convert(varchar,getdate(),101)  So I thought I’d add the sytle ID numbers for future reference.

Style ID

Style Type

0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yyyy
102 yyyy.mm.dd
103 dd/mm/yyyy
104 dd.mm.yyyy
105 dd-mm-yyyy
106 dd mon yyyy
107 Mon dd, yyyy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yyyy
111 yyyy/mm/dd
112 yyyymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131 dd/mm/yyyy hh:mi:ss:mmmAM