0

Why do I get this error when I try to execute the following code? I have a table NewTable1 with two columns: column1 and column2.

I get this error: Incorrect syntax near 'column2'.

--DROP COLUMN PROCEDURE

CREATE PROCEDURE DropColumn
    @tableName varchar(50),
    @columnName varchar(50)
AS

    BEGIN
        DECLARE @SQL nvarchar(500);
        SET @SQL = N'ALTER TABLE ' + QUOTENAME(@tableName) 
         + ' DROP COLUMN ' + QUOTENAME(@columnName);
        EXEC sp_executesql @SQL;
    END
RETURN 0
GO

USE SKI_SHOP;
EXEC DropColumn 'NewTable1', 'column2';
GO
10
  • I don't think table/column name could be a variable. not 100% sure though Commented Nov 6, 2014 at 19:29
  • 1
    @Steve That's why op is using dynamic SQL Commented Nov 6, 2014 at 19:30
  • do you have a better idea? :d any tips? @Steve Commented Nov 6, 2014 at 19:32
  • Is SKI_SHOP the same DB you created the SP in? This code works fine for me in SQL Server 2012 Commented Nov 6, 2014 at 19:34
  • 1
    As a note: the return 0 is not part of the stored procedure. Commented Nov 6, 2014 at 19:41

1 Answer 1

2

Use appropriate data types. Also You will only be able to drop Columns for tables in callers default schema. Since procedure doesn't take schema into consideration, therefore you can only pass the table name and if a table exists in other than caller default schema they wont be able to delete it using this procedure .

CREATE PROCEDURE DropColumn
    @tableName  SYSNAME,
    @columnName SYSNAME
AS
BEGIN
  SET NOCOUNT ON;

 DECLARE @SQL NVARCHAR(MAX);

 SET @SQL = N' ALTER TABLE ' + QUOTENAME(@tableName) 
          + N' DROP COLUMN ' + QUOTENAME(@columnName);

      EXEC sp_executesql @SQL;
 END
GO

I over looked some basic simple issues in my first approach, whenever creating of Dropping objects in SQL Server always check if they exist, to avoid any errors . A more complete and safe approach would be something like ...

This time I have also added schema as a parameter.

ALTER PROCEDURE DropColumn
    @tableName  SYSNAME,
    @columnName SYSNAME,
    @Schema     SYSNAME,
    @Success    BIT OUTPUT
AS
BEGIN
  SET NOCOUNT ON;

 DECLARE @SQL NVARCHAR(MAX);

 SET @SQL = N' IF EXISTS (SELECT * FROM sys.tables t 
                          INNER JOIN sys.columns c 
                          ON t.[object_id] = c.[object_id]
                          INNER JOIN sys.schemas sc
                          ON t.[schema_id] = sc.[schema_id]
                          WHERE t.name  = @tableName
                            AND c.name  = @columnName
                            AND sc.name =  @Schema)
                BEGIN
                  ALTER TABLE ' +  QUOTENAME(@Schema)+ '.' + QUOTENAME(@tableName) 
           + N'   DROP COLUMN ' + QUOTENAME(@columnName)
           + N'   SET @Success = 1; '
           + N' END 
               ELSE
                BEGIN
                  SET @Success = 0; 
                END '

  EXEC sp_executesql @SQL
                     ,N'@tableName SYSNAME, @columnName SYSNAME, @Schema SYSNAME, @Success BIT OUTPUT'
                     ,@tableName
                     ,@columnName
                     ,@Schema
                     ,@Success OUTPUT
 END
GO
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.