zoukankan      html  css  js  c++  java
  • 有趣的自定义类型:表


    --创建自定义类型,这里的自定义类型是表,可存储记录
    CREATE TYPE LocationTableType AS TABLE 
        ( LocationName VARCHAR(50)
        , CostRate INT )
    GO
     
    
    declare @t LocationTableType
    
    /*
    --通过表变量来中转
    declare @tt table 
    ( LocationName VARCHAR(50),CostRate INT )
    
    insert into @tt
    select '123',1
    
    insert into @t
    select * from @tt
    */
    
    --可以直接把数据添加到LocationTableType类型中 
    --可以存储多条记录
    insert into @t
    select '123',1
    union all
    select 'abc',2
    
    select *
    from @t
    

    用于存储过程中:

    --创建自定义类型,这里的自定义类型是表,可存储记录
    CREATE TYPE MyTable AS TABLE 
        ( a VARCHAR(50),
          b INT )
    GO
    
    
    --drop proc proc_table
    
    
    --创建存储过程,传入参数为自定义表
    CREATE Procedure dbo.proc_table  
    (@ManyRows as MyTable readonly  
    )  
    as
    
    select * from @manyrows
    go
    
     
    
    
    declare @t MyTABLE
    
    
    --可以直接把数据添加到LocationTableType类型中 
    --可以存储多条记录
    insert into @t
    select '123',1
    union all
    select 'abc',2
    
    
    exec proc_table @t
    /*
    a                                                  b
    -------------------------------------------------- -----------
    123                                                1
    abc                                                2
    
    (2 行受影响)
    */
    




  • 相关阅读:
    Python的历史
    python excel
    excel xdr wdr
    sql 常用命令
    selenium 配置firefox
    SQL 一直恢复状态解决方法
    sqlserver 学习
    ITCHAT用法
    健身卡属性,以及业务规则,
    安装REDIS
  • 原文地址:https://www.cnblogs.com/momogua/p/8304578.html
Copyright © 2011-2022 走看看