0

I have a declared parameter, say @column_name varchar(200). I don't know how to add a column to a existing table with this column_name.

The following code has syntax errors.

alter table table_name add @column_name varchar(200)

Anyone know how do solve this?

1 Answer 1

1

You have to use dynamic SQL if your want a dynamic column name:

declare @column_name varchar(100)
set @column_name = 'col_new'
exec('alter table table_name add ' + @column_name + ' varchar(200)')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.