简体   繁体   English

将值传递给表值参数时忽略身份列

[英]Ignoring identity column when passing value to a table-valued parameter

I am trying to pass values to a table-valued parameter of a stored procedure like this: 我试图将值传递给这样的存储过程的表值参数:

DataTable Entries = new DataTable();
Entries.Columns.Add("InfoID", typeof (int));
Entries.Columns.Add("InfoValue", typeof (string));
foreach(FormInfoValue entry in FormEntries) {
    Entries.Rows.Add(entry.InfoID, entry.InfoValue);
}
DataSet res = ExecuteStoredProcedure("SaveData", new SqlParameter[] {
    new SqlParameter() {
        DbType = DbType.String, ParameterName = "@FormID", Value = FormID
    },
    new SqlParameter() {
        SqlDbType = SqlDbType.Structured, ParameterName = "@InfoValues", Value = Entries
    },
    new SqlParameter() {
        DbType = DbType.Int32, ParameterName = "@UserID", Value = Security.SessionControl.GetSession().UserID
    }
});
return int.Parse(res.Tables[0].Rows[0][0].ToString());

But the problem is that the Table-valued parameter @InfoValues has one extra column that is identity. 但是问题是表值参数@InfoValues有一列是身份。 I am using that column in my logic in stored procedure. 我在存储过程的逻辑中使用该列。 But at C# its throwing exception of having 3 columns instead of 2 in @InfoValues . 但是在C#中,它抛出的异常是@InfoValues中有3列而不是2列。 How can I pass values by ignoring identity column in this table-valued parameter? 如何通过忽略此表值参数中的标识列来传递值?

UPDATE UPDATE

I tried to use a temp table as a solution like this: 我试图使用临时表作为这样的解决方案:

DECLARE @FormData TABLE (
    ID INT IDENTITY(1,1) NOT NULL,
    InfoID INT NULL,
    InfoValue NVARCHAR(MAX) NULL
    )
INSERT INTO @FormData(InfoID,InfoValue) SELECT [InfoID],[InfoValue] FROM @InfoValues
        WHILE (1 = 1)
        BEGIN
            EXEC [dbo].OpenKeys
            SELECT TOP 1 @iid = [ID], @id = InfoID, @value = InfoValue FROM @FormData WHERE [ID] > @iid ORDER BY [ID]
            -- Exit loop if no more info values
            IF @@ROWCOUNT = 0 BREAK;

            INSERT INTO [InfoValues]([InfoID],[Value],[UserID],[DateAdded],[DateUpdated]) 
            VALUES(@id,[dbo].ENCRYPTDATA(@value),@UserID,GETDATE(), GETDATE()); 
            SET @retID = SCOPE_IDENTITY();
            INSERT INTO EVMap(EntryID, ValueID) VALUES(@EntryID, @retID)
        END

Is this approach efficient? 这种方法有效吗?

I of course can't test this but it at least parses. 我当然无法测试,但至少可以解析。 Here is an example of how this might look using OUTPUT instead of looping. 这是一个使用OUTPUT而不是循环的外观示例。 This assume that some of the variable are already defined and you receive @InfoValues as a table valued parameter. 假定某些变量已经定义,并且您收到@InfoValues作为表值参数。

EXEC [dbo].OpenKeys

INSERT INTO [InfoValues]
(
    [InfoID]
    , [Value]
    , [UserID]
    , [DateAdded]
    , [DateUpdated]
) 
OUTPUT @EntryID      -- I assume this is set somewhere else in your code
    , INSERTED.RetID -- or whatever column is your identity in InfoValues
INTO EVMap(EntryID, ValueID)
SELECT InfoID
    , [dbo].ENCRYPTDATA(InfoValue)
    , @UserID -- I assume this is set somewhere else in your code
    , GETDATE()
    , GETDATE()
FROM @InfoValues

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM