Search This Blog

Tuesday, April 2, 2013

DROP TABLE SQL Server

SQL Server > DDL > Drop > Table

Removes table definitions and data, indexes, triggers, constraints, and permissions for that table.

Example:

1. Simple drop
    DROP TABLE table_name

2. Drop temporary table and tests for its existence before

  if object_id('tempdb.dbo.##MyGlobalTemp1') Is Not Null
    DROP TABLE ##MyGlobalTemp1





Friday, March 29, 2013

PadLeft and PadRight C#

C# > StringPadRight & PadLeft

PadRight left aligns the characters in this string, padding on the right with a specified Unicode character.
PadLeft right aligns the characters in this string, padding on the lefy with a specified Unicode character.

Example:

string str = "four";
char pad = '.';
string str2 = str.PadLeft(7, pad); // "...four"
string str3 = str.PadRight(7, pad); //"four..."





StartWith string C#

C# > String > Methods > StartWith

StartWith determines whether the beginning of  a string matches the specified value.

string str = "--comment--";
if (str.StartsWith("--"))
   str = "comment";




String.Join Method C#

C# > String > Join

Join concatenates a specified separator string between each element of a specified String array.

Example


String[] str = { "red","green","blue","yellow" };
String sep = ",";
String result = String.Join(sep, str, 0, 4); // "red,green,blue,yellow"





Rowversion SQL Server

SQL Server > Types > Rowversion

Rowversion in SQL Server is a data type that exposes automatically generated, unique binary numbers within a database. Rowversion is generally used as a mechanism for version-stamping table rows.

Note: Use rowversion instead of timestamp wherever possible
Any update made to one row changes also the rowversion value

Example:

CREATE TABLE #tmp
(
    id       INT PRIMARY KEY,
    name     VARCHAR(20),
    rw       ROWVERSION
)
GO

INSERT #tmp(ID, Name) VALUES (1, 'John')
INSERT #tmp(ID, Name) VALUES (2, 'Dan')
GO


SELECT * FROM #tmp

id name rw
1  John  0x0000000000122432
2  Dan   0x0000000000122433

update
   #tmp
set
   name = 'Bill'
where
   id = 1

SELECT * FROM #tmp

id name rw
1  Bill    0x0000000000122434
2  Dan   0x0000000000122433

drop table #tmp






Wednesday, March 27, 2013

Substring function C#

C# > String > Substring

Substring function returns a string in a string

Parameters:

         [starting position] [length]

Example:

String myString = "abcdef";
myString = myString.Substring(0, 3); // "abc"

Other examples:






Tuesday, March 26, 2013

Insert multiple rows WITHOUT repeating INSERT INTO SQL Server

SQL Server > Scripts > Insert multiple rows

create table #user(id int, superiorid int)
insert into #user(id, superiorid) values  (1,null), (2,1) , (3,1)