StatCounter

View My Stats

Monday, February 4, 2008

To copy a Table Records

The query as Follows:

1. select * into NewTableName from OldTableName

It copies all records From old Table and create a new Table with all old table records


2. insert into to_TableName select column1,column2 from FromTableName


For Example Conside 2 Table
1.create table Employee(id1 int identity,name1 varchar(50),address varchar(100),city varchar(100))

2.create table NameCollextion(id1 int identity,name1 varchar(50))

For the above two table Employee,NameCollection ,If u want to copy all the Name Rows into NameCollection Table Follow the below query

insert into NameCollection(name1) select name1 from Employee

You can also use where condition for the above query
Example
Yo
insert into NameCollection(name1) select name1 from Employee where id1 between 1 and 50

Enable and Disable a Constraint in Table-SQL Server

The query as Follows:

Disable a constraint on a Table:

Alter table TableName nocheck constraint ConstraintName

Enable a constraint on a Table:

Alter table TableName check constraint ConstraintName

While Disabling a constaints: For Example Consider The colum of integer field have primary key constraints.Its functionality wont allow the duplicate values in that column.
After disabling the constraints....It allows the duplicate value in that column.
You can Enable and disable the constraints as per needs
Whi

Drop a Constraint From Table-SQL Server

The query as Follows:

ALTER TABLE employee DROP CONSTRAINT pk_employee

Here
employee-TableName
pk_employee-Constraint Name of the Employee Table

Adding a constraint to the esisting Table-SQL Server

The query as Follows:

ALTER TABLE TableName ADD CONSTRAINT pk_employee PRIMARY KEY (EmployeeId)

Here
pk_employee - constraint Name
EmployeeId -ColumnName of the Table

While adding the constraints to the before Existing table..that column name set to notNull(it must)

Removing a Leading and Trailing Space Of a Word

The query as Follows:

Select LTRIM(RTRIM(Column_Name)) From TableName

Ramdomly Selecting a recods in SQL Server:

The Query is as Follows:

Select * from TableName order by NewID()