Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Proc error

Author  Topic 

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 01:48:35
Hi All,

I have a small proc which checks for the presence of the records.
if it is present it should print a message. if not, it should insert.
but here i am getting error. pls let me know.

ALTER PROC sp_chkdupval (@FirstName varchar(50),@LastName varchar(50),@EmailID varchar(50),@MobileNo varchar(15))
AS
BEGIN
SELECT FirstName,LastName,EmailID,MobileNo,count(RecCount) as RecCount
FROM
(SELECT FirstName,LastName,EmailID,MobileNo,FirstName+LastName+EmailID+MobileNo as RecCount
FROM NameList
WHERE FirstName+LastName+EmailID+MobileNo like @FirstName+@LastName+@EmailID+@MobileNo
)M
GROUP BY FirstName,LastName,EmailID,MobileNo
IF count(RecCount) = 0 -- here i am getting invalid column name error.
BEGIN
INSERT INTO NameList (FirstName,LastName,EmailID,MobileNo) VALUES(@FirstName,@LastName,@EmailID,@MobileNo)
PRINT 'New Record!'
END
ELSE
PRINT 'Record Exists'
END

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 01:53:21
I don't quite understand what you are trying to do as the SELECT statement will return multiple records. Maybe you want to use @@ROWCOUNT instead, but I'm not exactly sure. Maybe you want a CASE statement.

Could you show us a data example.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 01:58:34
I am making insert into a table. I would like to know whether that record already exists.
In the actual proc, UniqueID is used. it is not a PK. but it is unique for each record.
So if UniqueID is null, it must insert else it must update.

This is working fine. But now i have to check for one more condition here:

If the user tries to enter the same rec twice, it must be avoided. since uniqueID will be null initially, i must check thru Name and emailid together to be unique.

To achieve this i wrote this proc.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 02:04:52
I'm not clear still, but here's what I came up with:


ALTER PROC sp_chkdupval (@FirstName varchar(50),@LastName varchar(50),@EmailID varchar(50),@MobileNo varchar(15))
AS
BEGIN

IF NOT EXISTS (SELECT * FROM NameList WHERE FirstName = @FirstName AND LastName = @LastName AND EmailID = @EmailID AND MobileNo = @MobileNo)
BEGIN
INSERT INTO NameList (FirstName,LastName,EmailID,MobileNo)
VALUES (@FirstName,@LastName,@EmailID,@MobileNo)

PRINT 'New Record!'
END
ELSE
PRINT 'Record Exists'


Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 02:09:45
If there are two rec with same name i.e two diff persons with same names, ABC and ABC are there, then it must check for other fields so that it can verify where it is a duplicate or not.

While inserting, duplication must be avoided.

if i write the proc as you said, there are chances of dup vals as there may be 2 persons with firstname ABC but with diff lastname and email. so i must check thoroughly and only then insert.
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 02:11:11
For this purpose i am taking Fname+LastName+email. if this matches, it means it is the duplicate rec. if not it is a new rec and it must insert.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 02:12:03
You'll need to tell us how a duplicate is determined then. The proc code I posted will ensure that all 4 fields are unique in the table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 02:17:25
I am sorry Madam, this works. But what if someone passes a blank value for LastName or any other column? Coz in the table, they are nullable.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 02:18:53
Blank values don't change the functionality above. All 4 columns combined have to be unique in order for the insert to proceed, that's what the above code does.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

mrm23
Posting Yak Master

198 Posts

Posted - 2010-04-22 : 02:24:55
When i gave LastName Blank and retained all other fields same, it considered as a new rec.
first i did

EXEC sp_chkdupval 'A','B','ab@xyz.com','1234567890'

This was inserted. But when i gave

EXEC sp_chkdupval 'A',' ','ab@xyz.com','1234567890'

This was inserted too.... here it is the same rec. assume someone else entered this rec.
Since emailid will be unique, we can make out if it is a duplicate.

i thought of something like FName+Lname+Email to be unique.

Will this work?
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2010-04-22 : 02:27:32
You need to tell us the business rule of what makes it unique. Give us all of the rules so that we can write the proc. If it's just emailid, then just search for that in the IF NOT EXISTS.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -