sql - Alter table variable to add primary key get error Incorrect syntax -
i want add composite primary key constraint defined table variable depending on condition:
declare @tbl table(col1 int, col2 int) if [mycondition] alter table @tbl add constraint c primary key(col1) else alter table @tbl add constraint c primary key(col1,col2)
but get:
incorrect syntax near '@tbl'.
alter statements can't used on table variables.
as alternative, use temporary table:
create table #tbl (col1 int not null, col2 int not null) if [mycondition] alter table #tbl add constraint c primary key(col1) else alter table #tbl add constraint c primary key(col1,col2)
Comments
Post a Comment