带参数的ms sql server的扩展存储过程,怎么传递参数
3个回答
展开全部
一、MS SQL Server 中调用扩展存储过程时,传入和传出参数与普通的存储过程一样。
例:
--注:扩展存储过程名为“ExtSt”,传入参数有两个“3”和变量@STRSTR,
------传出参数有两个,@OutOK 与 @HEXSTRGet。
EXEC MASTER.DBO.ExtSt '3',@STRSTR,@OutOK OUTPUT,@HEXSTRGet OUTPUT
二、MS SQL 的存储过程编写要用到编程开发工具,比如微软VS系列中的VC,宝蓝公司的Delphi、C++Builder等。不同的编程环境下,编写的具体方式、代码差异非常大。下面是本人前些时候用VC写的实用代码,给出关键代码供有编程基础的朋友作参考:
//--dbcc ESQLDLL(free) --SQL 服务器上执行,删除扩展存储过程 ESQLDLL 语句
//exec sp_addextendedproc N'BeyExtSt', N'D:\VCSQLEXT\ESQLDLL.dll' ----SQL 服务器上执行,生成扩展存储过程 ESQLDLL 语句
RETCODE __declspec(dllexport) BeyExtSt(SRV_PROC *pSrvProc){
INT REBZ[1]; //SQLEXTL输出(状态标志)
CHAR ReArgsStr[256];//SQLEXT输出字串
ULONG ReArgsLen; //SQLEXT输出长度
INT nArgs; //传入参数个数 最多接受8个参数
CHAR InArgsChar1[256],InArgsChar2[256],InArgsChar3[256],InArgsChar4[256];//用于传入参数内容
CHAR InArgsChar5[256],InArgsChar6[256],InArgsChar7[256],InArgsChar8[256];//用于传入参数内容
ULONG InArgs1Len,InArgs2Len,InArgs3Len,InArgs4Len;//用于传入参数字符长
ULONG InArgs5Len,InArgs6Len,InArgs7Len,InArgs8Len;//用于传入参数字符长
nArgs = srv_rpcparams(pSrvProc);
sprintf_s(InArgsChar1,sizeof(InArgsChar1),"%s",(const char*)srv_paramdata(pSrvProc, 1));
sprintf_s(InArgsChar2,sizeof(InArgsChar2),"%s",(const char*)srv_paramdata(pSrvProc, 2));
sprintf_s(InArgsChar3,sizeof(InArgsChar3),"%s",(const char*)srv_paramdata(pSrvProc, 3));
sprintf_s(InArgsChar4,sizeof(InArgsChar4),"%s",(const char*)srv_paramdata(pSrvProc, 4));
sprintf_s(InArgsChar5,sizeof(InArgsChar5),"%s",(const char*)srv_paramdata(pSrvProc, 5));
sprintf_s(InArgsChar6,sizeof(InArgsChar6),"%s",(const char*)srv_paramdata(pSrvProc, 6));
sprintf_s(InArgsChar7,sizeof(InArgsChar7),"%s",(const char*)srv_paramdata(pSrvProc, 7));
sprintf_s(InArgsChar8,sizeof(InArgsChar8),"%s",(const char*)srv_paramdata(pSrvProc, 8));
InArgs1Len = srv_paramlen(pSrvProc, 1);
InArgs2Len = srv_paramlen(pSrvProc, 2);
InArgs3Len = srv_paramlen(pSrvProc, 3);
InArgs4Len = srv_paramlen(pSrvProc, 4);
InArgs5Len = srv_paramlen(pSrvProc, 5);
InArgs6Len = srv_paramlen(pSrvProc, 6);
InArgs7Len = srv_paramlen(pSrvProc, 7);
InArgs8Len = srv_paramlen(pSrvProc, 8);
if(InArgsChar1[0] == '0'){//###########功能(0),功能测试##########
srv_describe(pSrvProc, 1, "NO1", SRV_NULLTERM, SRVCHAR, InArgs1Len, SRVCHAR, InArgs1Len, (void*)InArgsChar1);
srv_describe(pSrvProc, 2, "NO2", SRV_NULLTERM, SRVCHAR, InArgs2Len, SRVCHAR, InArgs2Len, (void*)InArgsChar2);
srv_describe(pSrvProc, 3, "NO3", SRV_NULLTERM, SRVCHAR, InArgs3Len, SRVCHAR, InArgs3Len, (void*)InArgsChar3);
srv_describe(pSrvProc, 4, "NO4", SRV_NULLTERM, SRVCHAR, InArgs4Len, SRVCHAR, InArgs4Len, (void*)InArgsChar4);
srv_describe(pSrvProc, 5, "NO5", SRV_NULLTERM, SRVCHAR, InArgs5Len, SRVCHAR, InArgs5Len, (void*)InArgsChar5);
srv_describe(pSrvProc, 6, "NO6", SRV_NULLTERM, SRVCHAR, InArgs6Len, SRVCHAR, InArgs6Len, (void*)InArgsChar6);
srv_describe(pSrvProc, 7, "NO7", SRV_NULLTERM, SRVCHAR, InArgs7Len, SRVCHAR, InArgs7Len, (void*)InArgsChar7);
srv_describe(pSrvProc, 8, "NO8", SRV_NULLTERM, SRVCHAR, InArgs8Len, SRVCHAR, InArgs8Len, (void*)InArgsChar8);
srv_sendrow(pSrvProc);
sprintf_s(ReArgsStr,sizeof(ReArgsStr),"Hello Word!");
ReArgsLen = (ULONG)strlen(ReArgsStr);
REBZ[0] = 0;srv_paramsetoutput(pSrvProc,9,(BYTE*)REBZ,1,FALSE);
srv_paramsetoutput(pSrvProc,10,(BYTE*)ReArgsStr,ReArgsLen,FALSE);
srv_senddone(pSrvProc, SRV_DONE_FINAL, 0, 0);
/*
DECLARE @ab CHAR(65)
EXEC BeyExtSt '0','MyXXXxxxx','9911223','2','2',NULL,'','',@ab OUTPUT
SELECT @ab AS AABB
*/
}
if(InArgsChar1[0] == '1'){//###########功能(1),生成特征码##########
//------- ...............省略
}
if(InArgsChar1[0] == '2'){//###########功能(2),生成注册码##########
//------- ...............省略
}
if(InArgsChar1[0] == '3'){//###########功能(3),加密登录码(需要校核注册)##########
//------- ...............省略
}
if(InArgsChar1[0] == '4'){//###########功能(4),回译登录码(需要校核注册)##########
//------- ...............省略
}
return XP_ERROR;
}
2015-03-26
展开全部
确切的说不行-SQL SERVER没有数组类型,ANSI SQL 92标准也不支持数组。但可用其它的方法来实现。
1. You could simulate an array by passing one or more varchar(255) fields with comma-separated values and then use a WHILE loop with PATINDEX and SUBSTR to extract the values.
1、你可以使用几个VARCHAR(255)字段来模拟数组,字段中用逗号分开各个数据,然后使用循环和PATINDEX和SUBSTR分开这些数据。
2. The more usual way to do this would be to populate a temporary table with the values you need and then use the contents of that table from within the stored-procedure. Example of this below 2、通常这种方法需要为这些数据创建一个临时表,然后在存储过程使用表中的内容。如下例 create procedure mytest @MyParmTempTable varchar(30) as begin -- @MyParmTempTable contains my parameter list... 这个变量是包含参数的表名 -- For simplicity use dynamic sql to copy into a normal temp table...
create table #MyInternalList (
list_item varchar( 2 ) not null ) set nocount on insert #MyInternalList
select *
from sysobjects
create table #MyList (
list_item varchar( 2 ) not null ) insert #MyList values ( 'S' )
insert #MyList values ( 'U' )
insert #MyList values ( 'P' ) exec mytest "#MyList" 3. If all you wanted to do was use the array/list as input to an IN clause in a WHERE statement you could use :- 3、如果你想在IN子句里使用输入的数组参数可以这样做: CREATE PROCEDURE sp_MyProcedure (@MyCommaDelimitedString
1. You could simulate an array by passing one or more varchar(255) fields with comma-separated values and then use a WHILE loop with PATINDEX and SUBSTR to extract the values.
1、你可以使用几个VARCHAR(255)字段来模拟数组,字段中用逗号分开各个数据,然后使用循环和PATINDEX和SUBSTR分开这些数据。
2. The more usual way to do this would be to populate a temporary table with the values you need and then use the contents of that table from within the stored-procedure. Example of this below 2、通常这种方法需要为这些数据创建一个临时表,然后在存储过程使用表中的内容。如下例 create procedure mytest @MyParmTempTable varchar(30) as begin -- @MyParmTempTable contains my parameter list... 这个变量是包含参数的表名 -- For simplicity use dynamic sql to copy into a normal temp table...
create table #MyInternalList (
list_item varchar( 2 ) not null ) set nocount on insert #MyInternalList
select *
from sysobjects
create table #MyList (
list_item varchar( 2 ) not null ) insert #MyList values ( 'S' )
insert #MyList values ( 'U' )
insert #MyList values ( 'P' ) exec mytest "#MyList" 3. If all you wanted to do was use the array/list as input to an IN clause in a WHERE statement you could use :- 3、如果你想在IN子句里使用输入的数组参数可以这样做: CREATE PROCEDURE sp_MyProcedure (@MyCommaDelimitedString
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
与普通的sp一样传参。
什么应用场景?
什么应用场景?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询