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())

 

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-05
  • 2021-10-13
  • 2022-12-23
  • 2022-12-23
  • 2022-02-20
  • 2021-10-23
猜你喜欢
  • 2022-02-08
  • 2021-06-29
  • 2022-12-23
  • 2021-11-17
  • 2021-10-26
  • 2021-05-16
相关资源
相似解决方案