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
 if statement

Author  Topic 

sujeethbala2110
Starting Member

29 Posts

Posted - 2006-09-24 : 06:54:27
what is the problem with this programe. its giving an error
'incorrect syntax near @flag'

CREATE PROCEDURE spUpdateStock
@flag varchar(70)
AS
BEGIN
if exists(Select fld1 from table1 where fld1=Convert(varchar,Getdate(),101))
(
set @flag="asdsdfsaf"
exit
)
else
statemet 1

--multiple insert statement follows

END
GO

suji

Kristen
Test

22859 Posts

Posted - 2006-09-24 : 08:42:26
CREATE PROCEDURE spUpdateStock

You should prefix spUpdateStock by the owner - e.g. "dbo."

@flag varchar(70)

A parameter called "flag" seems unlikely to have a datatype of 70 characters

AS
BEGIN
if exists(Select fld1 from table1 where fld1=Convert(varchar,Getdate(),101))

You should specify the width of the VARCHAR in "Convert(varchar,Getdate(),101)"

You should use "SELECT *" and not "SELECT fld1"

(

You don't use brackets after IF EXIST, you need to use BEGIN / END

set @flag="asdsdfsaf"
exit

There is no such keyword EXIT

)
else
statemet 1

If "statemet 1" is a SProc call you need to use EXEC

--multiple insert statement follows

if this multiple insert statement is intended to be part of the ELSE, rather than only "statemet 1", then you need to surround it with BEGIN / END (good practice anyway, even if its a single statement)

END
GO

Lots of basic mistakes here, looks like you need some basic instruction - a book, course or some of the beginner material on the Internet.

Kristen
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-09-24 : 08:43:04
after your if statement should come the begin and end block ie


CREATE PROCEDURE spUpdateStock

AS

declare @flag varchar(70)
if exists(Select fld1 from table1 where fld1=Convert(varchar,Getdate(),101))
BEGIN


set @flag="asdsdfsaf"

end
else
statemet 1



also if you are setting the value of @flag in your code then it would be better to declare this variable at runtime rather than as a parameter to be passed in, something like above
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-09-24 : 08:43:39
[code]
if exists(Select fld1 from table1 where fld1=Convert(varchar,Getdate(),101))
begin
set @flag = 'asdsdfsaf' -- use single quote not double quote
exit return
end
[/code]


KH

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-09-24 : 10:16:24



KH

Go to Top of Page

sujeethbala2110
Starting Member

29 Posts

Posted - 2006-09-24 : 10:57:40
ya i am new to sql. thanks for reply.

suji
Go to Top of Page

sujeethbala2110
Starting Member

29 Posts

Posted - 2006-09-24 : 11:48:49
where i will get the beginner meterial. please give me some ref.

suji
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2006-09-24 : 14:10:00
excellent resource: BOOKS ONLINE, press the F 1 Key

Also google is your friend

http://www.google.com/search?hl=en&q=learn+ms+sql
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2006-09-24 : 21:26:39
Search for Ken Henderson and buy everything written by him.

http://www.amazon.com/s/ref=br_ss_hs/002-8022302-4782405?platform=gurupa&url=index%3Dblended&keywords=ken+henderson

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -