zoukankan      html  css  js  c++  java
  • sql 脚本

    1): 批量插入数据

    begin
        declare @i int
        declare @name nvarchar(max)
        set @i = 2
        while @i < 5000
            begin
                set @name = 'user' + convert(nvarchar(max), @i)
                insert into EricSun_DB.dbo.Users([LoginName]
                                              ,[Email]
                                              ,[DomainGroup]
                                              ,[Title]
                                              ,[Manager]
                                              ,[Department]
                                              ,[IsActive]
                                              ,[IsDeleted]
                                              ,[SID]
                                              ,[Flags]
                                              ,[DisplayName]) 
                            values(@name, 'email', 0, 'mvp', 'manager', 'deparment', 0, 0, 0, 0, 'display name')
                set @i = @i + 1
            end
      end

    2):将table中的数据select出来并且缓存到table变量中

    DECLARE @TempCustomer TABLE
    (
       CustomerId uniqueidentifier,
       FirstName nvarchar(100),
       LastName nvarchar(100),
       Email nvarchar(100)
    );
    INSERT INTO 
        @TempCustomer 
    SELECT 
        CustomerId, 
        FirstName, 
        LastName, 
        Email 
    FROM 
        Customer
    WHERE 
        CustomerId = @CustomerId

    http://stackoverflow.com/questions/4823880/sql-server-select-into-variable 

    3):循环便利table变量中的数据

    DECLARE @RowsToProcess  int
    DECLARE @CurrentRow     int
    DECLARE @SelectCol1     int
    
    DECLARE @table1 TABLE (RowID int not null primary key identity(1,1), col1 int )  
    INSERT into @table1 (col1) SELECT col1 FROM table2
    SET @RowsToProcess=@@ROWCOUNT
    
    SET @CurrentRow=0
    WHILE @CurrentRow<@RowsToProcess
    BEGIN
        SET @CurrentRow=@CurrentRow+1
        SELECT 
            @SelectCol1=col1
            FROM @table1
            WHERE RowID=@CurrentRow
    
        --do your thing here--
    
    END

    http://stackoverflow.com/questions/1578198/can-i-loop-through-a-table-variable-in-t-sql

    4):将table中的数据select出来并且缓存到int变量中,并且向对应的table中添加数据

    declare @dashboardRoleId int
    declare @dashboardAppId int
    declare @dashboardRoleName nvarchar(50)
    
    set @dashboardAppId = 1
    set @dashboardRoleName = 'EricSun'
    insert into [Roles]([AppID],[RoleName]) values (@dashboardAppId, @dashboardRoleName)
    set @dashboardRoleId = (select top(1) [RoleID] from [Roles] where [RoleName] = @dashboardRoleName)
    
    declare @dashboardPageId int
    set @dashboardPageId = (select top(1) [PageID] from [Pages] where [UrlName] = 'Dashboard')
    
    insert into [PageRoles]([PageID], [RoleID], [CreateTime]) values (@dashboardPageId, @dashboardRoleId, GETDATE())
     
  • 相关阅读:
    组合模式及C++实现
    YUV422(UYVY)转RGB565源代码及其讲解.md
    会用errno,事半功倍
    可变参数宏
    camera理论基础和工作原理
    !!!??? 2.3 核心模块与应用程序的对比
    KVM与VMware的性能比较
    单片机中定时器与计数器的区别
    编译器对变量的内存分配方式
    【转载】Modelsim 与Vivado联合仿真版本对应问题
  • 原文地址:https://www.cnblogs.com/mingmingruyuedlut/p/2707415.html
Copyright © 2011-2022 走看看