declare @pk_tablename nvarchar(500),@pk_id nvarchar(50),@pk_add_sql nvarchar(500),@pk_del_sql nvarchar(500) set @pk_tablename='Common_BodyPartRecord' set @pk_id='Id' set @pk_add_sql='ALTER TABLE @pk_tablename ADD CONSTRAINT PK_@pk_tablename PRIMARY KEY CLUSTERED (@pk_id)' EXEC SP_EXECUTESQL @pk_add_sql,N'@pk_tablename nvarchar(500),@pk_id nvarchar(50)',@pk_tablename,@pk_id
在PK_@pk_tablename这儿怎么改?
第一种方法:
declare @pk_tablename nvarchar(500),@pk_id nvarchar(50),@pk_add_sql nvarchar(500),@pk_del_sql nvarchar(500) set @pk_tablename='PK_Common_BodyPartRecord' set @pk_id='Id' set @pk_add_sql='ALTER TABLE @pk_tablename ADD CONSTRAINT @pk_tablename PRIMARY KEY CLUSTERED (@pk_id)' EXEC SP_EXECUTESQL @pk_add_sql,N'@pk_tablename nvarchar(500),@pk_id nvarchar(50)',@pk_tablename,@pk_id
第二种方法:
declare @pk_tablename nvarchar(500),@pk_id nvarchar(50),@pk_add_sql nvarchar(500),@pk_del_sql nvarchar(500) set @pk_tablename='Common_BodyPartRecord' set @pk_id='Id' set @pk_add_sql='Set @pk_tablename = ''PK_''+@pk_tablename;ALTER TABLE @pk_tablename ADD CONSTRAINT @pk_tablename PRIMARY KEY CLUSTERED (@pk_id)' EXEC SP_EXECUTESQL @pk_add_sql,N'@pk_tablename nvarchar(500),@pk_id nvarchar(50)',@pk_tablename,@pk_id