Hi guys,

Very new to sql and my question is prob very basic.

I have a table desing shown below. I will using a c# program to add new rows to the database but i only want to add a new row if the hostname doesnt currently exist in the table. If it does i just need to add the new info to the current row with the same hostname and update the UTS field.

CREATE TABLE [dbo].[T_Servers](
	[ServerID] [int] IDENTITY(1,1) NOT NULL,
	[HostName] [varchar](50) NOT NULL,
	[MachineType] [varchar](50) NOT NULL,
	[Kernel] [varchar](50) NOT NULL,
	[CPUS] [tinyint] NOT NULL,
	[CPUSPeed] [varchar](15) NOT NULL,
	[MemoryMB] [int] NOT NULL,
	[Architecture] [varchar](50) NOT NULL,
	[SerialNo] [varchar](100) NOT NULL,
	[PrimaryIP] [varchar](20) NOT NULL,
	[DNS] [varchar](10) NOT NULL,
	[NIS] [varchar](10) NOT NULL,
	[CTS] [datetime] NOT NULL,
	[UTS] [datetime] NOT NULL,
 CONSTRAINT [PK_t_serverID] PRIMARY KEY CLUSTERED

The query i run to add new rows is shown below ( i havent actually filled in the values as this is read in from a file via c#":

INSERT INTO T_Servers(HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
NIS)
VALUES (HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
NIS)INSERT INTO T_Servers(HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
NIS)
VALUES (HostName, MachineType, Kernel, CPUS, CPUSPeed, MemoryMB, Architecture, SerialNo, PrimaryIP, DNS,
NIS)

I guess my question is can i change this code to do the checking i need so it will only insert rows with hostnames not in the table and if they are just update them?? Any help much apreciated

solved it myself using an update statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.