My database model is quite simple. I have the following tables: Monitor
, Computer
, and Monitor_Computer_Map
as my mapping table for a many-to-many relationship between the other two tables.
I have a script that will run on end users' computers as the user. (This is why I chose a stored procedure in the first place. Is there a better suggestion?). I need to insert data about the user's computer into the Computer
table and data about the attached monitors into the Monitor
table. What I would like to do is map the monitors to the computers in the mapping table. Is it possible to do all this in a stored procedure?
p.s I'm new to working with databases and I may be going about this all wrong.
EDIT: With the help of @Michael Green i was able to come up with the script below. However, I still can't get it working. Currently I'm reading up on how variables are set and used in SQL so I'm slowly getting it. Also, I'm using a Powershell script to do the inserting which I'm pretty sure I have on lock.
Because my database schema is so simple I'll just post it here. Note: All timestamps are set in the default values.
-- My Table Parameter Value (TVP)
CREATE TYPE ComputerTableType AS TABLE
(Name nvarchar(15),
IPAddress nvarchar(15))
-- My Stored Procedure (SP)
CREATE PROCEDURE sp_InsertUpdateSerialInventory
@Serial nvarchar(50),
@TVPComputer ComputerTableType READONLY
AS
DECLARE @TVPComputer TABLE (Computers int)
INSERT INTO dbo.Computers (Name, IPAddress)
OUTPUT INSERTED.Id
INTO @TVPComputer
VALUES (@TVPComputer);
INSERT INTO dbo.Monitors(Serial)
OUTPUT INSERTED.Id
INTO @Serial
VALUES (@Serial);
EDIT #2:
Still learning...
So I've come to the conclusion i don't actually need a Table Parameter Value
(TPM) for my Computers
table. I only have two fields which isn't a big deal In my opinion. So what I've done is add each parameter to the top of my Stored Procedure
and continue with following @Michael Green's suggestion. I can't seem to get the Table Variable
working correctly. Below is my updated code. Still not working! I keep getting a "Must declare the scalar variable". I found some other topics on this error on Stack Exchange but nothing that resolves my issue. It looks like a scope issue or something?
CREATE PROCEDURE sp_InsertUpdateSerialInventory
@Serial nvarchar(50),
@Name nvarchar(15),
@IPAddress nvarchar(15)
AS
BEGIN
DECLARE @MonitorId TABLE (Id int);
INSERT INTO dbo.Monitors (Serial)
OUTPUT inserted.Id INTO @MonitorId(Id)
VALUES (@Serial)
DECLARE @ComputerId TABLE (Id int);
INSERT INTO dbo.Computers (Name, IPAddress)
OUTPUT inserted.Id INTO @ComputerId(Id)
VALUES (@Name, @IPAddress);
INSERT INTO dbo.Monitor_Computers (Monitor_Computer_Monitor, Monitor_Computer_Computer)
VALUES (@MonitorId, @ComputerId)
END
EDIT #3 - Working! I can't thank you enough @Michael Green for your help. Your answer took me on quite a learning journey which ultimately brought me to a resolution. Below is my working code!
CREATE PROCEDURE InsertUpdateSerialInventory --Fixed Name. That link was a good read thank you!
@Name nvarchar(15),
@IPAddress nvarchar(15),
@Serial MonitorTableType READONLY
AS
BEGIN
DECLARE @MonitorId TABLE (MId int);
INSERT INTO dbo.Monitors (Serial)
OUTPUT inserted.Id INTO @MonitorId(MId)
SELECT Serial
FROM @Serial;
DECLARE @ComputerId TABLE (CId int);
INSERT INTO dbo.Computers (Name, IPAddress)
OUTPUT inserted.Id INTO @ComputerId(CId)
VALUES (@Name, @IPAddress);
INSERT INTO dbo.Monitor_Computers (Monitor_Computer_Monitor, Monitor_Computer_Computer)
SELECT m.MId, c.CId
FROM @MonitorId AS m CROSS JOIN @ComputerId AS c;
END