如果有超过300行,如何在c#中编写存储过程

时间:2012-12-12 11:34:18

标签: c# sql stored-procedures

如何在C#

中编写以下存储过程

我正在创建类库文件。我得到一个情况......不要将scirpt文件提供给客户......所以我必须在c#代码中创建SP。我非常想把它保存在C#程序集中 让我知道这是可能的

IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[AdjustTax]') AND type in (N'P', N'PC'))

DROP PROCEDURE [dbo].[AdjustTax]

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO
CREATE PROCEDURE [dbo].[AdjustTax] 

    @IN_SOPType       INT,
    @IN_SOPNo         VARCHAR(21),
    @IN_AdjustAmount  NUMERIC(19,5),
    @O_iError         INT OUTPUT  

AS

BEGIN

  DECLARE @Sequence       INT,

          @FunctionalAmt  NUMERIC(19,5),
          @OriginalAmt    NUMERIC(19,5),
          @TaxDetail      VARCHAR(30),
          @TaxAmt         NUMERIC(19,5),
          @CreditAmt      NUMERIC(19,5),
          @DexRowIDSOP    INT,
          @SOPHdrTbl      VARCHAR(20)  

    SET NOCOUNT ON;

BEGIN TRANSACTION

    SET @O_iError = 0  

    SELECT @SOPHdrTbl = RTRIM(DB_NAME()) + '.dbo.SOP10100'


    SELECT @DexRowIDSOP = DEX_ROW_ID 

      FROM SOP10100 (nolock)

        WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo


    IF (EXISTS(SELECT 1 FROM tempdb..DEX_LOCK (NOLOCK) WHERE table_path_name = @SOPHdrTbl AND row_id = @DexRowIDSOP))

    BEGIN  

      SET @O_iError = 2022 

    END

    ELSE

    BEGIN   

      SET @FunctionalAmt = @IN_AdjustAmount

      SET @OriginalAmt   = @IN_AdjustAmount     

      UPDATE SOP10100 

        SET TAXAMNT  = TAXAMNT   + @FunctionalAmt,

            DOCAMNT  = DOCAMNT   + @FunctionalAmt,

            ACCTAMNT = ACCTAMNT  + @FunctionalAmt,

            ORTAXAMT = ORTAXAMT  + @OriginalAmt, 

            ORDOCAMT = ORDOCAMT  + @OriginalAmt,  

            ORACTAMT = ORACTAMT  + @OriginalAmt  

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo  

      SELECT TOP 1 @TaxDetail = TAXDTLID, @TaxAmt = STAXAMNT

        FROM SOP10105

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo

        ORDER BY LNITMSEQ


      UPDATE SOP10105

        SET STAXAMNT = STAXAMNT + @FunctionalAmt,

            ORSLSTAX = ORSLSTAX + @OriginalAmt      -- Currently the same as Functional Amount

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo 

            AND LNITMSEQ=0

            AND TAXDTLID=@TaxDetail     

      SELECT TOP 1 @Sequence = SEQNUMBR, @CreditAmt=CRDTAMNT

        FROM SOP10102

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo

            AND DISTTYPE=9 

            AND ((DEBITAMT + CRDTAMNT) = @TaxAmt) 


      IF (@CreditAmt <> 0) 
      BEGIN

        UPDATE SOP10102

          SET CRDTAMNT = CRDTAMNT + @FunctionalAmt,

              ORCRDAMT = ORCRDAMT + @OriginalAmt  

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo

            AND SEQNUMBR=@Sequence
      END

      ELSE

      BEGIN 

        UPDATE SOP10102

          SET DEBITAMT = DEBITAMT + @FunctionalAmt,

              ORDBTAMT = ORDBTAMT + @OriginalAmt    

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo

            AND SEQNUMBR=@Sequence
      END 

      SELECT TOP 1 @Sequence = SEQNUMBR, @CreditAmt=CRDTAMNT
        FROM SOP10102

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo
            AND DISTTYPE=2 AND ((DEBITAMT + CRDTAMNT) > 0.0)

      IF (@CreditAmt <> 0) 
      BEGIN
        UPDATE SOP10102

          SET CRDTAMNT = CRDTAMNT + @FunctionalAmt,
              ORCRDAMT = ORCRDAMT + @OriginalAmt   

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo
            AND SEQNUMBR=@Sequence
      END
      ELSE
      BEGIN 
        UPDATE SOP10102

          SET DEBITAMT = DEBITAMT + @FunctionalAmt,

              ORDBTAMT = ORDBTAMT + @OriginalAmt   

          WHERE SOPTYPE=@IN_SOPType AND SOPNUMBE=@IN_SOPNo

            AND SEQNUMBR=@Sequence
      END            
    END

IF (@O_iError = 0)

  COMMIT TRANSACTION

ELSE

  ROLLBACK TRANSACTION

END

GO

2 个答案:

答案 0 :(得分:5)

为什么你首先用C#写它?

如果要管理数据库架构,请使用 Visual Studio数据库项目。

如果您迫切希望将其保留在C#程序集中,请创建一个单独的.sql文件,将其作为资源嵌入,并将其作为字符串加载到C#中。

在文件属性中将构建操作更改为嵌入式资源并使用以下代码加载

using (Stream stream = Assembly.GetExecutingAssembly()
                               .GetManifestResourceStream("Your assembly namespace" + "file.sql"))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

答案 1 :(得分:0)

300行SP很好,只要它清晰易懂

对于大部分SP,最好写一些评论