如何有条件地为SQL CREATE TABLE语句构造表名? [英] How can I conditionally construct a table name for an SQL CREATE TABLE statement?
本文介绍了如何有条件地为SQL CREATE TABLE语句构造表名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在一个SQL存储过程中,我希望能够构造一个表名并创建它.
Within an SQL stored procedure, I would like to have the ability to construct a table name and create it.
示例:我刚刚登录到公司03下的数据库,并且不存在客户表,因此我希望将处理过程转移到CREATE TABLE CUSTOMER03
.
Example: I just logged into my database under company 03 and a customer table does not exist, so I would like for the proc to CREATE TABLE CUSTOMER03
.
是否有一种方法可以将append company_id char(2)
转换为CUSTOMER
并将其提供给CREATE TABLE
语句?也许像
Is there a way to append company_id char(2)
to CUSTOMER
and feed it to the CREATE TABLE
statement? maybe like
CREATE TABLE $tablename or $tablename+company_id?
推荐答案
您将需要使用动态SQL,例如:
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)')
(已测试)
这篇关于如何有条件地为SQL CREATE TABLE语句构造表名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文