zoukankan      html  css  js  c++  java
  • 游标 循环插入值

     1  DECLARE @AccessRightFK VARCHAR(500)
     2  DECLARE @UserGroupFK uniqueidentifier
     3 
     4  DECLARE UserGroup_Cursor CURSOR FOR
     5  
     6  SELECT AccessRightFK,UserGroupFK
     7  FROM sec.UserGroup_AccessRight ugar WHERE ugar.AccessRightFK = 'Client/Module/QuotationTemplate'
     8 
     9 
    10  
    11  OPEN UserGroup_Cursor
    12  
    13  FETCH NEXT FROM UserGroup_Cursor 
    14  INTO @AccessRightFK, @UserGroupFK
    15  
    16  WHILE @@FETCH_STATUS = 0
    17  BEGIN     
    18     DECLARE @AccessRight nvarchar(100)
    19     DECLARE @RecordCount int = 0
    20 
    21      -- Find if exist
    22     SELECT @RecordCount=COUNT(1)
    23     FROM sec.UserGroup_AccessRight ugar
    24     WHERE ugar.UserGroupFK=@UserGroupFK AND ugar.AccessRightFK = 'Client/Module/CaseFolder/QuotationView'
    25 
    26     -- Find AccessRight
    27     SELECT @AccessRight = ugar.AccessRight
    28     FROM sec.UserGroup_AccessRight ugar
    29     WHERE ugar.UserGroupFK=@UserGroupFK AND ugar.AccessRightFK = @AccessRightFK
    30 
    31     -- Insert if not exist
    32      IF @RecordCount = 0
    33     BEGIN
    34         INSERT INTO SEC.UserGroup_AccessRight
    35         (
    36             AccessRightFK,
    37             UserGroupFK,
    38             AccessRight
    39         )
    40         VALUES
    41         (
    42             'Client/Module/CaseFolder/QuotationView', -- AccessRightFK - varchar
    43             @UserGroupFK, -- UserGroupFK - uniqueidentifier
    44             @AccessRight -- AccessRight - varchar
    45         )
    46     END
    47      
    48       FETCH NEXT FROM UserGroup_Cursor 
    49     INTO @AccessRightFK, @UserGroupFK
    50  END 
    51  
    52  
    53  CLOSE UserGroup_Cursor;
    54  DEALLOCATE UserGroup_Cursor;
    55 
    56  
  • 相关阅读:
    double相加(減)结果会有些误差
    创建表,操作表
    删除数据库
    DDL语句
    HCDA-12-配置基本静态路由
    HCDA-11-配置直连路由
    1-5配置STelnet登录系统
    HCDA-9-管理设备文件系统
    HCDA-8-了解设备系统文件
    Java生成随机汉字
  • 原文地址:https://www.cnblogs.com/zqt14520/p/12463955.html
Copyright © 2011-2022 走看看