2016-07-25 69 views
2

這是我的T-SQL代碼。如何使用SQL Server中的ID連接字符串?

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) NOT NULL DEFAULT /*what goes in here*/, 
    [LastName]  NVARCHAR(50) NOT NULL DEFAULT 'LastName', 

    CONSTRAINT [PK_dbo.AspNetUsers] 
     PRIMARY KEY CLUSTERED ([Id] ASC) 
); 
GO 

CREATE UNIQUE NONCLUSTERED INDEX [UserNameIndex] 
    ON [dbo].[AspNetUsers]([UserName] ASC); 

我想FirstNameLastName將不允許作爲空,但我不斷收到一個錯誤說,我要麼添加默認或任我應該讓他們有空值(我不想做完全)。所以我想我會添加默認值。我想採用Id並將其與連字和LastName連接起來。

例如,如果我的Id是345,我希望我默認值FirstNameLastName分別爲345FirstName345LastName(的nvarchar的類型)。

那麼我該如何做到這一點?

+0

你不能寫'[名字] NVARCHAR(50)NOT NULL默認密碼+'FirstName'' - 它不會工作。插入/更新觸發器更新此列後更好地使用。 – gofr1

+0

其實我不知道T-SQL,我用Visual Studio的設計編輯器來添加這些列。如果可能,你可以修改整個T-SQL嗎?或者有沒有辦法在Visual Studio中使用設計編輯器? –

+0

我用例子添加一個答案。 – gofr1

回答

1

你可以讓這個字段爲空的和更新後添加,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 
2

DEFAULT約束必須是常量,你不能在DEFAULT定義上下文中使用列名。我認爲唯一的方法是使用觸發器(我不確定這是不是過度工程)。看看這個例子:

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) NOT NULL, 
    [LastName] NVARCHAR(50) NOT NULL, 
    CONSTRAINT [PK_dbo.AspNetUsers] PRIMARY KEY CLUSTERED ([Id] ASC) 
); 
GO 
CREATE TRIGGER trInsert_AspNetUsers 
ON [dbo].[AspNetUsers] INSTEAD OF INSERT 
AS 
    INSERT [dbo].[AspNetUsers]([Id], [Email], [PasswordHash], [UserName], [FirstName], [LastName]) 
    SELECT [Id], [Email], [PasswordHash], [UserName], COALESCE([FirstName], [Id]+'FirstName'), COALESCE([LastName], [Id]+'LastName') 
    FROM inserted 
GO 

INSERT dbo.AspNetUsers VALUES('1', '2', '3','4',NULL,NULL) 
INSERT dbo.AspNetUsers VALUES('5', '6', '7', '8', 'Paweł', 'Dyl') 
SELECT * FROM dbo.AspNetUsers 

SELECT返回下表:

Id Email PasswordHash UserName FirstName  LastName 
1 2  3   4  1FirstName 1LastName 
5 6  7   8  Paweł   Dyl 
+0

而不是插入觸發器的想法甚至更好! – gofr1