In This Tutorial we create table and want to same structure and same data then We create new table with data type after that insert in table
this process is very time consuming and take more time so solved this problem in sql server we just create replica of table only using simple query like in example
step wise show-
1st Step - Firstly open sql server and select database
2nd Step - Create table like in examples
Example-
CREATE TABLE Employee
(
EmpId int,
EmpName nvarchar(50),
)
3rd Step - Created table after that Insert Data.
4Th Step - Create Replica of table in Sql query like
Syntex-
SELECT * INTO New_TableName From Existing_Table
is existing table name so using this we easly create duplicate table.
In this artical you can prvent duplicat entery.Means if you enter any record and you don't enter same record again then you check firstly record is exit and not follows some steps like-
BEGIN
SELECT '! exist'
END
ELSE
SELECT '! Not exist'
NOTE- Here @ColumnName is parameter pass when we create procedure then Write this code then we can't face duplicate entry
Note- See In previous example in Employee table Ram EmpName is already exit. Output show Record already exist
1st Step - Firstly open sql server and select database
2nd Step - Create table like in examples
Example-
CREATE TABLE Employee
(
EmpId int,
EmpName nvarchar(50),
)
3rd Step - Created table after that Insert Data.
4Th Step - Create Replica of table in Sql query like
Syntex-
SELECT * INTO New_TableName From Existing_Table
Example
SELECT * INTO Replica_Employee From Employee
5th Step - Run This Query and See Replica_Employee Table Data
In this example Replica_Employee is new table name whatever u want and Employee5th Step - Run This Query and See Replica_Employee Table Data
is existing table name so using this we easly create duplicate table.
HOW TO CHECK DUPLICATE ENTERY
In this artical you can prvent duplicat entery.Means if you enter any record and you don't enter same record again then you check firstly record is exit and not follows some steps like-
- Step 1 : Firstly open sql and create database in Sql.
- Step 2 : Second steps is create table in Sql.
- Step 3 : Third steps is Insert records in Sql.
- Step 4 : Forth step Check Duplicat record.
- Step 5 : Get result.
CREATE DATABASE db_Emp
created database and execute run(F5) then use this write code like-
USE db_Emp --
Step 2:Created table Employee and insert record in table step 3 both steps See in previous example.
Step 4:Inserted record and check duplicate entery in table like-
created database and execute run(F5) then use this write code like-
USE db_Emp --
SYNTEX
IF((SELECT COUNT(*) FROM TableName WHERE ColumnName= @ColumnName) > 0)BEGIN
SELECT '! exist'
END
ELSE
SELECT '! Not exist'
NOTE- Here @ColumnName is parameter pass when we create procedure then Write this code then we can't face duplicate entry
Example
CREATE Sp_CheckDuplicate
(
@Name nvarchar(50)
)
Set @Name='Ram'
IF((SELECT COUNT(*) FROM Employee WHERE EmpName= @Name) > 0)
BEGIN
SELECT '!Record already exist'
END
ELSE
SELECT '! Not exist'
Output-
!Record already exist(
@Name nvarchar(50)
)
Set @Name='Ram'
IF((SELECT COUNT(*) FROM Employee WHERE EmpName= @Name) > 0)
BEGIN
SELECT '!Record already exist'
END
ELSE
SELECT '! Not exist'
Note- See In previous example in Employee table Ram EmpName is already exit. Output show Record already exist
0 comments :
Post a Comment