By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,364 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,364 IT Pros & Developers. It's quick & easy.

Insert into a table only if count is less than 'n'

P: 90
I need to insert the record in the database only if the count is less than 'n'.
ie if i need to enter a name first i need to find the count of name if it is less than 2 then isert the name and if it is more than 2 then it should not be entered,
i tried but it does not work where did i go wrong??

insert into tablename values('aaa') where count(*)columnname <=2

I think where could not be used with aggregate functions instead of that what has to be put?
Apr 21 '08 #1
Share this Question
Share on Google+
1 Reply


P: 9
I need to insert the record in the database only if the count is less than 'n'.
ie if i need to enter a name first i need to find the count of name if it is less than 2 then isert the name and if it is more than 2 then it should not be entered,
i tried but it does not work where did i go wrong??

insert into tablename values('aaa') where count(*)columnname <=2

I think where could not be used with aggregate functions instead of that what has to be put?
Look up on Google the IF and CASE statements. In your case you would need to do something like:

Expand|Select|Wrap|Line Numbers
  1. IF (SELECT COUNT(*) FROM [tablename]) <= 2 
  2.       INSERT INTO [tablename] ([column]) VALUES 'aaa'
  3.  
Apr 21 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.