ALTER PROCEDURE [dbo].[create_history_table]
@TagID Integer
AS
BEGIN
exec('CREATE TABLE [his].[' + @TagID + '](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Time] [datetime] NOT NULL
CONSTRAINT [PK_' + @TagID + '] PRIMARY KEY NONCLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
)')
RAISERROR('需要重新設(shè)置新表 his.%d的分區(qū)策略.',16,1, @TagID)
exec('CREATE NONCLUSTERED INDEX [IX_card_time] ON [his].[' + @TagID + ']
(
[Time] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
')
END
-- 描述存儲過程create_history_table:
exec sp_helptext [create_history_table]
-- 判斷表是否存在:
select count(*) from information_schema.TABLES
where 1 = 1
and TABLE_CATALOG = '數(shù)據(jù)庫名'
and TABLE_SCHEMA = '模式名'
and TABLE_NAME = '表名'
-- 不存在的話,調(diào)用存儲過程create_history_table創(chuàng)建表:
-- 兩端的花括號不要漏掉袱瓮。
{ call [數(shù)據(jù)庫名'].[模式名'].[create_history_table](21) }