error while executing stored procedure

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hanuman308
    New Member
    • Sep 2008
    • 2

    error while executing stored procedure

    hi there
    i have written follwing SP
    to add two columns in my table (passing tablename as parameter)

    create procedure sp_addCol
    @tablename varchar (50)
    as
    DECLARE @tsql_TZ varchar (200)
    SET @tsql_TZ = 'ALTER TABLE [ '+@tablename+'] ADD timezone varchar (5)'
    DECLARE @tsql_EXP varchar (200)
    EXEC(@tsql_TZ)
    SET @tsql_EXP = 'ALTER TABLE [ '+@tablename+'] ADD exported varchar (5)'
    EXEC(@tsql_EXP)

    when i try to execute the SP
    using following command

    execute sp_addCol 'MGV10SEP_MNPC_ MNLD'

    it show me the error
    Server: Msg 4902, Level 16, State 1, Line 1
    Cannot alter table ' MGV10SEP_MNPC_M NLD' because this table does not exist in database 'SIL_TESTDB'.
    Server: Msg 4902, Level 16, State 1, Line 1
    Cannot alter table ' MGV10SEP_MNPC_M NLD' because this table does not exist in database 'SIL_TESTDB'.

    but table is actually exist in database
    please help

    thanks & regards
  • deepuv04
    Recognized Expert New Member
    • Nov 2007
    • 227

    #2
    Hi,
    In the following two statements there is an extra space at the tablename.

    SET @tsql_TZ = 'ALTER TABLE [ '+@tablename+'] ADD timezone varchar (5)'
    SET @tsql_EXP = 'ALTER TABLE [ '+@tablename+'] ADD exported varchar (5)'


    When the string is created an extra space is added to the name of the table which is not there. Remove the space and try again.

    try this code
    [code=sql]
    create procedure sp_addCol
    @tablename varchar (50)
    as
    DECLARE @tsql_TZ varchar (200)
    SET @tsql_TZ = 'ALTER TABLE ['+@tablename+'] ADD timezone varchar (5)'
    DECLARE @tsql_EXP varchar (200)
    EXEC(@tsql_TZ)
    SET @tsql_EXP = 'ALTER TABLE ['+@tablename+'] ADD exported varchar (5)'
    EXEC(@tsql_EXP)

    [/code]

    Thanks

    Comment

    • hanuman308
      New Member
      • Sep 2008
      • 2

      #3
      Originally posted by deepuv04
      Hi,
      In the following two statements there is an extra space at the tablename.

      SET @tsql_TZ = 'ALTER TABLE [ '+@tablename+'] ADD timezone varchar (5)'
      SET @tsql_EXP = 'ALTER TABLE [ '+@tablename+'] ADD exported varchar (5)'


      When the string is created an extra space is added to the name of the table which is not there. Remove the space and try again.

      try this code
      [code=sql]
      create procedure sp_addCol
      @tablename varchar (50)
      as
      DECLARE @tsql_TZ varchar (200)
      SET @tsql_TZ = 'ALTER TABLE ['+@tablename+'] ADD timezone varchar (5)'
      DECLARE @tsql_EXP varchar (200)
      EXEC(@tsql_TZ)
      SET @tsql_EXP = 'ALTER TABLE ['+@tablename+'] ADD exported varchar (5)'
      EXEC(@tsql_EXP)

      [/code]

      Thanks




      oh!!!!!
      thanks 4 ur reply :-)

      Comment

      Working...