Create Proc yangyang8848.OneGoods
@GoodsCode varchar(10)
As
Select * From Master_Goods Where GoodsCode = @GoodsCode
Go
调用的代码:
Declare @Code varchar(10)
Set @Code = '0004'
Exec yangyang8848.OneGoods @Code
在参数的后边加入Output 表明该参数为输出参数。
Create Proc yangyang8848.OneGoods
@GoodsCode2 varchar(10) output,@GoodsCode varchar(10) = '0011'
As
Select * From Master_Goods Where GoodsCode = @GoodsCode
Set @GoodsCode2 = '0005'
Go
调用方法:
Declare @VV2 varchar(10)
Exec yangyang8848.OneGoods @Code out
[RECOMPILE]:指示数据库引擎 不缓存该过程的计划,该过程在运行时编译。如果指定了 FOR REPLICATION,则不能使用此选项。对于 CLR 存储过程,不能指定 RECOMPILE。
这个说一个非常好用的函数 OBJECT_ID :返回架构范围内对象的数据库对象标识号。
例如:我们创建存储过程时,可以如下写代码
If Object_ID('yangyang8848.OneGoods') Is Not Null
Drop Proc yangyang8848.OneGoods
Go
Create Proc yangyang8848.OneGoods
@GoodsCode2 varchar(10) out,@GoodsCode varchar(10) = '0011'
As
Select * From Master_Goods Where GoodsCode = @GoodsCode
Set @GoodsCode2 = '0005'
Go
针对于上边的这个存储过程,我们调用以下SQL查询
Select definition From sys.sql_modules
Where object_id = Object_ID('yangyang8848.OneGoods');
我们是可以查到结果的。
可是如果我们对该存储过程加入[ ENCRYPTION ] 那么你将无法看到任何结果
If Object_ID('yangyang8848.OneGoods') Is Not Null
Drop Proc yangyang8848.OneGoods
Go
If Object_ID('dbo.GetMasterGoods') Is Not Null
Drop Proc dbo.GetMasterGoods
Go
Create Proc GetMasterGoods
@MyCursor Cursor Varying Output
With Encryption
As
Set @MyCursor = Cursor
For
Select GoodsCode,GoodsName From Master_Goods
Open @MyCursor
Go
--下边建立另外一个存储过程,用于遍历游标输出结果
Create Proc GetAllGoodsIDAndName
As
Declare @GoodsCode varchar(18)
Declare @GoodsName nvarchar(20)
Declare @MasterGoodsCursor Cursor
Exec GetMasterGoods @MasterGoodsCursor out
Fetch Next From @MasterGoodsCursor
InTo @GoodsCode,@GoodsName
While(@@Fetch_Status = 0)
Begin
Begin
Print @GoodsCode + ':' + @GoodsName
End
Fetch Next From @MasterGoodsCursor
InTo @GoodsCode,@GoodsName
End
Close @MasterGoodsCursor
Deallocate @MasterGoodsCursor
Go