【问题标题】:Create @TableVariable based on an existing database table?基于现有数据库表创建@TableVariable?
【发布时间】:2011-12-20 08:59:48
【问题描述】:

我想在存储过程中使用表变量,但这里有个问题。我的表非常大,声明一个表变量也需要很长的代码来编写和调试。

请告诉我一些快速声明表变量的方法,是否可以根据现有表创建表变量?

或者请分享任何技巧来创建用于创建表变量的代码。

谢谢

【问题讨论】:

    标签: sql-server sql-server-2005 tsql


    【解决方案1】:

    简单的答案是“不,您不能基于其他表创建变量表”

    但是,您可以通过使用类型表来概括一下。 例如(注意:您可以将文档添加到类型表和列中,以供将来参考):

    PRINT 'type: [dbo].[foo_type]'
    PRINT ' - Check if [dbo].[foo_type] TYPE exists (and drop it if it does).'
    GO
    IF EXISTS (SELECT 1 FROM sys.types WHERE name = 'foo_type' AND is_table_type = 1 AND SCHEMA_ID('dbo') = schema_id)
    BEGIN
        -- Create the proc
        PRINT ' - Drop TYPE [dbo].[foo_type]';
        DROP TYPE [dbo].[foo_type];
    END;
    GO
    PRINT ' - create [dbo].[foo_type] TYPE.'
    GO
    CREATE type [dbo].[foo_type] as Table
    (
            [id]                    int identity(1,1) PRIMARY KEY
            , [name]                varchar(255) NOT NULL
            , [description]            varchar(255)
            , numeric_data            numeric(26, 6)
            , datetimestamp            datetime default getdate() 
            , Unique_Indicator        float unique not null default cast(getdate() as float)
            , CHECK (Unique_Indicator > 0)
    
    );
    GO
    PRINT ' - done.'
    GO
    
    
    -- Adding the descriptions
    PRINT ' - Adding Type level Description'
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'describe the usage of this type.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TYPE',@level1name=N'foo_type'
    GO
    PRINT ' - Adding Column level Descriptions'
    PRINT '   - column: id'
    EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'ID of the record...' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TYPE',@level1name=N'foo_type', @level2type=N'COLUMN',@level2name=N'ID';
    GO
    
    ------------------------------------------------------------------------------------------------
    -- use the type defined above to manipulate the variable table:
    
    declare @foo_table foo_type;
    
    --insert using the default value for the for the unique indicator.
    insert into @foo_table (name, [description], numeric_data, datetimestamp)
        values('babar', 'this is the king of the elephants', 12.5, '1931-01-01')
            ;
    
    -- insert the records one by one to use the scope_identity() for the unique indicator.
    insert into @foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
        values('zephyr', 'Babar''s monkey friend', 5.5, '1932-01-01', scope_identity())
            ;
    insert into @foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
        values ('Celeste', 'Babar''s sister', 19.5, '1932-01-01', scope_identity())
            ;
    
    -- insert using a list of values
    insert into @foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator )
        values('Babur', 'Not Babar!!!', 1483, '1983-02-14', 10)
            , ('Mephistopheles', 'Not Babar either...', 666, '1866-01-01',11)
            ;
    
    -- insert using a select
    insert into @foo_table (name, [description], numeric_data, datetimestamp, Unique_Indicator)
        (select 'Conan', 'The Cimmerian barbarian', 850, '1932-12-01',99 union
            select 'Robert E. Howard', 'Conan''s creator', 30, '1906-01-22', 100
        );
    
    -- check the data we inserted in the variable table.
    select * from @foo_table;
    
    
    -- Clean up the example type
    DROP TYPE [dbo].[foo_type];
    

    【讨论】:

      【解决方案2】:

      正如SO Question 中所讨论的,您不能选择表变量。

      当您说“大”时,如果您的意思是很多列,那么对您来说最好的方法可能是将该表编写为创建脚本并保存定义并在您的 Declare 语句中使用它。

      如果您的意思是表变量中的行数很大,您可能需要考虑使用一个临时表,然后您可以执行 SELECT INTO 语句来根据原始表创建它.

      SELECT * INTO #tmpTable FROM srcTable
      

      【讨论】:

      • 但是如果表结构发生了变化呢?
      【解决方案3】:

      右击表格,选择Script As Create

      create table xxx 替换为declare @xxx table

      【讨论】:

      • 谢谢,我们可以根据现有表创建表变量吗?
      • @haansi Andomar 所描述的(以及我所描述的)是如何去做。正如我在回答中所说,您不能像对临时表那样从现有表中创建它。编写脚本并将其用于您的声明是唯一真正的方法。
      猜你喜欢
      • 1970-01-01
      • 2011-01-09
      • 2015-01-30
      • 2020-01-01
      • 2015-11-28
      • 2013-06-05
      • 1970-01-01
      • 1970-01-01
      • 2021-08-21
      相关资源
      最近更新 更多