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

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -