Tuesday, September 8, 2015

Function for check table is exist or not in sql database

public int CheckTable(string TableName)
        {
            int i = 0;
            string cmdText = @"IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES
                       WHERE TABLE_NAME='" + TableName + "') SELECT 1 ELSE SELECT 0";
            i = (int)ExecuteScalar(cmdText,CommandType.Text,null,null);
            return i;
        }

Thursday, June 11, 2015

How to reset auto increment field starting from 0

DBCC CHECKIDENT (faq_sections, RESEED, 0)

Tuesday, May 12, 2015

How to Find Column From All Tables of Database

SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;

How to find a string from whole database?

DECLARE @MyValue NVarChar(4000) = 'something';

SELECT S.name SchemaName, T.name TableName
INTO #T
FROM sys.schemas S INNER JOIN
     sys.tables T ON S.schema_id = T.schema_id;

WHILE (EXISTS (SELECT * FROM #T)) BEGIN
  DECLARE @SQL NVarChar(4000) = 'SELECT * FROM $$TableName WHERE (0 = 1) ';
  DECLARE @TableName NVarChar(1000) = (
    SELECT TOP 1 SchemaName + '.' + TableName FROM #T
  );
  SELECT @SQL = REPLACE(@SQL, '$$TableName', @TableName);

  DECLARE @Cols NVarChar(4000) = '';

  SELECT
    @Cols = COALESCE(@Cols + 'OR CONVERT(NVarChar(4000), ', '') + C.name + ') = CONVERT(NVarChar(4000), ''$$MyValue'') '
  FROM sys.columns C
  WHERE C.object_id = OBJECT_ID(@TableName);

  SELECT @Cols = REPLACE(@Cols, '$$MyValue', @MyValue);
  SELECT @SQL = @SQL + @Cols;

  EXECUTE(@SQL);

  DELETE FROM #T
  WHERE SchemaName + '.' + TableName = @TableName;
END;

DROP TABLE #T;

Friday, May 1, 2015

How to get the row count for all tables in a SQL SERVER database

CREATE TABLE #counts
(
    table_name varchar(255),
    row_count int
)

EXEC sp_MSForEachTable @command1='INSERT #counts (table_name, row_count) SELECT ''?'', COUNT(*) FROM ?'
SELECT table_name, row_count FROM #counts ORDER BY table_name, row_count DESC

How to drop all the tables from any database ?

DECLARE @Sql NVARCHAR(500) DECLARE @Cursor CURSOR

SET @Cursor = CURSOR FAST_FORWARD FOR
SELECT DISTINCT sql = 'ALTER TABLE [' + tc2.TABLE_NAME + '] DROP [' + rc1.CONSTRAINT_NAME + ']'
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME

OPEN @Cursor FETCH NEXT FROM @Cursor INTO @Sql

WHILE (@@FETCH_STATUS = 0)
BEGIN
Exec SP_EXECUTESQL @Sql
FETCH NEXT FROM @Cursor INTO @Sql
END

CLOSE @Cursor DEALLOCATE @Cursor
GO

EXEC sp_MSForEachTable 'DROP TABLE ?'
GO

Tuesday, March 24, 2015

LINQ : Single, SingleOrDefault, First and FirstOrDefault

Single

Returns a single specific element from a collection of elements when element match found. An exception is thrown, when none or more than one match found for that element in the collection.

SingleOrDefault

Returns a single specific element from a collection of elements when element match found. An exception is thrown, when more than one match found for that element in the collection. A default value is returned, when no match is found for that element in the collection.

First

Returns first specific element from a collection of elements when one or more than one match found for that element. An exception is thrown, when no match is found for that element in the collection.

FirstOrDefault

Returns first specific element from a collection of elements when one or more than one match found for that element. A default value is returned, when no match is found for that element in the collection.

When to use Single, SingleOrDefault, First and FirstOrDefault


  1. If you want an exception to be thrown if the result set contains many records, use Single or SingleOrDefault.
  2. If you want a default value is returned if the result set contains no record, use SingleOrDefault.
  3. If you always want one record no matter what the result set contains, use First or FirstOrDefault.
  4. If you want a default value if the result set contains no record, use FirstOrDefault.

Perfomance of SingleOrDefault and FirstOrDefault

FirstOrDefault usually perform faster as compared SingleOrDefault, since it's iterate the collection until they find the first match. While SingleOrDefault iterate the whole collection to find one single match.

Tuesday, February 17, 2015

How to declare variable in php.

1. Start with a "$"
2. Followed by letter or underscore
3. Can contain letters, numbers, underscores or dashes
4. No spaces
5. Case sensitive

Example : Variable names

$item
$Item
$myVariable
$this_variable
$this-variable
$product1
$_book
$__bookPage

But, I recommend that do not use these from the above variable declaration
$this-variable,
$_book - Because php uses _ sign internally for many things
$__bookPage