Monday, August 22, 2011

Different ways to Create SQL Table

Create Table Category2
(
ID INT Primary Key,
NAME VARCHAR(50)
)
Insert into Category2 (ID, Name)
Select CategoryID, CategoryName from Category
GO
Select ID, Name from Category2
GO


--OR


Select CategoryID as ID, CategoryName as Name INTO Category2 from Category
Select ID, Name from Category2


--OR


Create table #Category2
(
ID INT Primary Key,
NAME VARCHAR(50)
)
Insert into #Category2
Select CategoryID, CategoryName from Category

Select ID, Name from #Category2
GO

--OR

Create View Category2
as
Select CategoryID as ID, CategoryName as Name from Category
GO
Select ID, Name from Category2
GO

--OR

Declare @Category2 Table
(ID INT Primary Key,
NAME VARCHAR(50)
)
Insert into @Category2 (ID, Name)
Select CategoryID, CategoryName from Category

Select ID, Name from @Category2
GO

--OR


With Category2
( ID, Name)
AS
(
Select CategoryID, CategoryName from Category
),

Select * from Category2
GO