I'm having two table on my SQL
server GamerName
and GamerValues
, I want that, when I insert
a new row on GamerName
GamerName
╔════╦═════════╦
║ ID ║ Name ║
╠════╬═════════╬
║ 1 ║ Jeff ║
║ 2 ║ Geoff ║
║ 3 ║ Jarrod ║
║ 4 ║ Joel Sp ║
╚════╩═════════╩
So, with the last inserted
ID on the table GamerName
, the values should be filled on GamerValues
GamerValues
╔════╦══════════════╦════════╗
║ ID ║ GamerID ║ Score ║
╠════╬══════════════╬════════╣
║ 1 ║ 1 ║ 5636 ║
║ 2 ║ 2 ║ 148 ║
║ 3 ║ 3 ║ 101 ║
║ 4 ║ 4 ║ 959 ║
╚════╩══════════════╩════════╝
Is it possible to do it with a single query?
You should manage this with a stored procedure. You can concentrate any additional logic you need when creating a new gamer this way (for example, checking if already exists).
CREATE PROCEDURE dbo.CreateGamer
@Name VARCHAR(100)
AS
BEGIN
IF EXISTS (SELECT 'gamer name already exists' FROM GamerName AS G WHERE G.Name = @Name)
BEGIN
RAISERROR('Gamer name already used!', 16, 1)
RETURN
END
INSERT INTO GamerName (
Name)
VALUES
(@Name)
DECLARE @GamerID INT = SCOPE_IDENTITY()
INSERT INTO GamerValues (
GamerID,
Score)
SELECT
GamerID = @GamerID,
Score = 0
END
You can ensure the uniqueness of the gamer name with a UNIQUE
constraint, I'm just using the EXISTS
to show how you can put additional logic in your SP.
You don't need a trigger or any of the @@IDENTITY
, SCOPE_IDENTITY()
functions. All of them have restrictions and none of them can deal with values that aren't produced by an IDENTITY
constraint. None of them can deal with multiple insertions either.
You can use the OUTPUT clause of INSERT
to copy newly inserted values into another table or a table variable.
OUTPUT is also available for UPDATE and DELETE. You can retrieve the new/modified columns with the inserted.
and deleted.
prefixes
For these tables :
create table #GamerName
(
ID int IDENTITY primary key,
Name nvarchar(20) not null
);
create table #GamerValues(
ID int IDENTITY primary key,
GamerID int not null,
Score int not null
);
You can insert new records in #GamerName
and copy the generated ID to #GamerValues
with :
INSERT INTO #GamerName (Name)
OUTPUT inserted.ID,0 into #GamerValues(GamerID,Score)
VALUES
('Jeff'),
('Geoff'),
('Jarrod'),
New values appear in the inserted
virtual table. OUTPUT is also availa
A new line will be created for each of the gamers in GamerValues. Let's modify the default score with :
UPDATE #GamerValues
SET Score=100
The table will look like :
ID GamerID Score
1 1 100
2 2 100
3 3 100
Adding another gamer with
insert into #GamerName (Name)
output inserted.ID,0 into #GamerValues(GamerID,Score)
Values
('Joel sp')
Will result in a new line with a Score of 0
ID GamerID Score
1 1 100
2 2 100
3 3 100
4 4 0
You have to add a trigger to this for you.
CREATE TRIGGER newTrigger ON GamerName
FOR INSERT
AS
INSERT INTO GamerValues
(GamerID)
SELECT
'ID'
FROM inserted
GO
Btw the score variable should be set 0 by default since you don't have a value when a new user is created. Unless if you do.
Hope this helps
If you have an identity column in Gamername Table you can use
INSERT INTO GamerName (Name)VALUES
('Joel Sp');
INSERT INTO GamerValues (GamerID,Score)
SELECT SCOPE_IDENTITY() ,0 score;
If you just need the last column inserted and assuming the gamer id in gamername table are sequential then you can simply use
INSERT INTO GamerValues (GamerID,Score)
select max(id),0 score from gamername;
If Nothing Satisfies your condition then TRIGGER is the best Option
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.