存储过程的用法及与C#代码的不同

下面是个存储过程的应用

 

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:  <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName>
 -- Add the parameters for the stored procedure here
 @title nvarchar(100),  --新闻标题
 @content TEXT,         --新闻内容
 @pubUser NVARCHAR(50), --发布人
 @catids varchar(200),  --新闻类别列表,用“:”分割
 @error nvarchar(200) OUTPUT  --用来返回错误信息
AS
BEGIN
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT ON;
 declare @newsid int  --新闻id
 declare @catid varchar(10) --新闻类别id
 declare @pos int  --类别列表中分隔符":"的位置
   
    begin transaction
    begin try
        insert into newscontent(title,[content],pubuser)values(@title,@content,@pubUser)
        set @newsid=@@identity

        while(len(@catids)>0)
        begin
            set @pos=charindex(':',@catids)
   if(@pos<>0)
   begin
    set @catid=substring(@catids,1,@pos-1)
    set @catids=substring(@catids,@pos+1,@pos)
   end
   else
   begin
    set @catid=@catids
    set @catids=''
   end
   insert into newscategory(newsid,catid)values(@newsid,cast(@catid as int))
        end
  commit transaction
  return 0
    end try
 begin catch
  set @error=error_message()
  rollback transaction
  return 1
    end catch
END

 

在存储中语句的内容要写在begin和end之间

例如:

c#中的if语句

if(){};

而存储过程中的if语句

if()

begin 语句  end

 

catch语句写成

begin catch       

//语句

end catch

 

在就是声明变量的方法不一样 用declare @num nvarchar形式 声明

赋值使用set赋值

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值