April 8, 2010 at 12:13 pm
Hi, I am very new to SQL SErver. Have been coding ORacle and want some help with this. What I am basically trying to do is-
If a value doesn't already exist, then set identity on and insert, else, set identity off and insert. SQL server doesn't like it when i tried to set the identity in the condition. How can I achieve this? This is urgent and any help is appreciated.
This is the query I used -
if not exists (select campaigncommentid from CampaignStagingComment where campaigncommentid in (select CampaignCommentID from CampaignComment cc INNER JOIN campaignstorerun cs ON (cc.campaignid = cs.camapignid))) (set identity_insert CampaignStagingComment on insert CampaignStagingComment (..,..,..) select * from CampaignComment cc where exists (select 1 from campaignstorerun ctrr where cc.campaignid = ctrr.campaignid) and not exists (Select 1 from CampaignStagingComment cs where cc.campaignid = cs.campaignid) set identity_insert CampaignStagingComment off)
else (set identity_insert CampaignStagingComment off insert CampaignStagingComment (..,..,..) select * from CampaignComment cc where exists (select 1 from campaignstorerun ctrr where cc.campaignid = ctrr.campaignid) and not exists (Select 1 from CampaignStagingComment cs where cc.campaignid = cs.campaignid) set identity_insert CampaignStagingComment on)
April 8, 2010 at 12:38 pm
would you mind in formatting your query properly? pretty please?
_____________________________________
Pablo (Paul) Berzukov
Author of Understanding Database Administration available at Amazon and other bookstores.
Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.April 8, 2010 at 12:39 pm
One quick thing, you're not formatting your conditional statement correctly ...
IF blah blah
BEGIN
<do this>
<do that>
END
ELSE
BEGIN
<do some other thing>
<do one last thing>
END
Posting your code in a more readable format and including complete table definitions will help too.
April 8, 2010 at 2:56 pm
Thanks so much. 🙂
April 8, 2010 at 8:57 pm
As a side bar, it sounds like you may be trying to keep a "gapless" IDENTITY column. That's normally a highly counter productive thing to do. Please explain why your doing this. And don't just say "business requirements", please. What is the logic behind it all?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply