We already have this table (IPConfig
) in db. (Sql Server 2k8)
IP | Member
-----------------------
10.1.2.100 | Joe
10.2.2.200 | Maley
Now, I should have a query which should do this:
- Leave current table intact. If table is not there, create one.
- Insert new records (defined in the next version deployment)
I started with this but could not proceed.
IF (NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dbo'
AND TABLE_NAME = 'IPConfig'))
BEGIN
CREATE TABLE dbo.IPConfig (
IP CHAR(10) PRIMARY KEY NOT NULL,
Member VARCHAR(32) NOT NULL)
END
DECLARE @TempTable Table( -- Create a temp table.
IP CHAR(10) PRIMARY KEY NOT NULL,
Member VARCHAR(32) NOT NULL)
INSERT INTO @TempTable(
IP,
Member)
SELECT
'10.1.2.100', --Already existing Ip; dont insert
'Joe'
UNION SELECT
'10.2.2.200', --Already existing Ip; dont insert
'Maley',
UNION SELECT
'10.3.5.200', --New; Insert.
'NewUser',
UPDATE TABLE dbo.IPConfig
/// Here what should be done ? Should I loop through the temp table using triggers or what is the best way ?
A super simple example please.
Sorry if this a naive question; totally new to MSSQL programming.