t-sql将数据插入加密列

bkhjykvo  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(268)

基于大量的研究和本文档,我下面的存储过程应该可以工作。但是,无论是直接在sqlserver17中调用还是通过ado连接调用,我都会遇到这个错误。
不允许从数据类型varchar隐式转换为varchar(255),使用(加密类型='deterministic',加密算法\u name='aead \u aes \u 256 \u cbc \u hmac \u sha \u 256',列\u encryption \u key \u name='autostripes \u machine \u cek',列\u encryption \u key \u database \u name='autostripes')排序规则\u name='sql \u latin1 \u general \u cp1 \u ci as'加密。
我有 Column Encryption Setting = Enabled 并为始终加密的签入查询选项启用参数化,并且我在使用的计算机上有密钥。
存储过程如下。我使用cursor是因为不可能加密临时表afaik,所以我想参数化vin和where条件,以便在插入记录后为这些字段执行update语句。

USE AutoSTripes;
GO 

CREATE PROCEDURE [dbo].[loadVehicles] (@xmlString XML)

AS 

BEGIN

    DECLARE @tempVehs TABLE 
    (
        [ProductNo] [bigint],
        [VersionNo] [bigint],
        [DateAdded] [smalldatetime],
        [UserID] [varchar](255),
        [VehicleYear] [varchar](255),
        [Make] [varchar](255),
        [Model] [varchar](255),
        [VINNumber] [varchar](255), -- matches column that is encrypted
        -- .... more columns
        [VehicleSeqNo] [bigint],
        [CollDed] [varchar](255)
)

    INSERT INTO @tempVehs   
    SELECT
         XD.xTbl.value('./Product[1]', 'bigint') as ProductNo
         ,XD.xTbl.value('./Version[1]', 'bigint') as VersionNo
         ,XD.xTbl.value('./DateAdded[1]', 'smalldatetime') as DateAdded
         ,XD.xTbl.value('./UserID[1]', 'varchar(255)') as UserID
         ,XD.xTbl.value('./VehYear[1]', 'varchar(255)') as VehiclYear
         ,XD.xTbl.value('./Make[1]', 'varchar(255)') as Make
         ,XD.xTbl.value('./Model[1]', 'varchar(255)') as Model
         ,XD.xTbl.value('./Vin[1]', 'varchar(255)') as VinNumber
         -- ... more columns
         ,XD.xTbl.value('./SeqNo[1]', 'bigint') as VehicleSeqNo
         ,XD.xTbl.value('./CollDed[1]', 'varchar(255)') as CollDed
    FROM
        @xmlString.nodes('//VehicleList/Vehicle') AS XD(xTbl)

    INSERT INTO dbo.tVehicle
        (ProductNo, VersionNo, DateAdded, UserID, VehicleYear, Make, Model, GLCity, GLState, GLZip, VehicleType, Radius, Class, VehicleSeqNo, PIP, AddlPIP, MedPay, UMUIM, UMPD, OTCCov, OTCDed, ACVorStated, CollCov, CollDed) 
        SELECT
            ProductNo, VersionNo, DateAdded, UserID, VehicleYear, Make, Model, GLCity, GLState, GLZip,  VehicleType, Radius, Class, VehicleSeqNo, PIP, AddlPIP, MedPay, UMUIM, UMPD, OTCCov, OTCDed, ACVorStated, CollCov, CollDed
        FROM
            @tempVehs

    -- loop temp table and update VIN for each unique record    
    DECLARE @vin varchar(255)
    DECLARE @product bigint
    DECLARE @version bigint
    DECLARE @sequence bigint

    DECLARE @curs CURSOR

    BEGIN

        SET @curs = CURSOR FOR SELECT ProductNo, VersionNo,  VinNumber, VehicleSeqNo FROM @tempVehs
        OPEN @curs
        FETCH NEXT FROM @curs INTO @product, @version, @vin, @sequence

        Set @product = @product
        Set @version = @version
        Set @vin = @vin
        Set @sequence = @sequence

        UPDATE dbo.tVehicle SET VINNumber = @vin WHERE ProductNo = @product AND VersionNo = @version and VehicleSeqNo = @sequence

    END

    close @curs
    deallocate @curs

END

GO

我错过了什么?还是根本不可能?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题