简体   繁体   English

SQL - 尝试使用 while 循环向表中添加行,但没有添加任何行,我也没有收到错误消息

[英]SQL - Trying to add rows to a table with while loop, but no rows get added and I get no error message

I am trying to add rows of new IP addresses to an existing table called IP Alloc, but no rows get added.我正在尝试将新 IP 地址行添加到名为 IP Alloc 的现有表中,但没有添加任何行。 I dont get errors either.我也没有收到错误。 What is happening?怎么了?

SELECT 
    *
FROM
    IP_Alloc
BEGIN
         
    DECLARE @IPaddress VARCHAR
    SET @IPaddress = '.'

    DECLARE @IPoct1 VARCHAR
    SET @IPoct1 = 10

    DECLARE @IPoct2 VARCHAR
    SET @IPoct2 = 200

    DECLARE @IPoct3 VARCHAR
    SET @IPoct3 = 20

    DECLARE @IPoct4 INT
    SET @IPoct4 = 0
END

WHILE (@IPoct4 < 256)
BEGIN
    SET @IPaddress = CONCAT(@IPoct1,'.', @IPoct2,'.' , @IPoct3,'.', @IPoct4);

    INSERT INTO IP_Alloc (site, area, line, Device, deviceDescription, PartNo, IPaddress, deviceType, MACaddress, comment, IPoct1, IPoct2, IPoct3, IPoct4, created, hostName, modelNo, modified, revision, series, switchPort, visible, wallPlate)
--Generic values, to be changed later               
    VALUES ('BIL', 'area', 'line', 'Device', 'deviceDescription', 'PartNo', @IPaddress, 'deviceType', 'MACaddress','comment', @IPoct1, @IPoct2, @IPoct3, @IPoct4, '0000-00-00', 'hostName', 'modelNo', '0000-00-00', 'revision', 'series', 'switchPort', '0', 'wallPlate')
               
    SET @IPoct4 = @IPoct4 + 1 
END

So you have some issues with your data type declarations.因此,您的数据类型声明存在一些问题。 @IPoct4 needs to be an int as you have already to be able to iterate through the variable. @IPoct4 需要是一个 int ,因为您已经能够遍历该变量。

@IPaddress needs to be VARCHAR as it has a . @IPaddress 需要是 VARCHAR,因为它有一个 . in however you need to add the length declaration.但是,您需要添加长度声明。 If its not specified the length defaults to 1. VARCHAR(15) will work sufficiently for an ip address.如果未指定,则长度默认为 1。 VARCHAR(15) 将足以用于 ip 地址。

Then for @IPoct1,@IPoct2,@IPoct3 you're currently specifying these as varchar however you're setting them using a number without speech marks.然后对于@IPoct1、@IPoct2、@IPoct3,您当前将它们指定为 varchar,但是您使用不带语音标记的数字设置它们。 You can only do that when your defining INTs.您只能在定义 INT 时这样做。 You're much better off defining it as an int你最好把它定义为一个 int

You also have a begin and end which isn't needed either.你也有一个也不需要的开始和结束。 So your Query would turn into:所以你的查询会变成:

DECLARE @IPaddress VARCHAR(15)
SET @IPaddress = '.'

DECLARE @IPoct1 INT
SET @IPoct1 = 10

DECLARE @IPoct2 INT
SET @IPoct2 = 200

DECLARE @IPoct3 INT
SET @IPoct3 = 20

DECLARE @IPoct4 INT
SET @IPoct4 = 0

WHILE (@IPoct4 < 256)
BEGIN
    SET @IPaddress = CONCAT(@IPoct1,'.', @IPoct2,'.' , @IPoct3,'.', @IPoct4);

    INSERT INTO IP_Alloc (site, area, line, Device, deviceDescription, PartNo, IPaddress, deviceType, MACaddress, comment, IPoct1, IPoct2, IPoct3, IPoct4, created, hostName, modelNo, modified, revision, series, switchPort, visible, wallPlate)
--Generic values, to be changed later               
    VALUES ('BIL', 'area', 'line', 'Device', 'deviceDescription', 'PartNo', @IPaddress, 'deviceType', 'MACaddress','comment', @IPoct1, @IPoct2, @IPoct3, @IPoct4, '0000-00-00', 'hostName', 'modelNo', '0000-00-00', 'revision', 'series', 'switchPort', '0', 'wallPlate')
               
    SET @IPoct4 = @IPoct4 + 1 
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM