Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

SQL SERVER: Delete Duplicate Rows with Primary Id

Deletes duplicates (leaving one instance) where the table has a primary key. Good for tables with Id, DupColumn, DupColumn...

(This is MS-SQL specific)

DELETE
FROM 	TableName
WHERE 	Id NOT IN
	(SELECT 	MAX(Id)
        FROM   		TableName
        GROUP BY 	DuplicateColumName1, DuplicateColumName2)

Search for specific text in all stored procedures

declare @search varchar(50)
SET @search = 'searchterm'

SELECT    
     ROUTINE_NAME,
     ROUTINE_DEFINITION
FROM    
    INFORMATION_SCHEMA.ROUTINES
WHERE    
    ROUTINE_DEFINITION LIKE @search
ORDER BY
    ROUTINE_NAME

Query and join tables across server instances.

Use the following to query or join tables on remote servers. The query must be executed on a system running SQL Server, but the remote system can be pretty much anything an ODBC driver exists for:
select * from OpenDataSource(’SQLOLEDB‘,’Data Source=server.asdf.com;User ID=yourusername;Password=yourpassword‘).somedatabase.dbo.sometable;
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS