SQL 存储过程可以工作,但有错误
我有一个包含 3 个主键的表:CODE_TARIF、UNITE
和 MODE_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
猜测...
您正在使用 ISO 游标语法。这默认为非 INSENSITIVE 。这意味着当您插入行时,当您 FETCH 时,您将获得刚刚插入的行等。
从 DECLARE CURSOR
在任何情况下,您所需要的就是:不需要循环
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
In any event, all you need is this: No need to loop