Create Keys on a Temp Table in a SP

摘要:Create Keys in a Temp Table with a SP

 

-- Create a store procedure

CREATE PROCEDURE sp_test

AS

--Create a Temp table with a primary key
CREATE TABLE #table
(
 [id1] [int] NOT NULL,
 [id2] [nchar](10) NULL,
 [id3] [nchar](10) NULL,
 CONSTRAINT [PK_table] PRIMARY KEY CLUSTERED
 ([id1] ASC))

--Create a NONCLUSTERED key on a temp table
CREATE NONCLUSTERED INDEX [IX_tb01] ON #table
(
 [id2] ASC
)

INSERT #table VALUES(1,1,1)

SELECT * FROM #table

--important: remeber to drop this temp table
--   otherwise u will get a message saying that the key PK_table exists in the DB when u exec the sp for the second --     time

DROP TABLE #table

go

--the end of the sp
-----------------------------------------------------------------------

--Testing this SP
EXEC sp_test


 

 資料資考自 http://forums.microsoft.com/MSDN-CHT/ShowPost.aspx?PostID=1453287&SiteID=14