Tuesday, July 7, 2020

HOW TO INSERT RECORDS IN TEMPORARY TABLE IN SQL SERVER

Step 1 - You have to first create Temporary table.

DECLARE @USERTEMPTABLE TABLE
(

   ID INT PRIMARY KEY IDENTITY(1,1),

   NAME NVARCHAR(100),

   USER_NAME NVARCHAR(200),

   EMAIL NVARCHAR(200),

   MOBILE_NUMBER NVARCHAR(50),

   ADDRESS NVARCHAR(200)
)

Step 2 - Insert records into the temporary table.

INSERT INTO @USERTEMPTABLE(NAME,USER_NAME,EMAIL,MOBILE_NUMBER,ADDRESS)
VALUES
('RAMAN SACHDEVA','RMN_001','***@gmail.com','98780*****','CHANDIGARH'),
('ANAND SACHDEVA','ANAD_051','***@gmail.com','99520*****','CHANDIGARH'),
('ROHIT SACHDEVA','RHT_250','***@gmail.com','87520*****','CHANDIGARH'),
('ARUN SHARMA','ARN_002','***@gmail.com','95140*****','MOHALI'),
('SURINDER SINGH','SRN_300','***@gmail.com','98520*****','JALANDHAR'),
('KULWINDER SINGH','KLN_1000','***@gmail.com','87550*****','AMRITSAR'),
('PALKA SACHDEVA','PLK_550','***@gmail.com','98560*****','CHANDIGARH'),
('AKASHDEEP SINGH','AKSH_121','***@gmail.com','79851*****','DELHI'),
('HARISH KUMAR','HRSH_050','***@gmail.com','79862*****','MUMBAI'),
('FATEH SINGH','FTH_660','***@gmail.com','96468*****','PUNE')

After inserting the records, you can fetch all the records from temporary table with the below query.

SELECT * FROM @USERTEMPTABLE

Here is an example of inserting a records into the temorary table. Please see the screenshot for further clarification.




- - - - - - - - - - - - - - - - - - - - - - - - Cheers, Happy to Help! - - - - - - - - - - - - - - - - - - - - - - - - - - -

4 comments:

  1. What is the use of temp table?

    ReplyDelete
    Replies
    1. Temporary table are used to store and process the immediate results. It is very useful when we need to store temporary data.

      Delete
    2. Then what is the difference between normal table & temp table ?

      Delete
    3. Temp table acts like a physical table and its created or stored in tempdb database.You can do any operation in temp table as you do with the normal table. Normal table are the permanent table which store the information permanently. Temp table works like as a session to store data in temp table.

      Delete

DIFFERENCE BETWEEN CHAR AND VARCHAR IN SQL SERVER

CHAR:  The  CHAR datatype is a fixed length data type in sql server. It is used to store fixed length type of string data or character strin...