sqlserver if语句 [英] sqlserver if statement

查看:558
本文介绍了sqlserver if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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))



这是我的代码.当我执行它给错误.

消息4145,级别15,状态1,过程UDSP_GetContractNo,第8行
在预期有条件的上下文中在"else"附近指定的非布尔类型的表达式.
消息102,级别15,状态1,过程UDSP_GetContractNo,第8行



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 '')''.

推荐答案

if期望布尔条件-您正在返回数据.
我认为您的意思是如果该项目存在,则将其返回,否则创建它?
如果是这样,请尝试以下操作:
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:
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


使用------------
Use------------
IF NOT EXISTS( select id from tbl_ContractNo where Contractno=@Contractno)
BEGIN
insert into tbl_ContractNo(Contractno) values (@Contractno)
END


这篇关于sqlserver if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆