[英]Data is not inserted into the SQL Server table
I am developing a project and I have a difficulty in the part of SQL Server, I created a procedure, and I need that when the value hum in the table Bi_medicao
is greater than 10 he put that line in the other table Bi_alerta
but the the problem is that when I run the code everything is fine, but when I see it it doesn't put anything in the table... Just don't add any rows to the Bi_alerta
table.我正在开发一个项目,我在 SQL 服务器的部分有困难,我创建了一个程序,我需要当表
Bi_medicao
中的值 hum 大于 10 时,他将该行放在另一个表Bi_alerta
但问题是,当我运行代码时,一切都很好,但是当我看到它时,它并没有在表中添加任何内容......只是不要在Bi_alerta
表中添加任何行。
SQL Server stored procedure: SQL 服务器存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[Bi_alertainset]
@alerta_hum varchar(50),
@alerta_temp varchar(50),
@alerta_lpg varchar(50),
@alerta_co varchar(50),
@alerta_fumo varchar(50)
AS
BEGIN
SELECT
@alerta_hum = med_hum,
@alerta_temp = med_temp,
@alerta_lpg = med_lpg,
@alerta_co = med_co,
@alerta_fumo = med_fumo
FROM
Bi_medicao
IF (@alerta_hum > 10)
INSERT INTO Bi_alerta (alerta_hum, alerta_temp, alerta_lpg, alerta_co, alerta_fumo)
VALUES (@alerta_hum, @alerta_temp, @alerta_lpg, @alerta_co, @alerta_fumo)
END
It's difficult to reproduce without any sample data, table structure and so on.没有任何样本数据、表结构等,很难重现。 Anyways, I would suggest to modify the insert as follows:
无论如何,我建议修改插入如下:
INSERT INTO Bi_alerta (alerta_hum, alerta_temp, alerta_lpg, alerta_co, alerta_fumo)
SELECT med_hum, med_temp, med_lpg, med_co, med_fumo
FROM Bi_medicao
WHERE CAST(med_hum AS INT) > 10
I think the problem could be that @alerta_hum is Varchar and not INT.我认为问题可能是@alerta_hum 是 Varchar 而不是 INT。 Either you change the declaration to INT or you CONVERT your @alerta_hum Also, I would recommend using BEGIN/END for IF as well.
要么将声明更改为 INT,要么转换您的 @alerta_hum 另外,我建议也将 BEGIN/END 用于 IF。
IF (CONVERT(INT, @alerta_hum) > 10)
BEGIN
INSERT INTO Bi_alerta (alerta_hum, alerta_temp, alerta_lpg, alerta_co, alerta_fumo)
VALUES (@alerta_hum, @alerta_temp, @alerta_lpg, @alerta_co, @alerta_fumo)
END
If that still doesn't work, you should check if you variables are all asigned after the reset with Bi_medicao.如果这仍然不起作用,您应该检查您的变量是否在使用 Bi_medicao 重置后全部分配。 Simples way would be to insert some PRINT Statements like PRINT @alerta_hum
简单的方法是插入一些 PRINT 语句,如 PRINT @alerta_hum
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.