2015-03-25 51 views
0

假设,我有一个临时表#Temp1与说两列名称和国籍作为Varchar(50)数据类型和表中有一些记录。我从这个表(#temp1)创建另一个临时表作为#Temp2。现在我想添加另外几列到该临时表中,将PhoneNo表示为Int和Gender作为Char数据类型。我如何实现这一目标?改变动态创建表ms

Begin Tran 
Create table #Temp1(
    Name Varchar(50), 
    Nationality Varchar(50) 
); 

Insert Into #Temp1 (Name, Nationality) Values ('Jayaraj','Indian'); 

Select Identity(Int,1,1) SlNo, Name Into #Temp2 
From #Temp1 Order By Nationality  

-- Now the actual issue begins. I wish to alter the #Temp2 table. 
-- So I try to alter the table, to add column Phone and Gender 

Alter Table #Temp2 
Add(
    Phone Int, 
    Gender Char 
); 

Select * from #Temp2 

-- Upon Executing I get this error : 
/* 
    Msg 102, Level 15, State 1, Line 16 
    Incorrect syntax near '('. 
*/ 

Rollback 

感谢您的帮助..

回答

2

你可以这样做:

Alter Table #Temp2 
Add Phone Int, Gender Char 

只是要删除 “()”。 :-)

+0

解决了这个问题。感谢您的快速回复。 – 2015-03-25 06:06:54

+0

NP @JayarajPS :) – 2015-03-25 06:14:20

0

使用此syntax

ALTER TABLE #Temp2 
ADD Phone Int 

ALTER TABLE #Temp2 
ADD Gender Char