执行存储过程 NewId() 时语法不正确
Incorrect Syntax When Executing Stored Proc NewId()
IdentityId
是 UNIQUEIDENTIFIER
类型,当我插入一条记录并将其设置为 NewId()
时,我收到错误消息
Incorrect Syntax new )
不确定我错过了什么。请指教
执行命令:
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())
将值设置为一个变量并使用它:
declare @id uniqueidentifer;
set @id = newid();
exec . . .
@IdentityId = @id,
. . .;
这里newid()
没有什么特别的。 exec
语句不解析表达式,甚至不解析简单的函数调用。
也一样,但是begin
和end
围绕存储过程体。你应该总是这样做,以防止意外错误。
IdentityId
是 UNIQUEIDENTIFIER
类型,当我插入一条记录并将其设置为 NewId()
时,我收到错误消息
Incorrect Syntax new )
不确定我错过了什么。请指教
执行命令:
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())
将值设置为一个变量并使用它:
declare @id uniqueidentifer;
set @id = newid();
exec . . .
@IdentityId = @id,
. . .;
这里newid()
没有什么特别的。 exec
语句不解析表达式,甚至不解析简单的函数调用。
也一样,但是begin
和end
围绕存储过程体。你应该总是这样做,以防止意外错误。