可能重複:
ALTER TABLE my_table ADD @column INTT-SQL存儲過程的列動態添加到基於參數的表
誰能告訴我,如果有可能有一個存儲過程這實際上是這樣的:
USE [dbname]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [Schema].[CreateColumn]
@Table varchar(255),
@ColumnName varchar(255)
AS
BEGIN
BEGIN TRANSACTION
SET QUOTED_IDENTIFIER ON
SET ARITHABORT ON
SET NUMERIC_ROUNDABORT OFF
SET CONCAT_NULL_YIELDS_NULL ON
SET ANSI_NULLS ON
SET ANSI_PADDING ON
SET ANSI_WARNINGS ON
COMMIT
BEGIN TRANSACTION
ALTER TABLE [Schema][email protected] ADD @ColumnName varchar(255) NULL
ALTER TABLE [Schema][email protected] SET (LOCK_ESCALATION = TABLE)
COMMIT
END
GO
基本上,我可以傳遞一個表名和列名,它會通過正確的交易在該表上創建它。
你需要動態SQL http://www.sommarskog.se/dynamic_sql.html –