SQL 存储过程可以工作,但有错误

发布于 2024-12-11 05:04:02 字数 1598 浏览 0 评论 0原文

我有一个包含 3 个主键的表:CODE_TARIF、UNITEMODE_LIV

我编写了一个存储过程来复制和粘贴,但使用不同的 MODE_LIV

例如:如果我在表 T_TARIF 中已经有 2 行 MODE_LIV = 2,,当我使用输入 MODE_LIV =3 运行此存储过程时,我将有 4 行。

ALTER PROCEDURE [dbo].[Copy_Tarif]
    -- Add the parameters for the stored procedure here
  @MODE_LIV int
AS
BEGIN
    DECLARE @CODE_TARIF varchar(15)
    DECLARE @ZONE int
    DECLARE @UNITE int
    DECLARE @LIBELLE varchar(30)

    DECLARE @TR_DEB int
    DECLARE @TR_FIN int
    DECLARE @MONTANT decimal(18,2)


    DECLARE tarif_cursor CURSOR FOR     
    SELECT CODE_TARIF, ZONE, UNITE, LIBELLE, TR_DEBUT, TR_FIN, MONTANT
    FROM T_TARIF

    OPEN tarif_cursor;

    FETCH NEXT FROM tarif_cursor 
    INTO  @CODE_TARIF, @ZONE, @UNITE, @LIBELLE,  @TR_DEB, @TR_FIN, @MONTANT;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO [T_TARIF]
           ([CODE_TARIF]
           ,[ZONE]
           ,[UNITE] 
           ,[MODE_LIV]         
           ,[LIBELLE]
           ,[TR_DEBUT]
           ,[TR_FIN]
           ,[MONTANT]

           )
     VALUES
           (@CODE_TARIF
           ,@ZONE
           ,@UNITE   
           ,@MODE_LIV       
           ,@LIBELLE
           ,@TR_DEB
           ,@TR_FIN
           ,@MONTANT

           )

        FETCH NEXT FROM tarif_cursor
        INTO  @CODE_TARIF, @ZONE, @UNITE, @LIBELLE,  @TR_DEB, @TR_FIN, @MONTANT;



    END
END

它有效,但出现错误,请参阅视频:奇怪的存储过程

提前谢谢您, 斯蒂夫

I have table with 3 primary keys : CODE_TARIF, UNITE, and MODE_LIV.

I write a stored procedure to copy and paste but with different MODE_LIV.

ex: if I already have 2 rows in table T_TARIF with MODE_LIV = 2, when I run this stored procedure with input MODE_LIV =3, I will have 4 rows .

ALTER PROCEDURE [dbo].[Copy_Tarif]
    -- Add the parameters for the stored procedure here
  @MODE_LIV int
AS
BEGIN
    DECLARE @CODE_TARIF varchar(15)
    DECLARE @ZONE int
    DECLARE @UNITE int
    DECLARE @LIBELLE varchar(30)

    DECLARE @TR_DEB int
    DECLARE @TR_FIN int
    DECLARE @MONTANT decimal(18,2)


    DECLARE tarif_cursor CURSOR FOR     
    SELECT CODE_TARIF, ZONE, UNITE, LIBELLE, TR_DEBUT, TR_FIN, MONTANT
    FROM T_TARIF

    OPEN tarif_cursor;

    FETCH NEXT FROM tarif_cursor 
    INTO  @CODE_TARIF, @ZONE, @UNITE, @LIBELLE,  @TR_DEB, @TR_FIN, @MONTANT;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        INSERT INTO [T_TARIF]
           ([CODE_TARIF]
           ,[ZONE]
           ,[UNITE] 
           ,[MODE_LIV]         
           ,[LIBELLE]
           ,[TR_DEBUT]
           ,[TR_FIN]
           ,[MONTANT]

           )
     VALUES
           (@CODE_TARIF
           ,@ZONE
           ,@UNITE   
           ,@MODE_LIV       
           ,@LIBELLE
           ,@TR_DEB
           ,@TR_FIN
           ,@MONTANT

           )

        FETCH NEXT FROM tarif_cursor
        INTO  @CODE_TARIF, @ZONE, @UNITE, @LIBELLE,  @TR_DEB, @TR_FIN, @MONTANT;



    END
END

It works, but gives an error see Video : Strange Stored Procedure

Thanks you in advance,
Stev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萧瑟寒风 2024-12-18 05:04:02

猜测...

您正在使用 ISO 游标语法。这默认为非 INSENSITIVE 。这意味着当您插入行时,当您 FETCH 时,您将获得刚刚插入的行等。

从 DECLARE CURSOR

声明光标名称 [ INSENSITIVE ] [ SCROLL ] CURSOR

定义一个游标,该游标制作游标使用的数据的临时副本。所有对游标的请求都从tempdb中的这个临时表得到应答;因此,对基表所做的修改不会反映在对该游标进行提取所返回的数据中,并且该游标不允许修改。 使用 ISO 语法时,如果省略 INSENSITIVE,则(任何用户)对基础表所做的已提交删除和更新将反映在后续提取中。

在任何情况下,您所需要的就是:不需要循环

 INSERT INTO [T_TARIF]
       ([CODE_TARIF]
       ,[ZONE]
       ,[UNITE] 
       ,[MODE_LIV]         
       ,[LIBELLE]
       ,[TR_DEBUT]
       ,[TR_FIN]
       ,[MONTANT])
 SELECT 
       [CODE_TARIF]
       ,[ZONE]
       ,[UNITE] 
       ,@MODE_LIV       
       ,[LIBELLE]
       ,[TR_DEBUT]
       ,[TR_FIN]
       ,[MONTANT]
 FROM
      [T_TARIF]
      

Guessing...

You are using ISO cursor syntax. This defaults to not INSENSITIVE . Which means as you insert rows then when you FETCH you get rows you have just inserted etc etc.

From DECLARE CURSOR

DECLARE cursor_name [ INSENSITIVE ] [ SCROLL ] CURSOR

Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications. When ISO syntax is used, if INSENSITIVE is omitted, committed deletes and updates made to the underlying tables (by any user) are reflected in subsequent fetches.

In any event, all you need is this: No need to loop

 INSERT INTO [T_TARIF]
       ([CODE_TARIF]
       ,[ZONE]
       ,[UNITE] 
       ,[MODE_LIV]         
       ,[LIBELLE]
       ,[TR_DEBUT]
       ,[TR_FIN]
       ,[MONTANT])
 SELECT 
       [CODE_TARIF]
       ,[ZONE]
       ,[UNITE] 
       ,@MODE_LIV       
       ,[LIBELLE]
       ,[TR_DEBUT]
       ,[TR_FIN]
       ,[MONTANT]
 FROM
      [T_TARIF]
      
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文