DDL Stored Procedure

  • Is there a way to pass table's name to a stored procedure?

    This is what I am trying to do:

    CREATE PROCEDURE NAME_PROC

    (

    @TABLENAME varchar(30)

    )

    AS

    CREATE TABLE @TABLENAME (OID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,......)

    GO

    but raise a sintax error near @TABLENAME

  • You would need to use dynamic sql for this task:-

    CREATE PROCEDURE NAME_PROC

    (

    @TABLENAME sysname

    )

    AS

    declare @sql varchar(1000)

    select @sql = 'CREATE TABLE ' + @TABLENAME + ' (OID int IDENTITY(1, 1) PRIMARY KEY CLUSTERED,......)'

    exec (@sql) --Could also use sp_executesql here.

    Regards,

    Andy Jones

    .

Viewing 2 posts - 1 through 1 (of 1 total)

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