0

VB.NetとSQLServerの使用

表1

Id             Value .....

1001P0010001   100
1001P0010002   200
1001P0010003   300
1001P0010004   400
...

にn個の列と行がありtable1、から、table1すべての列の詳細を新しいID番号でコピーしたい...

idnoはこのようなものです1001P0020001, 1001P0020002, .......

P002次のID、P003次のID....。

最初の4桁と最後の4桁は読み取ったままでtable1、中央の4桁は次のシリーズに変更する必要があります

期待される出力

Id             Value .....

1001P0010001   100
1001P0010002   200
1001P0010003   300
1001P0010004   400
1001P0020001   100
1001P0020002   200
1001P0020003   300
1001P0020004   400
...

これを行うための最良の方法はどれですか?

VB.NetまたはSQLクエリで実行できます...?これを行う方法を提案してください。

4

2 に答える 2

1
CREATE TABLE CocoJambo (
    Id  CHAR(12) NOT NULL,
    Value INT NULL,
    CHECK( Id LIKE '[0-9][0-9][0-9][0-9][A-Z][0-9][0-9][0-9][0-9][0-9][0-9][0-9]' )
);
GO

CREATE UNIQUE INDEX IUN_CocoJambo_Id
ON CocoJambo (Id);
GO

INSERT  CocoJambo (Id, Value)
          SELECT '1001P0010001', 100
UNION ALL SELECT '1001P0010002', 200
UNION ALL SELECT '1001P0010003', 300
UNION ALL SELECT '1001P0010004', 400
UNION ALL SELECT '1001P0020001', 100
UNION ALL SELECT '1001P0020002', 200
UNION ALL SELECT '1001P0020003', 300
UNION ALL SELECT '1001P0020004', 400;
GO

-- Test 1: generating a single Id
DECLARE @Prefix CHAR(5),
        @Sufix CHAR(4);
SELECT  @Prefix = '1001P',
        @Sufix = '0001';

BEGIN TRAN

DECLARE @LastGeneratedMiddleValue  INT,
        @LastValue INT;

SELECT  @LastGeneratedMiddleValue = y.MiddleValue,
        @LastValue = y.Value
FROM 
    (
    SELECT  x.MiddleValue, x.Value,
            ROW_NUMBER() OVER(ORDER BY x.MiddleValue DESC) AS RowNum
    FROM 
        (
        SELECT  CONVERT(INT,SUBSTRING(a.Id,6,3)) AS MiddleValue, a.Value
        FROM    CocoJambo a WITH(UPDLOCK) -- It will lock the rows (U lock) during transaction
        WHERE   a.Id LIKE @Prefix+'%'+@Sufix
        ) x
    ) y
WHERE   y.RowNum=1;

SELECT  @LastGeneratedMiddleValue  = ISNULL(@LastGeneratedMiddleValue ,0)
SELECT  @Prefix
        +RIGHT('00'+CONVERT(VARCHAR(3),@LastGeneratedMiddleValue +1),3)
        +@Sufix AS MyNewId,
        @LastValue AS Value

COMMIT TRAN;
GO

-- Test 2: generating many Id's
BEGIN TRAN

DECLARE @Results TABLE (
    Prefix CHAR(5) NOT NULL,
    Sufix CHAR(4) NOT NULL,
    LastGeneratedMiddleValue  INT NOT NULL,
    LastValue INT NULL
);
INSERT  @Results (Prefix, Sufix, LastGeneratedMiddleValue, LastValue)
SELECT  y.Prefix, y.Sufix, y.MiddleValue, y.Value
FROM 
    (
    SELECT  x.Prefix, x.MiddleValue, x.Sufix, x.Value,
            ROW_NUMBER() OVER(PARTITION BY x.Prefix, x.Sufix ORDER BY x.MiddleValue DESC) AS RowNum
    FROM 
        (
        SELECT  SUBSTRING(a.Id,1,5) AS Prefix,
                CONVERT(INT,SUBSTRING(a.Id,6,3)) AS MiddleValue,
                SUBSTRING(a.Id,9,4) AS Sufix,
                a.Value
        FROM    CocoJambo a WITH(UPDLOCK) -- It will lock the rows (U lock) during transaction
        ) x
    ) y
WHERE   y.RowNum=1;

SELECT  r.*, 
        r.Prefix
        +RIGHT('00'+CONVERT(VARCHAR(3),r.LastGeneratedMiddleValue +1),3)
        +r.Sufix AS MyNewId,
        r.LastValue AS Value
FROM    @Results r;

COMMIT TRAN;
GO
于 2012-10-13T10:45:52.010 に答える
0
insert into table1 (id, value)
select 
    l +
    replicate('0', 3 - lenght(m)) + m +
    r,
    value
from (
    select 
        left(id, 5) l,
        cast(cast(substring(id, 6, 3) as integer) + 1 as varchar(3)) m,
        right(id, 4),
        value
    from table1
) s
于 2012-10-13T09:53:53.340 に答える