SQL Server - Move TempDb Log File

by Administrator 21. January 2010 23:53

ALTER DATABASE TempDB MODIFY FILE
(NAME = templog, FILENAME = 't:\templog.ldf')

Just change the logical name of your log file - and the new physical file location

Note that you need to restart SQL Server for this to take effect

 

SQL Server - Add multiple email addresses for an operator

by Administrator 10. November 2009 00:00

SQLServer Agent Jobs can only have one operator defined under the notifications section so what do you do if you want to send notification emails to more than one person? You could set up a distribution list within your email system but a simpler approach is simply to semi-colon separate the list of email addresses. 

Its as simple as that ! Just make sure you have enabled your alerting for the SQL Server Agent and configured database correctly and away you go

 

 

Attach Database Error Unable to open the physical file Operating system error

by Administrator 2. November 2009 23:56

This one caused a bit of a panic I can tell you! We had major trouble reattaching a very large database file. We kept getting the error below (Running SQL 2008 on Windows 7):

Unable to open the physical file Operating system error 5: "5(failed to retrieve text for this error. Reason: 15105)". (Microsoft SQL Server, Error: 5

This occured both when using the Management Console and if running the stored procedures 'sp_attach_db' and 'sp_attach_single_file_db'

Solution

Not sure if there is a 'better' way of doing this but we found that connecting using 'sa' and attaching the database as normal solved the problem

 

An explicit value for the identity column in table '' can only be specified when a column list is used and IDENTITY_INSERT is ON

by Administrator 23. October 2009 20:06

Easy one this, if you're trying to insert rows with specific values to go into an identity column (e.g. when transferring data from one database to another) you need to use the following kind of statement

SET IDENTITY_INSERT tablename ON

 

INSERT INTO tablename (column1, column2)

SELECT column1, column2 FROM table2

 

SET IDENTITY_INSERT tablename OFF


SQL Server - Raiserror Syntax

by Administrator 14. October 2009 19:44

-- RAISERROR with severity 11-19 will cause execution to 
-- jump to the CATCH block.

RAISERROR ('Error raised in TRY block.', -- Message text.


               16, -- Severity.


               1 -- State.


               );

Tags:

SQL Server - Try Catch Statement Syntax

by Administrator 8. October 2009 21:39

I'm sure there are more complexities to this - but this seems to work well in most applications

BEGIN TRY


 BEGIN TRANSACTION
 
 -- Do whatever you need to do
 

 COMMIT TRANSACTION


END TRY


BEGIN CATCH


 -- Rollback if in a transaction


 IF @@TRANCOUNT > 0 ROLLBACK TRANSACTION
 
 -- Do whatever error logging you need to do


END CATCH

 

SQL Server - Send Email Using Database Mail From TSQL or Stored Procedure

by Administrator 8. October 2009 18:28

Pretty simple stuff. This assumes that a valid database account etc. is set up and Database Mail is configured correctly

EXECUTE msdb.dbo.sp_send_dbmail @recipients='to@xxx.com',
@subject = 'Test email',
@body = 'Test email body',
@reply_to='from@yyy.com'

VB.NET - Programmatically add items to context menu

by Administrator 8. October 2009 00:11

First you need an event handler

Private Sub ContextMenuHandler_Click(ByVal sender As Object, ByVal e As System.EventArgs)

 

' Do something when the user clicks on a node

' Use the sender.tag to determine which menu item was clicked

 

End Sub

Then you can add the menu items dynamically

' This could be a strip added via form designer

Dim oContextMenu As New ContextMenuStrip

' Add a toolstrip Item

Dim oNewToolStripItem As New ToolStripMenuItem

oNewToolStripItem.Text = "Parent"

oNewToolStripItem.Tag = "1"

AddHandler oNewToolStripItem.Click, AddressOf ContextMenuHandler_Click

oContextMenu.Items.Add(oNewToolStripItem)

 

' Add a couple of children

Dim oNewToolStripItem2 As New ToolStripMenuItem

oNewToolStripItem2.Text = "Child 1"

oNewToolStripItem2.Tag = "2"

AddHandler oNewToolStripItem2.Click, AddressOf ContextMenuHandler_Click

oNewToolStripItem.DropDownItems.Add(oNewToolStripItem2)

 

Dim oNewToolStripItem3 As New ToolStripMenuItem

oNewToolStripItem3.Text = "Child 2"

oNewToolStripItem3.Tag = "3"

AddHandler oNewToolStripItem3.Click, AddressOf ContextMenuHandler_Click

oNewToolStripItem.DropDownItems.Add(oNewToolStripItem3)

SQL Server - Check if variable or column is numeric

by Administrator 7. October 2009 18:39

The simplest way to check if a value is numeric is to use the 'ISNUMERIC' function

-- This returns 1

select ISNUMERIC('1.1')

-- This returns 1

select ISNUMERIC('12')

-- This returns 0

select ISNUMERIC('aaaa')

SQL Server - Force Database Log File Shrink

by Administrator 6. October 2009 20:51

 

USE DATABASENAME
GO
DBCC SHRINKFILE(LOGNAME, 1)
BACKUP LOG DATABASENAME WITH TRUNCATE_ONLY
DBCC SHRINKFILE(LOGNAME, 1)


Buy Me a Beer ?

If you've found this site useful, you could help support its running costs by either checking out the adverts on the page if you find anything that interests you - or by making a small donation