Click here to Skip to main content
15,913,669 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
SQL
alter proc UDSP_GetContractNo
(
@Contractno nvarchar(50)
)
as

if( select id from tbl_ContractNo where Contractno=@Contractno )
else(insert into tbl_ContractNo(Contractno) values (@Contractno))


Hi this is my code. when i am executing it gives error.

Msg 4145, Level 15, State 1, Procedure UDSP_GetContractNo, Line 8
An expression of non-boolean type specified in a context where a condition is expected, near 'else'.
Msg 102, Level 15, State 1, Procedure UDSP_GetContractNo, Line 8
Incorrect syntax near ')'.
Posted

if expects a boolean condition - you are returning data.
I assume that you meant that if the item exists, return it, otherwise create it?
If so, then try something like:
SQL
IF (NOT EXISTS(SELECT id FROM tbl_ContractNo WHERE Contractno=@Contractno))
   INSERT INTO tbl_ContractNo (ContractNo ) VALUES (@ContractNo)

SELECT id FROM tbl_ContractNo WHERE Contractno=@Contractno
 
Share this answer
 
Use------------
SQL
IF NOT EXISTS( select id from tbl_ContractNo where Contractno=@Contractno)
BEGIN
insert into tbl_ContractNo(Contractno) values (@Contractno)
END
 
Share this answer
 
v2

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