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 2000 Forums
 Transact-SQL (2000)
 Problem with ID

Author  Topic 

shapper
Constraint Violating Yak Guru

450 Posts

Posted - 2006-12-20 : 17:30:38
Hello,

I have a table where the primary key is FAQId which is a GUID.
My Procedure has a parameter named @FAQId.

I want to check if the is a record in the table where FAQId = @FAQId

Here is the important part of my code:

-- Find record with given FAQId
SELECT FAQId FROM dbo.by27_FAQ WHERE FAQId = @FAQId

-- Check if FAQId is Null
IF FAQId IS NULL
BEGIN
-- Create a new FAQId by defining a new GUID
SET FAQId = NEWID();

-- Insert new FAQ in by27_FAQ
INSERT dbo.tFAQ (FAQId, FAQType)
VALUES (FAQId, @FAQType)

I am getting the error "Incorrect Syntax near =" on code line SET FAQId = NEWID();

Any idea what might be going on?

Thanks,
Miguel

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2006-12-20 : 22:23:40
Because FAQId is not a variable so that you can directly assign to it.

if not exists(SELECT * FROM dbo.by27_FAQ WHERE FAQId = @FAQId)
begin
insert dbo.tFAQ (FAQId, FAQType)
Values(NEWID(),@FAQType)
end


Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-12-21 : 00:56:18
@FAQid variable might be an OUT parameter.
if @faqid is null
begin
select @faqid = newid()
insert dbo.tFAQ (FAQId, FAQType)
Values(@faqid,@FAQType)
end
else if not exists(SELECT * FROM dbo.by27_FAQ WHERE FAQId = @FAQId)
begin
insert dbo.tFAQ (FAQId, FAQType)
Values(@faqid,@FAQType)
end

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-12-21 : 00:57:53
If you want FAQid to be a GUID, change
1. The datatype to be a GUID
2. property settings in your table to be newid()

It should work
Go to Top of Page
   

- Advertisement -