i got error in sql

Posted by Kannan1986 under Sql Server on 2/19/2010 | Views : 1322 | Status : [Member] | Replies : 3
if exists(select username from Login_Log where username = 'kannan')

if exists (SELECT COUNT(date_time) FROM Login_log GROUP BY username HAVING COUNT(date_time) >= 5)

delete from Login_log where date_time = (select min(date_time) from Login_log where username = 'kannan')

insert into Login_Log values ('kannan','2010-02-19 13:09:49.430','192.168.0.6','')

else

insert into Login_Log values('kannan','2010-02-19 13:09:48.430','192.168.0.6','')

else

insert into Login_Log values ('kannan','2010-02-19 14:22:45.227','192.168.0.6','')


error:

Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'else'.
Msg 156, Level 15, State 1, Line 13
Incorrect syntax near the keyword 'else'.


if i coment first insert query in work... but both insert and delete query are not working




Responses

Posted by: Vuyiswamb on: 2/19/2010 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Change it to this



if exists(select username from Login_Log where username = 'kannan')

begin
if exists (SELECT COUNT(date_time) FROM Login_log GROUP BY username HAVING COUNT(date_time) >= 5)
begin
delete from Login_log where date_time = (select min(date_time) from Login_log where username = 'kannan')

insert into Login_Log values ('kannan','2010-02-19 13:09:49.430','192.168.0.6','')
end
else
begin
insert into Login_Log values('kannan','2010-02-19 13:09:48.430','192.168.0.6','')
end end
else
begin
insert into Login_Log values ('kannan','2010-02-19 14:22:45.227','192.168.0.6','')
end


Thank you for posting at Dotnetfunda

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Kannan1986, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhi2434 on: 2/19/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
Always use Begin / End while you start a block.

I think this must be the problem with you.
After every If place a Begin and End when your block ends.

www.abhisheksur.com

Kannan1986, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nishithraj on: 2/24/2010 [Member] Bronze

Up
0
Down
Yes, you should use begin and end blocks for multiple statements. If there is only one tsql statement that is not required.

Mark this as answer, if it is.....

With regards
Nishithraj Narayanan

Kannan1986, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response