我具有由如何檢索並存儲這些指導以備後用?
CREATE TABLE Partners (
id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(),
name NVARCHAR(50) NOT NULL,
email NVARCHAR(254) NOT NULL, -- 254 is optimal length according to http://stackoverflow.com/questions/1199190/what-is-the-optimal-length-for-an-email-address-in-a-database
PRIMARY KEY (id)
);
,並在一個存儲過程一個嘗試用於插入行並返回插入行的id
生成的表Partners
:
CREATE PROCEDURE AddPartner
@name NVARCHAR(50), @email NVARCHAR(254)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO Partners (name,email)
OUTPUT INSERTED.id INTO @new_guid
VALUES (@name,@email)
SELECT @new_guid
END
假設我要(1)呼叫AddPartner
與(name,email)
等於('some dude','[email protected]
)並返回id
(稱爲dude_id
),這樣我可以在以後插入另一行時使用它:
INSERT INTO Answers (question_id,partner_id,val) VALUES (1,dude_id,24);
我到底該怎麼做? (1)和(2)之間會有GO
聲明破壞一切嗎?
如果你沒有在進口時的ID檢查。 http://stackoverflow.com/questions/42648/best-way-to-get-identity-of-inserted-row –