lidds Posted September 26, 2005 Posted September 26, 2005 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 Quote
*Experts* Nerseus Posted September 26, 2005 *Experts* Posted September 26, 2005 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 Quote "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
lidds Posted September 26, 2005 Author Posted September 26, 2005 Thanks I will give this a go tomorrow, bit late for me to attempt new stuff :p Cheers Simon Quote
lidds Posted September 28, 2005 Author Posted September 28, 2005 Can't thank you enough Nerseus, It worked like a charm, and has given me ideas on how to acheive some other stuff as well. Simon Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.