Alter Table within a Stored Procedure

  • I have the following Stored Procedure. When I run it, it executes without errors, but it does not alter the table to add the new column? Any ideas?

    CREATE PROCEDURE sp_MyStoredProcedure AS

    BEGIN

    exec sp_executesql 'alter table MyTable add dte datetime null'

    END

  • First I think you need

    ... sp_executeSQL N' alter table ...'

    And it works for me, SS2K5, dev, SP2.

    Columns added to table.

    Do you have permissions to add columns?

  • It should work. Are you sure you're in the right database? Are you looking through Management Studio? You might need to refresh in order to see the change.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks Steve,

    Indeed the N is needed before the string otherwise you get the following error:

    Procedure expects parameter '@statement' of type 'ntext/nchar/nvarchar'

  • Also, on a side note, it is not recommended to use the prefix "sp" for user-defined stored procedures. This prefix is used with system stored procedures and can affect performance by causing an extra lookup in the master database. You can use whatever naming conventions you feel comfortable with but the common convention is to use "usp". I know that you gave that name just as an example, but just in case.....

    Bob Pinella

Viewing 5 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic. Login to reply