티스토리 뷰

// 저장 프로시저 방식
public async Task<long> InsertWordMatrix(WordMatrixEntity wordMatrixEntity) {
    SqlParameter p0 = new SqlParameter("@wordSeq", wordMatrixEntity.WordSeq ?? "");
    SqlParameter p1 = new SqlParameter("@wordMatrixGroupSeq", 0);
    p1.Direction = ParameterDirection.Output;

    using (var context = new SQLDBContext()) {
        string sp = "exec up_WordMatrix_insert @wordSeq, @wordMatrixGroupSeq out";
        await context.Database.ExecuteSqlRawAsync(sp, p0, p1);
        
        return (long)p1.Value;
    }
}

/*  
    Author      :   장정훈
    Create date :   2024-03-19
    Description :	WordMatrix 정보를 등록합니다.
    Related page:   
    Test db     :   exec dbo.up_WordMatrix_insert '189,190'
    Real db     :   declare @wordMatrixGroupSeq bigint
					exec dbo.up_WordMatrix_insert '189,190', @wordMatrixGroupSeq output
					print @wordMatrixGroupSeq

    History     :	2024-03-19 장정훈 #150275:create
*/ 
create PROCEDURE [dbo].[up_WordMatrix_insert]
	@wordSeq varchar(4000)
,	@wordMatrixGroupSeq bigint output
AS
begin
	set nocount on
	set transaction isolation level read uncommitted
	
	set @wordMatrixGroupSeq = next value for dbo.WordMatrixGroup

	insert into dbo.WordMatrix (
		WordMatrixGroupSeq
	,	WordSeq
	)
	select
		 @wordMatrixGroupSeq
	,	cast(value as bigint)
	from string_split(@wordSeq, ',')

end

// 쿼리문 방식
public async Task<long> SetWord(WordEntity wordEntity)
{
    using (var context = new SQLDBContext()) 
    {
        SqlParameter p4 = new SqlParameter("@p4", SqlDbType.BigInt);
        p4.Direction = ParameterDirection.Output;

        string query = $"""
            insert into dbo.Word (
                Spelling
                , Meaning
                , NationSeq
                , Level
            ) values (
                @p0
                , @p1
                , @p2
                , @p3
            );
            
            set @p4 = scope_identity();
        """;

        await context.Database.ExecuteSqlRawAsync(query, 
            wordEntity.Spelling??""
            , wordEntity.Meaning??""
            , wordEntity.NationSeq
            , wordEntity.Level??""
            , p4
        );

        return (long)p4.Value;
    }
}
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함