Thursday, November 11, 2010

How to find SQL Server object

If you need to find, if so and so object (stored procedure, table, function) exists in the database, following queries could be of help to you :

SELECT * FROM SYSOBJECTS WHERE NAME LIKE '%ObjectName%'

If you know the type of object, even better :
SELECT * FROM SYSOBJECTS WHERE XTYPE = 'U' AND NAME LIKE '%ObjectName%'
-- 'U' denotes a User Table

If you need to find out if the object exists in any of the databases on the system :

EXEC sp_MSforeachdb 'SELECT * FROM ?..SYSOBJECTS WHERE XTYPE= ''U'' AND NAME LIKE '%ObjectName%'

Looping through comma-separated string in SQL Server

Ever needed a snippet to loop through the comma separated string in SQL Server?

Here is one :

DECLARE @tString AS NVARCHAR(MAX)
SET @
IF SUBSTRING(@tString, LEN(@tString)-1,1)<>','
SET @tString = @tString + ',' --add a comma to the end if it isn't there

SET @nCount = 0

WHILE CHARINDEX(',',@tString) > 0
BEGIN
SET @tSeparatedString = SUBSTRING(@tString,0, CHARINDEX(',',@tString))

SET @tString = SUBSTRING(@tString, CHARINDEX(',',@tString)+1, LEN(@tString) - @nCount) --remove the first item from the list

IF(@tSeparatedString IS NOT NULL AND @tSeparatedString <> '')
BEGIN
-- DO whatever with that separated string
END

Check 1 2 3

Looking for a solution to the current problem that you're facing in coding?
Be it C#, ASP .NET, SQL Server, Visual Studio tips....

Don't Google right away... You may find it here more easily...

We're planning to develop this blog as a repository for all the research we do over internet day in and out.

Thank you :)

We , a team at Sumeru (http://www.sumeru.com)