执行存储过程NewId()时语法不正确

时间:2017-05-21 12:01:07

标签: sql sql-server database stored-procedures uniqueidentifier

IdentityId属于UNIQUEIDENTIFIER类型,当我插入记录并将其设置为NewId()时,我收到错误消息

  

新语法不正确

不确定我错过了什么。请指教。

执行命令:

Exec dbo.CreateUser 
         @Title = 'MR',
         @FirstName = 'John', 
         @LastName = 'Smith', 
         @IdentityId = NewId(), 
         @PhoneNumber = '01234456789', 
         @MobileNumber = '0987654321', 
         @StatesId = 2, 
         @AddressLineOne = '1 A Road',
         @AddressLineTwo = '',
         @Town = 'Some Town',
         @County = 'County',
         @PostCode = 'AA1 1AA',
         @Country = 'United Kingdom',
         @OrganisationName = 'OrgName',
         @OrganisationDomain = 'orgName.com'

CreateUser存储过程:

CREATE PROCEDURE [dbo].[CreateUser]
    @Title NVARCHAR(50),
    @FirstName NVARCHAR(50), 
    @LastName NVARCHAR(50), 
    @IdentityId UNIQUEIDENTIFIER, 
    @PhoneNumber NVARCHAR(50), 
    @MobileNumber NVARCHAR(50), 
    @StatesId INT, 
    @AddressLineOne NVARCHAR(MAX),
    @AddressLineTwo NVARCHAR(MAX),
    @Town NVARCHAR(50),
    @County NVARCHAR(50),
    @PostCode NVARCHAR(10),
    @Country NVARCHAR(20),
    @OrganisationName NVARCHAR(MAX),
    @OrganisationDomain NVARCHAR(MAX)
AS
    INSERT INTO Addresses (AddressLineOne, AddressLineTwo, Town, County, PostCode, Country)
    VALUES (@AddressLineOne, @AddressLineTwo, @Town, @County, @PostCode, @Country)

    INSERT INTO Organisations(Name, Domain, AddressId)
    VALUES (@OrganisationName, @OrganisationDomain, SCOPE_IDENTITY())

    INSERT INTO Users (Title, FirstName, LastName, IdentityId, MobileNumber, PhoneNumber, OrganisationId, StatesId, DateCreated)
    VALUES (@Title, @FirstName, @LastName, @IdentityId, @PhoneNumber, @MobileNumber, SCOPE_IDENTITY(), @StatesId, GETDATE())

1 个答案:

答案 0 :(得分:0)

将值设置为变量并使用:

declare @id uniqueidentifer;

set @id = newid();

exec . . .
     @IdentityId = @id,
     . . .;

这里没有关于newid()的具体内容。 exec语句不解析表达式,甚至是简单的函数调用。

此外,存储过程正文周围只有beginend。您应始终执行此操作,以防止意外错误。