从字符串转换为uniqueidentifier sql时转换失败

时间:2019-01-13 18:27:49

标签: sql sql-server tsql uniqueidentifier

我创建了一个具有以下属性的表:

CREATE TABLE [a02].[property] 
(
    propertyID UNIQUEIDENTIFIER PRIMARY KEY DEFAULT newsequentialid(),
    propertyTitle VARCHAR(100) NOT NULL,
    residenceName VARCHAR(100) NOT NULL,
    residenceStreet VARCHAR(100) NOT NULL,
    postCode VARCHAR(100) NOT NULL,
    bedroomCount INT NOT NULL,
    bathroomCount INT NOT NULL,
    garageCarCount INT NOT NULL,
    internalSqMetres FLOAT NOT NULL,
    outdoorSqMetres FLOAT NOT NULL,
    askingPrice FLOAT NOT NULL,
    sellingPrice FLOAT,
    registeredDate DATE NOT NULL,
    soldDate DATE,
    localityID UNIQUEIDENTIFIER NOT NULL
            REFERENCES [a02].[locality] (localityID),
    typeID UNIQUEIDENTIFIER NOT NULL
            REFERENCES [a02].[propertyType] (typeID),
    accountID UNIQUEIDENTIFIER NOT NULL
            REFERENCES [a02].[userAccount] (accountID)
);

我要插入以下记录:

INSERT INTO [a02].[property] (propertyTitle, residenceName, residenceStreet, 
                              postCode, bedroomCount, bathroomCount, garageCarCount,             
                              internalSqMetres, outdoorSqMetres, askingPrice, sellingPrice,         
                              registeredDate, soldDate, localityID, typeID, accountID)
VALUES ('Ground Floor Maisonette Fgura', '10', 'Triq il-Kbira', 
        'FGR2000', 3, 1.5, 2, 
        130, 50, 250000, 235000, 
        '11/13/2018', '11/13/2018', 
        (SELECT localityName FROM [a02].[locality] 
         WHERE localityName = 'Il-Fgura'), 
        (SELECT typeName FROM [a02].[propertyType] 
         WHERE typeName ='Maisonette'), 
        (SELECT accountID FROM [a02].[userAccount] 
         WHERE accountUsername = 'joeb')),
      ('First Floor Floor Maisonette Fgura', '10', 'Triq il-Kbira', 
       'FGR2000', 3, 1.5, 2, 
       130, 50, 350000, 335000, '11/13/2018', '11/13/2017', 
       (SELECT localityName FROM [a02].[locality] 
        WHERE localityName = 'Il-Fgura'), 
       (SELECT typeName FROM [a02].[propertyType] 
        WHERE typeName = 'Maisonette'), 
       (SELECT accountID FROM [a02].[userAccount] 
        WHERE accountUsername ='joeb')),
      ('Ground Floor Maisonette Mosta', '20', 'Triq il-Qamh', 
       'MST1000', 4, 2, 2, 
       150, 50, 275000, NULL, '11/13/2018', NULL, 
       (SELECT localityName FROM [a02].[locality] 
        WHERE localityName = 'Il-Mosta'), 
       (SELECT typeName FROM [a02].[propertyType] 
        WHERE typeName = 'Maisonette'), 
       (SELECT accountID FROM [a02].[userAccount] 
        WHERE accountUsername = 'lisaA')),
      ('Penthouse Zabbar', '5', 'Triq il-Passa',  
       'ZBR1050', 3, 1, 1, 
       150, 50, 300000, NULL, '11/13/2018', NULL, 
       (SELECT localityName FROM [a02].[locality] 
        WHERE localityName = 'Haz-Zabbar'),
       (SELECT typeName FROM [a02].[propertyType] 
        WHERE typeName = 'Penthouse'), 
       (SELECT accountID FROM [a02].[userAccount] 
        WHERE accountUsername ='joeb'));

我收到此错误:

  

从字符串转换为uniqueidentifier时转换失败

它标记了这一行:

INSERT INTO [a02].[property] (propertyTitle, residenceName,residenceStreet, postCode, bedroomCount, bathroomCount, garageCarCount, internalSqMetres, outdoorSqMetres, askingPrice, sellingPrice, registeredDate, soldDate, localityID, typeID, accountID)

有什么线索吗?

1 个答案:

答案 0 :(得分:0)

在插入语句中使用以下查询将字符串值转换为uniqueidentifier。

字符串值应为> =33。如果> 33

,则只需33个字符
SELECT  CAST(
        SUBSTRING(<ColName>, 1, 8) + '-' + SUBSTRING(<ColName>, 9, 4) + '-' + SUBSTRING(<ColName>, 13, 4) + '-' +
        SUBSTRING(<ColName>, 17, 4) + '-' + SUBSTRING(<ColName>, 21, 12)
        AS UNIQUEIDENTIFIER)

就像

INSERT INTO [a02].[property] (
      propertyTitle
      , residenceName
      , residenceStreet
      , postCode
      , bedroomCount
      , bathroomCount
      , garageCarCount
      , internalSqMetres
      , outdoorSqMetres
      , askingPrice
      , sellingPrice
      , registeredDate
      , soldDate
      , localityID
      , typeID
      , accountID
      )
VALUES (
      'Ground Floor Maisonette Fgura'
      , '10'
      , 'Triq il-Kbira'
      , 'FGR2000'
      , 3
      , 1.5
      , 2
      , 130
      , 50
      , 250000
      , 235000
      , '11/13/2018'
      , '11/13/2018'
      , (
            SELECT CAST(
        SUBSTRING(localityName, 1, 8) + '-' + SUBSTRING(localityName, 9, 4) + '-' + SUBSTRING(localityName, 13, 4) + '-' +
        SUBSTRING(localityName, 17, 4) + '-' + SUBSTRING(localityName, 21, 12)
        AS UNIQUEIDENTIFIER)
            FROM [a02].[locality]
            WHERE localityName = 'Il-Fgura'
            )
      , (
            SELECT CAST(
        SUBSTRING(typeName, 1, 8) + '-' + SUBSTRING(typeName, 9, 4) + '-' + SUBSTRING(typeName, 13, 4) + '-' +
        SUBSTRING(typeName, 17, 4) + '-' + SUBSTRING(typeName, 21, 12)
        AS UNIQUEIDENTIFIER)
            FROM [a02].[propertyType]
            WHERE typeName = 'Maisonette'
            )
      , (
            SELECT CAST(
        SUBSTRING(accountID, 1, 8) + '-' + SUBSTRING(accountID, 9, 4) + '-' + SUBSTRING(accountID, 13, 4) + '-' +
        SUBSTRING(accountID, 17, 4) + '-' + SUBSTRING(accountID, 21, 12)
        AS UNIQUEIDENTIFIER)
            FROM [a02].[userAccount]
            WHERE accountUsername = 'joeb'
            )
      )