22,298
社区成员
发帖
与我相关
我的任务
分享
declare @cid
select @cid=id from a_company where com_id=1113
if @cid<=0
/***********************************
添加公司IP的存储过程
************************************/
if exists (select * from sysobjects where name='Proc_Add_CID')
drop procedure Proc_Add_CID
go
create procedure Proc_Add_CID
@cid int,--公司的ID,与a_company匹配
@result int output
as
/*
@a_com_id:存放a_company的id
@v_cid存放Vote的CID,用于判断是否存在该id,如果存在就不用添加
*/
declare @a_com_id int,@v_cid int
--先查询a_company表,输入的公司ID是否存在于该表,不存在就不能添加
select @a_com_id=id from a_company where id=@cid
if @a_com_id<=0
set @result=-1
return @result
--先判断是否存在该公司ID,存在就不用重复添加
select @v_cid=id from Vote where Com_id=@cid
if @v_cid>0
set @result=0
return @result
insert into Vote(Com_id) values(@cid)
set @result=1
return @result
--执行存储过程
declare @resu int
exec Proc_Add_CID 35,@resu output
--print convert(varchar,@resu)
--select top 10 * from a_company
if @resu<=0
print '添加失败'
return
print '添加成功'
添加公司IP的存储
************************************/
if exists (select * from sysobjects where name='Proc_Add_CID')
drop procedure Proc_Add_CID
go
create procedure Proc_Add_CID
@cid int,--公司的ID,与a_company匹配
@result int output
as
begin
/*
@a_com_id:存放a_company的id
@v_cid存放Vote的CID,用于判断是否存在该id,如果存在就不用添加
*/
--先查询输入的公司ID是否存在于a_company表,不存在就不能添加
if exists(select 1 from a_company where id=@cid)
begin
--在判断vote表是否存在该ID,存在就不能重复添加
if exists(select 1 from Vote where Com_id=@cid)
begin
set @result=0
end
else
begin--Vote表中没该CID
insert into Vote(Com_id) values(@cid)
set @result=1
end
end
else
begin
set @result=-1
end
end--最靠外的begin end语句
create table #a (com_id int,id int)
go
insert into #a
select 1 com_id,11 id
go
declare @cid int
select @cid=id from #a where com_id=1113
select @cid
select ISNULL(@cid,0)
--result:
-----------
NULL
(1 行受影响)
-----------
0
(1 行受影响)
declare @cid varchar(20)
--declare @sql varchar(200)
select basedataname from sysbasedata where basedataid =0
select @cid=isnull(basedataname,'0') from sysbasedata where basedataid = 0
if @cid ='0'
print '结果为0'
else
print '结果不为0'
select basedataname from sysbasedata where basedataid =0
--try
if ISNULL(@a_com_id,0)<=0
if ISNULL(@v_cid,0)>0
MERGE Production.ProductInventory AS target
USING (SELECT ProductID, SUM(OrderQty) FROM Sales.SalesOrderDetail AS sod
JOIN Sales.SalesOrderHeader AS soh
ON sod.SalesOrderID = soh.SalesOrderID
AND soh.OrderDate = @OrderDate
GROUP BY ProductID) AS source (ProductID, OrderQty)
ON (target.ProductID = source.ProductID)
WHEN MATCHED AND target.Quantity - source.OrderQty <= 0
THEN DELETE
WHEN MATCHED
THEN UPDATE SET target.Quantity = target.Quantity - source.OrderQty,
target.ModifiedDate = GETDATE()
OUTPUT $action, Inserted.ProductID, Inserted.Quantity, Inserted.ModifiedDate, Deleted.ProductID,
Deleted.Quantity, Deleted.ModifiedDate;
declare @cid varchar(20)
select @cid=isnull(id,'0') from a_company where com_id = 3
print @cid
select @a_com_id=id from a_company where id=@cid
这种写法不好
反正都是等于id
if exists(select 1 from a_company where id=@cid) and @cid>0
set @result=0
else
set @result= 改成你想要的值 --当然如果不存在,肯定返回null ,这个你自己改成想要的值
return @result