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),
@Monitors MonitorList READONLY
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;
Best Answer
The basic form is:
In your case, that would be:
One thing you need to find out, though, is if users enter existing
empID
values here and expect to update existing employees with new information for the other columns... if so, you're throwing their updates away without telling them.