Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I don't know if this is possible, but what I want to have is a stored procedure that I can pass the name of the column that I wish to be added to my table from my vb.net application.

 

example: from my vb.net app I would call the stored procedure and pass 'column1' to it and then within the stored procedure it would alter the table and add a new column called 'column1' to it.

 

So the stored procedure would look something like this:

 

CREATE PROCEDURE [dbo].[spAddColumn] @columnName as varChar(50)
AS
ALTER TABLE myTbl ADD @columnName varChar(50)
GO

 

The above SQL statement gives me an error:

Error 170: Line 3: Incorrect syntax near '@columnName'.

 

Is this possible??? And if so could someone please point out what I am doing wrong.

 

Thanks in advance

 

Simon

  • *Experts*
Posted

You'll have to use some dynamic SQL for this to work and manually piece in the column name. No guarantees that will work but that's what I'd try. It would be something like this:

CREATE PROCEDURE [dbo].[spAddColumn] @columnName as varChar(50)
AS
DECLARE @GoodTableName varchar(255)
SET @GoodTableName = '[' + REPLACE(@columnName, '''', '''''') + ']'
exec ('ALTER TABLE myTbl ADD ' + @GoodTableName+ ' varChar(50)')

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...