How to determine if a SQL temp table exists

by Andrew Jackson 12. September 2009 11:55
IF OBJECT_ID('tempdb..#some_temp_name') IS NOT NULL
PRINT '#some_temp_name exists.'
ELSE
PRINT '#some_temp_name does not exist.'

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

How to find out the name of the stored procedure that is currently executing

by Andrew Jackson 15. August 2009 11:50
The following SQL will report the name of the currently executing stored procedure

/* Begin */
PRINT ISNULL(OBJECT_NAME(@@PROCID), '<none>')
/* End */

To fetch this into a variable use :

/* Begin */
DECLARE @procedure_name VARCHAR(255)
SET @procedure_name = ISNULL(OBJECT_NAME(@@PROCID), '<none>')
/* End */

The key to this is @@PROCID, however, this returns NULL if not executing in a stored procedure, the ISNULL prevents a NULL value being used

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Get the time portion of a date in SQL

by Andrew Jackson 11. July 2009 12:23

select cast(convert(varchar, getdate(), 108) as datetime)

Note: Code 108 returns the time portion of a date in the format hh:mm:ss

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Fixing SQL orphaned users

by Andrew Jackson 7. July 2009 10:57

More a note to myself for this age old problem when restoring SQL databases.

Run this against the database you have the orphaned user in and it will fix the record in the master database.

Username/Password is for the user you need to fix.

EXEC sp_change_users_login 'Auto_Fix', 'username', NULL, 'password'

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

SQL

How to change SQL Order By clause based on a variable expression

by Andrew Jackson 5. July 2009 12:20
DECLARE @SearchType int
DECLARE @SearchText varchar(50)

SET @SearchType = 1
SET @SearchText = 'A'


SELECT
*
FROM
[TableNameGoesHere]
WHERE
(@SearchType=1 AND [Field1] LIKE @SearchText + '%')
OR
(@SearchType=2 AND [Field2] LIKE @SearchText + '%')
ORDER BY
CASE @SearchType
WHEN 1 THEN [Field1]
WHEN 2 THEN [Field2]
END

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Returning SQL Results in a random order

by Andrew Jackson 5. July 2006 12:33
Bit of an un-usual one but if you want to return a result set in a random order then you can use this;

SELECT *
FROM table
ORDER BY NEWID()

NEWID creates a unique value every time it's executed, the values are non-consecutive.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

How to merge a field from multiple rows into one concatenated string

by Andrew Jackson 17. June 2006 16:31
Where you have multiple rows that you need to either display or report on as one single, concatenated field you can use the Coalesce function to merge them.

You can either do this in a stored procedure or create a function so it can be used inline in Select statements.

Below is an example of a function that will merge multiple policy numbers into one string containing them separated by commas.

CREATE FUNCTION dbo.fn_MergePolicyNums
(@HGNum as int)
RETURNS varchar(100) AS

BEGIN

declare @PolNums varchar(100) /* Holds multiple MEC numbers */

SELECT @PolNums = COALESCE (@PolNums + ', ' + PolicyNum, PolicyNum, @PolNums) FROM tblPolicy WHERE HGNum = @HGNum

RETURN ISNULL(@PolNums, '')
END

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Deleting Duplicate Record Entries in SQL

by Andrew Jackson 10. July 2005 12:40
This article describes how to delete a single row, when it is found that row is duplicated

Set Rowcount 1
Delete From
Table
Where
ID = 999 (Or any field name/value combination that identifies the duplicate row)
Set Rowcount 0

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Finding Duplicate Record Entries in SQL

by Andrew Jackson 10. July 2005 12:35
This article describes how to identify duplicate records in a SQL table.

Use the Group By / Having combination in a Select statement

Select
ID
From
Table
Group By
ID
Having
Count(ID) > 1

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Development | SQL

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen