Click here to Skip to main content
15,896,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I created the following store procedure. But when I run, it arises error. The error message is :
"Msg 156, Level 15, State 1, Procedure sp_ManpowerCollection_TEST, Line 7
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Procedure sp_ManpowerCollection_TEST, Line 7
Incorrect syntax near ')'.
Msg 156, Level 15, State 1, Procedure sp_ManpowerCollection_TEST, Line 15
Incorrect syntax near the keyword 'else'."

Please help me. Can I use message like c# code in else portion?

Create proc [dbo].[sp_ManpowerCollection_TEST]
@ATTFromDate datetime,
@ATTToDate datetime,
@Location nvarchar(50)
as
Begin
if(not exists select COUNT(*) from dbo.tblBacManpoweCollection where ATTdate between '@ATTFromDate' and '@ATTToDate' and Location='@Location')
begin
SELECT EmpCode, ATTdate, LineCode,LineName,DesigCode,Designation,CompanyName,UnitName,DeptName,SecName,InHour,
InMin,OutHour,OutMin,Location,Counter,TotalOTHour,DayFlag,ProcessTime FROM tblManpowerCollection WHERE ATTdate between '@ATTFromDate' and '@ATTToDate' and Location='@Location'
delete tblManpowerCollection where ATTdate between '@ATTFromDate' and '@ATTToDate' and Location='@Location'
end
else
begin
Return 'False'
end
End
Posted

Don't you need brackets round the SELECT?
SQL
IF NOT EXISTS (SELECT COUNT(*)...
Instead of
SQL
IF (NOT EXISTS SELECT COUNT(*)...
 
Share this answer
 
Comments
Sumon562 9-Sep-13 4:51am    
Thank you for your kind response. I have modified the procedure. But now it tells that error near second 'begin'
You made several mistakes. First of all consult EXISTS syntax: http://technet.microsoft.com/en-us/library/ms188336.aspx[^]
1) count(*) will always return a row, thus EXISTS will always be true. Simply use * instead.
2) EXISTS is like a "function", you need parenthesis: if(not exists (select * from db...)...
 
Share this answer
 
There are syntax error in your SP Code. Try this for a hint

SQL
Create proc [dbo].[sp_ManpowerCollection_TEST]
(
@ATTFromDate datetime,
@ATTToDate datetime,
@Location nvarchar(50)
)
as
Begin
if(not exists select COUNT(*) from dbo.tblBacManpoweCollection where ATTdate between @ATTFromDate and @ATTToDate and Location=@Location)
begin

    delete tblManpowerCollection where ATTdate between @ATTFromDate and @ATTToDate and   Location=@Location

    SELECT EmpCode, ATTdate, LineCode,LineName,DesigCode,Designation,CompanyName,UnitName,DeptName,SecName,InHour,
InMin,OutHour,OutMin,Location,Counter,TotalOTHour,DayFlag,ProcessTime FROM tblManpowerCollection WHERE ATTdate between @ATTFromDate and @ATTToDate and Location=@Location

end
end
 
Share this answer
 
Comments
Sumon562 9-Sep-13 5:15am    
Thank you for your response. Can you help me how can I call it in C# code. We use a method to connect database. I provide you the method :

public void DBConnection(string strDBName)
{
try
{
if (IDisconnection != null)
{
if (IDisconnection.State == System.Data.ConnectionState.Open)
{
IDisconnection.Close();
}
IDisconnection = null;
IDisconnection = new SqlConnection();
}
else
{

IDisconnection = new SqlConnection();
}
strConnectionString = "Data Source=localhost;Initial Catalog=" + strDBName + ";User ID=sa;Password=sa1234";
IDisconnection.ConnectionString = strConnectionString;
IDisconnection.Open();
mblnTransactionStart = false;
}

catch (System.Data.SqlClient.SqlException e1)
{
throw (e1);
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
}
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900