T-SQL How to create tables dynamically in stored procedures?

后端 未结 5 1090
执笔经年
执笔经年 2020-11-29 05:16

Code like this, but it\'s wrong:

CREATE PROC sp_createATable
  @name        VARCHAR(10),
  @properties  VARCHAR(500)
AS
  CREATE TABLE @name
  (
    id  CHAR         


        
5条回答
  •  半阙折子戏
    2020-11-29 06:05

    First up, you seem to be mixing table variables and tables.

    Either way, You can't pass in the table's name like that. You would have to use dynamic TSQL to do that.

    If you just want to declare a table variable:

    CREATE PROC sp_createATable 
      @name        VARCHAR(10), 
      @properties  VARCHAR(500) 
    AS 
      declare @tablename TABLE
      ( 
        id  CHAR(10)  PRIMARY KEY 
      ); 
    

    The fact that you want to create a stored procedure to dynamically create tables might suggest your design is wrong.

提交回复
热议问题