How can I conditionally construct a table name for an SQL CREATE TABLE statement?

前端 未结 3 1897
孤独总比滥情好
孤独总比滥情好 2021-01-24 17:51

Within an SQL stored procedure, I would like to have the ability to construct a table name and create it.

Example: I just logged into my database under company 03 and a

3条回答
  •  孤城傲影
    2021-01-24 18:41

    You would need to use dynamic SQL eg:

    DECLARE @company_id char(2)
    SET @company_id = '02'
    EXEC('CREATE TABLE CUSTOMER' + @company_id + ' (id int PRIMARY KEY)')
    

    (tested)

提交回复
热议问题