首页 > 代码库 > SQLServer存储过程

SQLServer存储过程

--无参数存储过程创建和调用
--创建存储过程usp_testusergoif exists(select * from sysobjects where name=‘usp_test‘)drop proc usp_testgocreate proc usp_testasselect * from testgo--执行存储过程exec usp_testgo
--有参存储过程创建和调用--创建存储过程user testif exists (select * from sysobjects where name=‘usp_test‘)drop proc usp_testgocreate proc usp_test@name varchar(32),@age int = nullas if @age is nullbegin select * from test where name = @nameendelse begin select * from test where name = @name and age = @ageendgo--调用存储过程exec usp_test @name=‘username‘go    

  

--有参数、输出存储过程和调用--统计某年龄的人数--创建存储过程use testif exists (select * from sysobjects where name=‘usp_test‘)drop proc usp_testgocreate proc usp_test@age varchar(32),@count int outputas set @count = (select count(1) from test where age = @age)go--执行存储过程declare @age varchar(32)declare @count int set @age = ‘26‘exec usp_test @age ,@count outputprint @countgo 

  

SQLServer存储过程