最多加1。行数

时间:2016-02-25 13:42:28

标签: sql

我试图通过从另一个表中选择值来在表中插入值,我给每个值一个唯一的id,所以我选择给它们行号但是在更新另一个表时我不希望所有的值都是再次选择,以获得更多性能,所以我只想添加新值。所以我每次添加新员工时都必须在另一个表中再次添加所有员工。那么我该怎么做才能获得最大行数并为它们添加1以获得这个新员工的新唯一ID?

Select    1 , -- FormNo - smallint
      334 , -- FieldNo - smallint
      RowNum = ROW_NUMBER() OVER (ORDER BY MAX(EmpId)+1) , -- FieldItemNo - smallint
      CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId)+'-'+profile.name) AS NVARCHAR(40)) , -- FieldItemValue - nvarchar(40)
      CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) , -- FieldItemAValue - nvarchar(40)
      CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId)+'-'+profile.name) AS NVARCHAR(40))  -- FieldItemFValue - nvarchar(40)
     FROM dbo.EmpAssignment 
    INNER JOIN profile ON Profile.ProfileId = EmpAssignment.EmpId 
    WHERE EmpAssignment.Status=1 AND
     (CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) NOT IN (SELECT FieldItemAValue FROM dbo.UserFieldsItems WHERE FieldNo = 334))
     GROUP BY ROW_NUMBER() OVER ( ORDER BY MAX(EmpId) + 1 ) ,
              CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId) + '-' + profile.name) AS NVARCHAR(40)) ,
              CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) ,
              CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId) + '-' + profile.name) AS NVARCHAR(40))
              ORDER BY CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId)+'-'+profile.name) AS NVARCHAR(40))

这段代码可以让我成为新员工:

Select    1 , -- FormNo - smallint
      334 , -- FieldNo - smallint
      ROW_NUMBER() OVER (ORDER BY EmpId) , -- FieldItemNo - smallint
      CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId)+'-'+profile.name) AS NVARCHAR(40)) , -- FieldItemValue - nvarchar(40)
     CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) , -- FieldItemAValue - nvarchar(40)
      CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId)+'-'+profile.name) AS NVARCHAR(40))  -- FieldItemFValue - nvarchar(40)
     FROM dbo.EmpAssignment 
    INNER JOIN profile ON Profile.ProfileId = EmpAssignment.EmpId 
    WHERE EmpAssignment.Status=1
    AND (CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) NOT IN (SELECT FieldItemAValue FROM dbo.UserFieldsItems WHERE FormNo = 1 AND FieldNo = 334))

但行数为1,因此会与第一名员工发生冲突

2 个答案:

答案 0 :(得分:0)

如果它的SQL Server然后将unique_id作为标识,如identity(10000,1),那么第一个值从10000开始,然后在每个插入时递增1。另外,不要为Unique_id选择值,插入时会自动更新。

https://www.simple-talk.com/sql/t-sql-programming/identity-columns/

答案 1 :(得分:0)

这是我从我的主管那里得到的答案,希望有一天可能会帮助某人。

 INSERT INTO dbo.UserFieldsItems
    ( FormNo ,
      FieldNo ,
      FieldItemNo ,
      FieldItemValue ,
      FieldItemAValue ,
      FieldItemFValue 
    )
SELECT  1 , -- FormNo - smallint
    334 , -- FieldNo - smallint
    ( SELECT MAX(dbo.UserFieldsItems.FieldItemNo)FROM dbo.UserFieldsItems) + ROW_NUMBER() OVER ( ORDER BY EmpId ) , -- FieldItemNo - smallint
    CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId) + '-' + Profile.Name) AS NVARCHAR(40)) , -- FieldItemValue - nvarchar(40)
    CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) , -- FieldItemAValue - nvarchar(40)
    CAST(LTRIM(STR(dbo.EmpAssignment.EmployeeId) + '-' + Profile.Name) AS NVARCHAR(40))  -- FieldItemFValue - nvarchar(40)
FROM    dbo.EmpAssignment
    INNER JOIN Profile ON Profile.ProfileId = EmpAssignment.EmpId
WHERE   EmpAssignment.Status = 1
    AND ( CAST(dbo.EmpAssignment.EmployeeId AS NVARCHAR(40)) NOT IN (
          SELECT    FieldItemAValue
          FROM      dbo.UserFieldsItems
          WHERE     FormNo = 1
                    AND FieldNo = 334 ) );