Saturday, October 31, 2009

Search string in any column in a table

Step 1.
USE digitallearning
GO
CREATE PROCEDURE sp_FindStringInTable @stringToFind VARCHAR(100), @schema sysname, @table sysname
AS
DECLARE @sqlCommand VARCHAR(8000)
DECLARE @where VARCHAR(8000)
DECLARE @columnName sysname
DECLARE @cursor VARCHAR(8000)
BEGIN TRY
SET @sqlCommand = 'SELECT * FROM ' + @schema + '.' + @table + ' WHERE'
SET @where = ''
SET @cursor = 'DECLARE col_cursor CURSOR FOR SELECT COLUMN_NAME
FROM ' + DB_NAME() + '.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = ''' + @schema + '''
AND TABLE_NAME = ''' + @table + '''
AND DATA_TYPE IN (''char'',''nchar'',''ntext'',''nvarchar'',''text'',''varchar'')'
EXEC (@cursor)
OPEN col_cursor
FETCH NEXT FROM col_cursor INTO @columnName
WHILE @@FETCH_STATUS = 0
BEGIN
IF @where <> ''
SET @where = @where + ' OR'
SET @where = @where + ' ' + @columnName + ' LIKE ''' + @stringToFind + ''''
FETCH NEXT FROM col_cursor INTO @columnName
END
CLOSE col_cursor
DEALLOCATE col_cursor
SET @sqlCommand = @sqlCommand + @where
--PRINT @sqlCommand
EXEC (@sqlCommand)
END TRY
BEGIN CATCH
PRINT 'There was an error'
IF CURSOR_STATUS('variable', 'col_cursor') <> -3
BEGIN
CLOSE col_cursor
DEALLOCATE col_cursor
END
END CATCH


Step 2.
Query to search the string "script src" from tbl_Article with owner DBO.

USE digitallearning
GO
EXEC sp_FindStringInTable '%script src%', 'dbo', 'tbl_Article'

Friday, October 9, 2009

Counting rows in a table for improving performance of the query

Below is the query for counting rows in a table for improving performance of the query.
SELECT rows FROM sysindexes WHERE id = OBJECT_ID('tbl_xyz') AND indid less than 2

Wednesday, October 7, 2009

Searching in a column of any table by a substring

Example : Searching 'Independence day' text in column name leave_desc for 'epen' substring.

SELECT leave_desc FROM tbl_calendar WHERE substring(leave_desc, 4, 4) = 'epen'

Select desired rows from table by IN Keyword

Below is the query for selecting any records/rows from the table.

select * from tbl_logo_Info where ImageID IN(4,6,10) order by image_Title asc

Select top two rows from SQL server database table

Below is the query for top two records.

Select top 2 * from tbl_news order by newid()

Insert rows/records from one table to another table

Below is the query to insert data from one table to another one.

insert into tbl_logo_Info_event (ImageURL_link,ImageURL_Show, Image_Name,Image_Title, Image_Profile) select ImageURL_link,ImageURL_Show,Image_Name,Image_Title,Image_Profile from tbl_logo_Info where ImageID='16'

How to select Duplicate Rows from a table

Below is the query to select duplicate rows from a table.
select email,count(*) from database_Amit group by email having count(*)>1

Tuesday, October 6, 2009

Create Serial Number Column using SQL Server

There is a common requirement to show records with serial number in Grid. The common scenario is, we can generate serial number using variable which is incremented each time. The another approach is to generate this column in SQL itself. Yes you can do that..! Here is a Query for that.
SELECT ROW_NUMBER() OVER (ORDER BY ColumnName1) As SrNo, ColumnName1, ColumnName2 FROM TableName