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
 General SQL Server Forums
 New to SQL Server Programming
 Syntax Errors in SP!

Author  Topic 

majidbhutta
Starting Member

13 Posts

Posted - 2005-11-30 : 06:51:55
Hi Dears Below is the Sp that i want to use but there are many Syntax erros plz verify and correct them Thnx

create procedure CheckStatus

@EmpID int
as
-- i need some variables so i declared them
declare @ShiftId int
declare @DayTime smalldatetime
declare @ShiftStartT smalldatetime
declare @ShiftEndT smalldatetime
declare @ShiftInOutStatus int
declare @ShiftValidity bit
declare @RTValue int


-- Now i m assigning the values to the declared variable by selecting some value from tables
-- from this point i m having syntax erroes i dont know what are they all atables and all fields are ok verified

set @ShiftId=SELECT S_Id FROM Emp_Info WHERE Emp_Info.Emp_Id=@EmpID
set @DayTime=SELECT GETDATE()
set @ShiftStartT=SELECT S_ST FROM Emp_Shift WHERE Emp_Shift.S_Id=@ShiftId
set @ShiftEndT=SELECT S_ET FROM Emp_Shift WHERE Emp_Shift.S_Id=@ShiftId
set @InOutStatus=SELECT IOStatus FROM Emp_Status WHERE Emp_Status.Emp_Id=@EmpId
-- here i m using a nested case for some checks to insert values into tables
CASE -- Main Case
WHEN @InOutStatus=0 OR @InOutStatus=-1
-- Calling udf_IsShiftValid(datetime,datetime,datetime) a function that ia also ok and returns two possible value 0 or 1
THEN @ShiftValidty=udf_IsShiftValid(@ShiftStartT,@ShiftEndT,@DayTime)
CASE --nested case starts here
WHEN @ShiftValidity=1-- Insertin value into Emp_Status table and assihning value to @RTValue
THEN (INSERT INTO Emp_Status(IOStatus) VALUES(@ShiftValidity) WHERE Emp_Status.Emp_Id=@EmpID
SET @RTValue=1)
WHEN @ShiftValidity=0 THEN SET @RTValue=-1
END-- Nested case End Here
WHEN @InOutStatus=1
THEN
INSERT INTO Emp_Status(IOStatus) VALUES(0) WHERE Emp_Status.Emp_Id=@EmpID
SET @RTValue=0
END

RETURN @RTValue

GO

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-11-30 : 07:02:22
quote:
Originally posted by majidbhutta


Hi Dears Below is the Sp that i want to use but there are many Syntax erros plz verify and correct them Thnx

create procedure CheckStatus

@EmpID int
as
-- i need some variables so i declared them
declare @ShiftId int
declare @DayTime smalldatetime
declare @ShiftStartT smalldatetime
declare @ShiftEndT smalldatetime
declare @ShiftInOutStatus int
declare @ShiftValidity bit
declare @RTValue int


-- Now i m assigning the values to the declared variable by selecting some value from tables
-- from this point i m having syntax erroes i dont know what are they all atables and all fields are ok verified

set @ShiftId=SELECT S_Id FROM Emp_Info WHERE Emp_Info.Emp_Id=@EmpIDnot the syntax error but see to it that it just return only one record..
set @DayTime=SELECT GETDATE()
set @ShiftStartT=SELECT S_ST FROM Emp_Shift WHERE Emp_Shift.S_Id=@ShiftIdnot the syntax error but see to it that it just return only one record..
set @ShiftEndT=SELECT S_ET FROM Emp_Shift WHERE Emp_Shift.S_Id=@ShiftIdnot the syntax error but see to it that it just return only one record..
set @InOutStatus=SELECT IOStatus FROM Emp_Status WHERE Emp_Status.Emp_Id=@EmpIdnot the syntax error but see to it that it just return only one record..
-- here i m using a nested case for some checks to insert values into tables
CASE -- Main
Case	WHEN @InOutStatus=0 OR @InOutStatus=-1
-- Calling udf_IsShiftValid(datetime,datetime,datetime) a function that ia also ok and returns two possible value 0 or 1
THEN @ShiftValidty=udf_IsShiftValid(@ShiftStartT,@ShiftEndT,@DayTime)
CASE --nested case starts here
WHEN @ShiftValidity=1-- Insertin value into Emp_Status table and assihning value to @RTValue
THEN (INSERT INTO Emp_Status(IOStatus) VALUES(@ShiftValidity) WHERE Emp_Status.Emp_Id=@EmpID
SET @RTValue=1)
WHEN @ShiftValidity=0 THEN SET @RTValue=-1
END-- Nested case End Here
WHEN @InOutStatus=1
THEN
INSERT INTO Emp_Status(IOStatus) VALUES(0) WHERE Emp_Status.Emp_Id=@EmpID
SET @RTValue=0
END


Instead of the above code use the series of if and else if .. since case can only work in the select clause..

somthing like this

if @InOutStatus=0 OR @InOutStatus=-1
-- What Ever you want to do ..
Else if ..
-- do somthing ..

.......
...

Else

-- Default..

RETURN @RTValue

GO



Hope this helps ..

Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2005-11-30 : 07:04:57
there are several error noted. If you are not familiar with the syntax, you should code bit and bit and test first before code the whole chunck and get several errors and don't know where to start.

Listed here are several error noted. You may try to correct it first and then test your code bit by bit.
1. SET
for set either you do this
set @ShiftId=(SELECT S_Id FROM Emp_Info WHERE Emp_Info.Emp_Id=@EmpID)
or
SELECT ShiftId = S_Id FROM Emp_Info WHERE Emp_Info.Emp_Id=@EmpID


2. @InOutStatus is not declared

3. You can use CASE in this way. use IF..ELSE instead

-----------------
[KH]
Go to Top of Page

shallu1_gupta
Constraint Violating Yak Guru

394 Posts

Posted - 2005-11-30 : 07:07:17
Hi,
Use If Statement instead of Case. Search BOL for Select Case Usage.
Insert Statement with Where Clause???
Are u sure of what u r trying to achieve ??

Go to Top of Page

chiragkhabaria
Master Smack Fu Yak Hacker

1907 Posts

Posted - 2005-11-30 : 07:21:10
It would be great if you can post the expected result.. and some other data.. out of this stored procedure....

Since there are lots logical as well as the syntactical error in ur proc..



Sucess Comes to those who Believe in Beauty of their Dream..
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2005-11-30 : 09:37:47
Give more details described here
http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -