【问题标题】:Procedure - create table referencing other table column type过程 - 创建引用其他表列类型的表
【发布时间】:2014-09-24 22:43:15
【问题描述】:

您好,我知道在 PL/SQL 中可以使用 %TYPE 引用列类型,但我想知道在 T-SQL 中是否可以使用相同的东西。

PROCEDURE ....

CREATE TABLE TABLE_B
(
   ID    NUMBER NOT NULL,
   NAME  TABLE_A.NAME%TYPE
);

... END PROCEDURE

这可能吗?

【问题讨论】:

标签: sql-server tsql


【解决方案1】:

简答否:

我是验证的忠实拥护者,所以这里有 3 个来源,它们基本上都说了同样的话:不,但它可以被模拟(尽管很痛苦),它给设计和维护增加了一层复杂性。

参考资料:

  1. Microsoft SQL Server does not support and has no an equivalent of Oracle %TYPE.
  2. If your table's data type is likely to change, you can use a user-defined-type in both the table and the proc. This will mean that any changes you make to the UDT are made to both the table and the proc
  3. Migrating to SQL server from Oracle

用户定义类型示例:

Oracle 的 %TYPE 数据类型允许您创建一个变量,并让该变量的数据类型由表或视图列或 PL/SQL 包变量定义。

在 T-SQL 中没有与 Oracle 的 %TYPE 数据类型等效的东西,但可以使用用户定义的数据类型 (UDT) 来模拟(虽然不是很方便)。这是一个例子:

EXEC sp_addtype 'MyType', 'smallint', NULL

CREATE TABLE MyTable (i MyType)

CREATE PROC MyProc
AS
BEGIN
DECLARE @i MyType
END

【讨论】:

    猜你喜欢
    • 2018-11-08
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2017-12-02
    • 2020-09-27
    • 1970-01-01
    • 2021-10-22
    • 1970-01-01
    相关资源
    最近更新 更多