Friday, March 23, 2012

Ineed the Right Syntax

Hello,

I need the Right Syntax of this Staement

IF(select count(*) from Department where Department_Id=@.De_Id)=0

begin

insert into Depatment vaule.....etc

esle

update

end

just i have problem with the syntax of first If Statement

Something like this would be a bit more efficient.

Searching stops when the first row is encountered, whereas SELECT count(*) has to search the entire filtered set.

Also, 'IF' does not require an 'END' as in Visual Basic.

Code Snippet

IF EXISTS

( SELECT 1

FROM Department

WHERE Department_ID = @.De_ID

)

INSERT INTO Department ( Columns )

VALUES ( @.Values )

ELSE

UPDATE Department

SET

Column1 = @.Value1,

etc.

|||

For anyone who is currently doing the first touch to the new version of SQL Server 2008, you should try the MERGE statement (aka UPSERT) which was requested for many of the previous version and is finally implemented.

Jens K. Suessmeyer.


http://www.sqlserver2005.de

No comments:

Post a Comment