1

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:

  1. Leave current table intact. If table is not there, create one.
  2. 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.

DatRid
  • 1,169
  • 2
  • 21
  • 46

3 Answers3

5

If I understand correctly you only insert new unmatched IPs to dbo.IPConfig. Left outer join is the best choice here. Insert the results to your target table.

INSERT INTO dbo.IPConfig([IP], [Member])
SELECT a.[IP], a.[Member]
FROM @TempTable a
LEFT OUTER JOIN dbo.IPConfig b ON a.[IP]=b.[IP]
WHERE b.[IP] is null
bmlsoftware
  • 121
  • 5
0

You don't need any temp table, you could create a stored procedure though. The simplest way is described here INSERT VALUES WHERE NOT EXISTS

INSERT  IPConfig (IP, Member) 
SELECT  '10.1.2.100', 'Joe'
WHERE   NOT EXISTS 
        (   SELECT  1
            FROM    IPConfig 
            WHERE   IP = '10.1.2.100'
        );

As stored procedure:

CREATE PROCEDURE InsertIfNotExist 
    @IP nvarchar(50), 
    @Member nvarchar(50) 
AS 

    INSERT  IPConfig (IP, Member) 
    SELECT  @IP, @Member
    WHERE   NOT EXISTS 
            (   SELECT  1
                FROM    IPConfig 
                WHERE   IP = @IP
            );
GO


EXECUTE InsertIfNotExist '10.1.2.100', 'Joe';
EXECUTE InsertIfNotExist '10.2.2.200', 'Maley'
Community
  • 1
  • 1
0

this worked for me

$query_reg="INSERT dbo.Registro (registro,fecha)
            SELECT 'Inscripcion','".$date."'
            WHERE NOT EXISTS (
                              SELECT registro,fecha 
                              FROM dbo.Registro
                              WHERE registro ='Inscripcion' AND
                                       fecha = '".$date."'
                              )";

             $sql_reg = sqlsrv_query($conn,$query_reg);
          if ($sql_reg) // Se eejectuto la sentencia SQL?
              {
              echo "SQLSuccess"; // Mensaje Afirmativo.            
                } else {
          die( print_r( sqlsrv_errors(), true)); // error.        
               }    

             exit();
hector teran
  • 369
  • 1
  • 5
  • 15