简体   繁体   English

在MS SQL中更改动态创建的表

[英]Alter dynamically created table in ms sql

Suppose, I have a temp table #Temp1 with say two columns Name and Nationality as Varchar(50) datatypes and the table has some records. 假设我有一个临时表#Temp1,其中说两列Name和Nationality为Varchar(50)数据类型,并且该表具有一些记录。 I create another temp table from this table(#temp1) as #Temp2. 我从该表(#temp1)创建另一个临时表作为#Temp2。 Now I wish to add another couple of column to that temp table say PhoneNo as Int and Gender as Char datatypes. 现在,我想在该临时表中添加另外几列,例如将PhoneNo表示为Int并将Gender表示为Char数据类型。 How do I achieve that? 我该如何实现?

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

Thanks for helping.. 感谢您的帮助。

You can do this: 你可以这样做:

Alter Table #Temp2
Add Phone Int, Gender Char

Just have to remove "()". 只需删除“()”即可。 :-) :-)

Use this syntax 使用此syntax

ALTER TABLE #Temp2
ADD Phone Int

ALTER TABLE #Temp2
ADD Gender Char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM