有关sql server存储过程带 查询带条件 的一些写法

-- use accmain

-- 删除表和过程
/**
 drop table tb_Server
 drop procedure sp_add
**/


/**
CREATE TABLE  tb_Server(
   id int identity(1,1) PRIMARY KEY,
   s_wmsccp  nvarchar(200),
   s_ip  nvarchar(200)
)
**/


Create PROC sp_add
@u_wmsccp nvarchar(50),
@s_ip nvarchar(50)
AS
declare @cid varchar(300)  -- 可以声明多个变量。
select @cid=count(id) from tb_server where s_ip=@s_ip
if @s_ip<>''
begin
IF @cid='0'--当没有记录时
 BEGIN
 Insert INTO tb_Server(s_wmsccp, s_ip)VALUES (@u_wmsccp, @s_ip)
 END
ELSE--当有记录时
 BEGIN
 Update tb_Server SET s_wmsccp = @u_wmsccp Where (s_ip = @s_ip)
 END
end

-- 执行存储过程
exec  sp_add 'jack555','666555';

-- 查询执行结果
select * from tb_Server

猜你喜欢

转载自jackroomage.iteye.com/blog/1583097