Click here to Skip to main content
15,899,314 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

It will we helpful if some could assist me resolving the issue. I've a following table and I want to know if there are no more rows in the table.

Table Name: Test

Name LastName
----- -------
Gena | Marry
Sam | Scott
April | Jones

The actual problem is, I want to insert a new record if and only if 'LastName' is not repeated in the column. (i.e. If LastName is Scott then there should be no new record inserted and /or LastName is Rio then only new record should be inserted.

Please assist.
Posted

1 solution

The follow are the steps for what you want to achieve

1. Write a small stored procedure where you pass all the values to be inserted in the table.
2. Check count of lastname which matches with the lastname you have passed.
3. if count is 0 the insert the record

if you dont understand what I am trying to say then check the code below
SQL
create procedure CheckCountOfLastName(@name varchar(20), @lastname varchar(20))
as
	declare @count numeric
	select @count = count(lastname) from yourtablename where lastname=@lastname
	if (@count = 0)
	begin
		insert into yourtablename(name, lastname) values(@name, @lastname)
	end

Hope it helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900