# Sql-server – Insert/Update Stored Procedure

sql serversql-server-2008-r2stored-procedures

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?

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),

-- My Stored Procedure (SP)
@Serial nvarchar(50),

AS
DECLARE @TVPComputer TABLE (Computers int)
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),

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);
OUTPUT inserted.Id INTO @ComputerId(Id)

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),

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);

OUTPUT inserted.Id INTO @ComputerId(CId)

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


One execution of your script will produce one computer name and a list of one or more monitors. You can pass these to a SQL Server stored procedure, you just have to get the parameter types correct.

The computer name is simplest. With just this the SP definition would look like this:

create procedure dbo.MyProcedureName
@ComputerName varchar(100)
as
...


I use varchar(100) as an example. You use whatever's right for you. The list of monitors is more difficult because it may contain many values. For this you have to use a table-valued parameter and for that you will have to define a type:

create type MonitorList AS TABLE (
MonitorName varchar(100)
);
GO


Now the SP looks like this:

create procedure dbo.MyProcedureName
@ComputerName varchar(100),
as
...


Now to the body of the SP. If you use the names as the primary key in tables Computer and Monitor the INSERT statements are straightforward. Assuming you have primary keys and foreign keys defined -- you should -- you have to insert in the correct sequence to respect these key definitions i.e. into Computer and Monitor and only then into the mapping table.

insert dbo.Computer (ComputerName)
values (@ComputerName);


The type we defined, and variables created off it, behave just like any other table:

insert dbo.Monitor (MonitorName)
select MonitorName
from @Monitors;

insert Monitor_Computer_Map (ComputerName, MonitorName)
select @ComputerName, MonitorName
from @Monitors;


If your tables have surrogate keys that use IDENTITY, however, you have more work to do. You have to capture these identity values as they are generated in the parent tables and use them in the mapping table. This can be done with the OUPUT clause:

declare @ComputerID table ( ComputerID int);

insert dbo.Computer (ComputerName)
output INSERTED.ID
into @ComputerID
values (@ComputerName);


Do the same thing for monitors and use the local table variables to populate the mapping table.

Of course you want to have appropriate validation, duplicate checking and error handling in the body of the SP, too.

You don't say what scripting language you use. The documentation for it will tell you how to declare and populate stored procedure parameters for SQL Server.

Response to OP's EDIT #2:

First, a few tips. Please post the full error message; it helps immensely with debugging. Second, if you're using SSMS you can double-click an error and it will highlight the code in error. Third, get in the habit of closing your statements with a semicolon. It is not required yet but it will be soon.

If all computers have exactly one monitor then the TVP is not needed. You are correct. How many developers have only one monitor these days? I've seen finance traders' stations with eight. In these cases you do want a TVP. Please, please, please do not be tempted to write @Serial1, @Serial2, @Serial3 ....

Your code will throw the error Must declare the scalar variable "@MonitorId". This is because of your third INSERT statement:

INSERT INTO dbo.Monitor_Computers (Monitor_Computer_Monitor, Monitor_Computer_Computer)
VALUES (@MonitorId, @ComputerId);


When you use the INSERT..VALUES syntax SQL Server demands that there can be only one value per variable. You have provided table valued variables which could (potentially) hold many values. What you need is the INSERT..SELECT syntax. Yes, I know @ComputerID only has one row but it is a table and it could have many rows. What you need is:

INSERT INTO dbo.Monitor_Computers (Monitor_Computer_Monitor, Monitor_Computer_Computer)
SELECT m.Id, c.Id
from @ComputerId AS c
cross join @MonitorId AS m;