你可以讓這個字段爲空的和更新後添加,INSERT觸發器:
CREATE TABLE [dbo].[AspNetUsers] (
[Id] NVARCHAR (128) NOT NULL,
[Email] NVARCHAR (256) NULL,
[PasswordHash] NVARCHAR (MAX) NULL,
[UserName] NVARCHAR (256) NOT NULL,
[FirstName] NVARCHAR(50) NULL,
[LastName] NVARCHAR(50) NULL,
CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC)
);
GO
CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex]
ON [dbo].[AspNetUsers]([UserName] ASC);
GO
CREATE TRIGGER [UserNameTrigger]
ON [dbo].[AspNetUsers]
AFTER INSERT, UPDATE
AS
UPDATE anu
SET [FirstName] = ISNULL(i.[FirstName],i.Id + 'FirstName'),
[LastName] = ISNULL(i.[LastName],i.Id + 'LastName')
FROM [dbo].[AspNetUsers] anu
INNER JOIN inserted i
ON i.[Id] = anu.Id
GO
所以,如果你運行此查詢:
INSERT INTO [dbo].[AspNetUsers]
VALUES
(NEWID(),'[email protected]','qwerty','username1',NULL,NULL),
(NEWID(),'[email protected]','qwerty','username2','John',NULL),
(NEWID(),'[email protected]','qwerty','username3',NULL,'Smith'),
(NEWID(),'[email protected]','qwerty','username4','Jason','Anderson')
GO
SELECT [Id]
,[Email]
,[PasswordHash]
,[UserName]
,[FirstName]
,[LastName]
FROM [dbo].[AspNetUsers]
GO
您將獲得:
Id Email PasswordHash UserName FirstName LastName
07AA1C80-7386-4E48-9556-9D81E2FBFCB0 [email protected] qwerty username2 John 07AA1C80-7386-4E48-9556-9D81E2FBFCB0LastName
6310E4E6-ADDB-4F50-9D1E-5C756059DB40 [email protected] qwerty username1 6310E4E6-ADDB-4F50-9D1E-5C756059DB40FirstName 6310E4E6-ADDB-4F50-9D1E-5C756059DB40LastName
6F539B99-2B4C-440E-A96A-8F5D54A9B758 [email protected] qwerty username3 6F539B99-2B4C-440E-A96A-8F5D54A9B758FirstName Smith
9665747B-A216-46CC-9BA3-159512AAC80C [email protected] qwerty username4 Jason Anderson
你不能寫'[名字] NVARCHAR(50)NOT NULL默認密碼+'FirstName'' - 它不會工作。插入/更新觸發器更新此列後更好地使用。 – gofr1
其實我不知道T-SQL,我用Visual Studio的設計編輯器來添加這些列。如果可能,你可以修改整個T-SQL嗎?或者有沒有辦法在Visual Studio中使用設計編輯器? –
我用例子添加一個答案。 – gofr1