插入数据时将数据类型varchar转换为数字时出错

时间:2015-09-24 15:59:10

标签: sql sql-server database

我的第一个insert声明存在问题。我在网上搜索,似乎没有任何帮助我理解这个问题或如何解决它。当我执行insert语句时,它会给我一条消息:

  

将数据类型varchar转换为数字时出错。

表格结构:

CREATE TABLE BOOKS
(
    ISBN             INT            NOT NULL  UNIQUE,
    TITLE            VARCHAR(25)    NOT NULL,
    AuthorFname      VARCHAR(20)    NOT NULL,
    AuthorLname      VARCHAR(20)    NOT NULL,
    GENRE            VARCHAR(10)    NOT NULL,
    PUBLISHER        VARCHAR(30)    NOT NULL,
    PUBLISHERDATE    DATE           NOT NULL,
    DESCRIPTION      VARCHAR(300)   NOT NULL,
    EDITION          varchar(100)   NOT NULL,
    FORMAT           VARCHAR(20)    NOT NULL,
    PAGENUMBER       INTEGER        NOT NULL,
    FILESIZE         TEXT           NOT NULL,
    IMAGE            varchar(20)    NOT NULL,
    THUMBNAIL        varchar(20)    NOT NULL,
    PRICE            DECIMAL(9,2)   NOT NULL,
    AVAILABILITY     VARCHAR(20)    NOT NULL,

    PRIMARY KEY(ISBN),
    FOREIGN KEY(TITLE) REFERENCES WISHLIST(TITLE)
)

CREATE TABLE USERINFORMATION
(
    USEREMAIL        VARCHAR(30)    NOT NULL UNIQUE,
    USERNAME         VARCHAR(30)    NOT NULL,
    PASSWORD         TEXT           NOT NULL,
    QUESTION1        TEXT           NOT NULL,
    QUESTION2        TEXT           NOT NULL,
    ANSWER1          VARCHAR(25)    NOT NULL,
    ANSWER2          VARCHAR(25)    NOT NULL,
    BIRTHDATE        DATE           NOT NULL,
    TITLE            VARCHAR(25)    NOT NULL,

    PRIMARY KEY(USEREMAIL),
    FOREIGN KEY(USERNAME) REFERENCES REVIEWS(USERNAME)
)

CREATE TABLE BILLINGINFO
(
    FIRSTNAME        VARCHAR(20)    NOT NULL UNIQUE,
    LASTNAME         VARCHAR(20)    NOT NULL,
    USERNAME         VARCHAR(20)    NOT NULL,
    ADDRESS1         VARCHAR(35)    NOT NULL,
    ADDRESS2         VARCHAR(35)    NOT NULL,
    CITY             VARCHAR(25)    NOT NULL,
    STATE            VARCHAR(35)    NOT NULL,
    ZIP              INTEGER        NOT NULL,
    PHONE            INTEGER        NOT NULL,
    PAYMENTTYPE      TEXT           NOT NULL,
    CARDNUMBER       INTEGER        NOT NULL,
    SECURITYCODE     INTEGER        NOT NULL,
    EXPIRATIONDATE   DATE           NOT NULL,

    PRIMARY KEY(FIRSTNAME)
)

CREATE TABLE LENDINGHISTORY
(
    TITLE            VARCHAR(25)    NOT NULL UNIQUE,
    CHECKUOTDATE     DATE           NOT NULL,
    DUEDATE          DATE           NOT NULL,
    STATUS           TEXT           NOT NULL,
    USERNAME         VARCHAR(20)    NOT NULL,
    RETURNEDDATE     DATE           NOT NULL,

    PRIMARY KEY(TITLE)
)

CREATE TABLE WISHLIST
(
    TITLE            VARCHAR(25)    NOT NULL UNIQUE,
    AUTHORFNAME      TEXT           NOT NULL,
    AUTHORLNAME      TEXT           NOT NULL,
    THUMBNAIL        VARCHAR(20)    NOT NULL,
    GENRE            VARCHAR(10)    NOT NULL,
    PRICE            DECIMAL(9,2)   NOT NULL,

    PRIMARY KEY(TITLE)
)

CREATE TABLE REVIEWS
(
    USERNAME         VARCHAR(30)    NOT NULL UNIQUE,
    REVIEWTEXT       VARCHAR(40)    NOT NULL,
    RATING           INT            NOT NULL,
    TITLE            VARCHAR(25)    NOT NULL,

    PRIMARY KEY(USERNAME)
)

ALTER TABLE REVIEWS
ADD FOREIGN KEY(TITLE)
REFERENCES WISHLIST(TITLE)

INSERT INTO BOOKS
VALUES ('0547928220', 'The Hobbit', 'J.J.R.', 'Tolkien', 'Fantasy', 'Houghton Mifflin Harcourt', '9-18-2012', 
        '"In a hole in the ground, there lived a hobbit." So begins one of the most beloved and delightful tales in the English language. Set in the imaginary world of Middle-earth, at once a classic myth and a modern fairy tale, The Hobbit is one of literature s most enduring and well-loved novels.',
        '75Th Anniversary Edition', 'PaperBack', '320', 'N/A', 'the_Hobbit', 'the_Hobbit2', '$8.30', 'Available');

SELECT *
FROM BOOKS 

1 个答案:

答案 0 :(得分:1)

我认为这里有一些不同的事情。首先,您有一行在插入之前读取drop TABLE BOOKS,这将从注册表中删除该表,然后才能向其添加任何行。

其次,您尝试插入的数字被引号括起来,而SQL与隐式转换的开头并不是很好。您的ISBN可能应该是VARCHAR,因为它包含前导0,它将在实际转换时丢弃。所以@marc_s是正确的,删除​​要插入的所有数值的引号;这包括ISBN(当前),页码和价格。

我认为错误实际上是指您在price字段中输入的内容,即$8.30。数字不知道如何在SQL中读取$。此外,还有一种数据类型可以满足这种称为money的需求。删除$并将价格列的数据类型更改为money,您应该没问题。

希望这有帮助。

-C§