php调用sqlserver存储过程实例,带输入输出参数,经测试可用

因工作原因最近需要用到调用sqlserver存储过程,并且需要输入输出参数,因为单纯的参考网上的一些例子总有问题,最后自己解决后决定写一写解决的过程。并把自己写的3个方案都放上,记录一下自己的思路过程,经测试方案1为最优方案,性能比方案2,方案3,在10万数量级快6倍左右。

首先存储过程如下(其中带*的需要注意):


方案1:
CREATE PROCEDURE [dbo].[UPDATE_STOCK]
	(
		@Updatetime  varchar(50),
		@Orgguid  varchar(50),
		@ReturnMsg    varchar(1000) output,         -- 返回提示或错误信息
		@ReturnValue    varchar(50) output        -- 返回提示或错误信息
	)
AS
	declare @SERVER varchar(500);
	declare @UID varchar(200);
	declare @PWD varchar(200);
BEGIN
	SET NOCOUNT ON; --如果想只获取输出参数,而不被存储过程里面的exec影响则需加上该设置,关闭影响的行数才行
	declare @NUM NUMERIC(10,0); --记录个数
	declare @per_page NUMERIC(10,0) = 500; --每页记录个数,经测试每页500个时间最快,而且超过1000多个好像就会出问题
	DECLARE @temp VARCHAR(500);
	DECLARE @dosql NVARCHAR(1000);
	SET @SERVER='192.168.100.2';
	SET @UID='sa';
	SET @PWD='testsql';

	--定义表变量(******)
	DECLARE @tb1 Table
  (
			SubCode VARCHAR(50),
	  ItemCode VARCHAR(50),
			StockQuantity NUMERIC(18,4),
			UnitPrice NUMERIC(18,6),
			IS_EXISTS INT,
			NUM INT
  );
	--远程查询库存信息(******)
	SET @dosql = 'SELECT SubCode,ItemCode,StockQuantity,UnitPrice,IS_EXISTS,NUM FROM 
				(
					SELECT a.SubCode,a.ItemCode,ISNULL(StockQuantity, 0) StockQuantity,ISNULL(UnitPrice, 0) UnitPrice,
						case when ISNULL(sbs.GOODSGUID, 0)=0 then 0 else 1 end as IS_EXISTS,ROW_NUMBER() over (order by UnitPrice ASC) as NUM 
					FROM OPENROWSET(''MSDASQL'',''DRIVER={SQL Server};SERVER='+@SERVER+';UID='+@UID+';PWD='+@PWD+''',
					WeiShopDB_empty.dbo.i_EntSubInfoNew) AS a 
					LEFT JOIN 
					(
							SELECT GOODSGUID,BRANCHGUID,ORGGUID FROM SHOP_BRANCH_STOCKCOUNT sbs1 GROUP BY GOODSGUID,BRANCHGUID,ORGGUID
					) sbs ON sbs.GOODSGUID=a.ItemCode AND sbs.ORGGUID=a.EntGuid AND sbs.BRANCHGUID=a.SubCode
					WHERE Billtime>='''+@Updatetime+''' and EntGuid='''+@Orgguid+'''
				) t';
	INSERT into @tb1 EXEC(@dosql); --插入表变量 (******)
	
	--获取记录的总个数
	SELECT @NUM = count(SubCode) from @tb1;
	DECLARE @i INT = 0; --循环计数
	DECLARE @now_time VARCHAR(25) = CONVERT(varchar(25), GETDATE(), 21); --当前时间

	IF(@NUM > 0)
		BEGIN
			
			--循环查询库存记录并进行更新,如果之前库存已经存在则更新,否则插入
			WHILE ( @i < CEILING(@NUM/@per_page) )
				BEGIN
					
					DECLARE @do_insert_sql nvarchar(MAX) = ''; --最后执行的insert sql语句
					DECLARE @do_update_sql nvarchar(MAX) = ''; --最后执行的update sql语句
					DECLARE @start_index INT = @i*CAST(@per_page AS INT)+1; --开始的索引值
					DECLARE @end_index INT = CAST(@start_index as INT)+CAST(@per_page AS INT)-1; --结束的索引值
					
					DECLARE @SubCode VARCHAR(50),@ItemCode VARCHAR(50),@StockQuantity NUMERIC(18,4),@UnitPrice NUMERIC(18,6),@IS_EXISTS INT,@RECORD_NUM INT;
				
					DECLARE STOCK_CURSOR CURSOR LOCAL for (
						SELECT SubCode,ItemCode,StockQuantity,UnitPrice,IS_EXISTS,NUM FROM @tb1
						WHERE NUM BETWEEN @start_index AND @end_index
					)

					Begin Tran;--开启事务
					--打开游标
					OPEN STOCK_CURSOR;
					--开始循环游标变量
					FETCH next from STOCK_CURSOR into @SubCode,@ItemCode,@StockQuantity,@UnitPrice,@IS_EXISTS,@RECORD_NUM;
					WHILE (@@FETCH_STATUS =0)
						BEGIN
							IF (@IS_EXISTS = 1)
								BEGIN
								--更新
									SET @do_update_sql = @do_update_sql+'UPDATE SHOP_BRANCH_STOCKCOUNT SET STOCKCOUNT='+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+',USERPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+',MARKETPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+',UPDATETIME='''+@now_time+''' 
									WHERE GOODSGUID='+@ItemCode+' AND BRANCHGUID='+@SubCode+';';

								END
							ELSE
								BEGIN
								--插入
									DECLARE @RECORD_GUID VARCHAR(50)=NEWID();
									IF ( ISNULL(@do_insert_sql, '') = '')
										BEGIN
											SET @do_insert_sql = @do_insert_sql+'SELECT '''+@RECORD_GUID+''','''+@Orgguid+''','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','''+@ItemCode+''','''+@SubCode+''','''+@now_time+'''';
										END
									ELSE
										BEGIN
											SET @do_insert_sql = @do_insert_sql+' UNION ALL SELECT '''+@RECORD_GUID+''','''+@Orgguid+''','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','''+@ItemCode+''','''+@SubCode+''','''+@now_time+'''';
										END
								END
							--让游标指向下一条记录
							FETCH next from STOCK_CURSOR into @SubCode,@ItemCode,@StockQuantity,@UnitPrice,@IS_EXISTS,@RECORD_NUM;
						END

					SET @i = @i+1;
					/***************************执行一遍更新或者插入语句*********************************/
					IF(ISNULL(@do_update_sql, '') <> '')
						BEGIN
							exec sp_executesql @do_update_sql; --如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数

							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = 'update record error';	
									GOTO SQLERR1;
								END
						END

					IF(ISNULL(@do_insert_sql, '') <> '')
						BEGIN
							SET @do_insert_sql = 'INSERT INTO SHOP_BRANCH_STOCKCOUNT(GUID,ORGGUID,STOCKCOUNT,USERPRICE,MARKETPRICE,GOODSGUID,BRANCHGUID,UPDATETIME) '+@do_insert_sql;

							--如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数
							exec sp_executesql @do_insert_sql; 
							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = 'insert record error';	
									GOTO SQLERR1;
								END
						END

					Commit Tran; --提交事务
					--关闭游标
					CLOSE STOCK_CURSOR;
					--销毁游标
					DEALLOCATE STOCK_CURSOR;

				END
			Set @ReturnMsg='success';
		END
	ELSE
		BEGIN
			Set @ReturnMsg = 'data empty';
		END
	SET NOCOUNT OFF; 
	Set @ReturnValue='1';
	return;
	/*************************************异常处理*********************************************************************/
	SQLErr1:
	RollBack Transaction;
	Set @ReturnValue = '-1';
	Return;
END




方案2:
CREATE PROCEDURE [dbo].[UPDATE_STOCK]
	(
		@Updatetime  varchar(50),                --更新时间
		@Orgguid  varchar(50),                   --组织机构guid
		@ReturnMsg    varchar(50) output,         -- 返回提示或错误信息
		@ReturnValue    varchar(50) output        -- 返回提示或错误信息
	)
AS
	--因存储过程中需要远程连接其他数据库所以需要以下参数
	declare @SERVER varchar(500); --服务器名称
	declare @UID varchar(200); --数据库用户名
	declare @PWD varchar(200); --密码
BEGIN
	SET NOCOUNT ON; --如果想只获取输出参数,而不被存储过程里面的exec影响则需加上该设置,关闭影响的行数才行*
	declare @NUM NUMERIC(10,0); --记录个数
	declare @per_page NUMERIC(10,0)=1000; --每页记录个数
	SET @SERVER='192.168.100.2'; 
	SET @UID='sq';
	SET @PWD='testsql';
	
	--远程查询库存信息
	--获取记录的总个数
	--这里是远程连接数据库的操作部分*
	SELECT @NUM=COUNT(Guid) FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=192.168.100.2;UID=sa;PWD=testsql',
  WeiShopDB_empty.dbo.i_EntSubInfoNew) AS a WHERE Billtime>=@Updatetime and EntGuid=@Orgguid;

	DECLARE @i INT = 0; --循环计数

	IF(@NUM > 0)
		BEGIN
			--循环查询库存记录并进行更新,如果之前库存已经存在则更新,否则插入
			WHILE ( @i < CEILING(@NUM/@per_page) )
				BEGIN
					Begin Tran;--开启事务
					DECLARE @do_insert_sql nvarchar(MAX) = ''; --最后执行的insert sql语句
					DECLARE @do_update_sql nvarchar(MAX) = ''; --最后执行的update sql语句
					DECLARE @fail_str VARCHAR(2000) = ''; --失败的语句
					DECLARE @start_index INT = @i*CAST(@per_page AS INT)+1; --开始的索引值
					DECLARE @end_index INT = CAST(@start_index as INT)+CAST(@per_page AS INT)-1; --结束的索引值
					DECLARE @Guid VARCHAR(50),@GOODSGUID VARCHAR(50),@SubCode VARCHAR(50),@ItemCode VARCHAR(50),@StockQuantity NUMERIC(18,4),@UnitPrice NUMERIC(18,6),@IS_EXISTS INT,@RECORD_NUM INT;
					--这里是获取需要的数据
					DECLARE STOCK_CURSOR CURSOR LOCAL SCROLL for (
							SELECT * FROM 
								(
									SELECT Guid,SubCode,ItemCode,ISNULL(StockQuantity, 0) StockQuantity,ISNULL(UnitPrice, 0) UnitPrice,sbs.GOODSGUID,
										case when ISNULL(sbs.GOODSGUID, 0)=0 then 0 else 1 end as IS_EXISTS,ROW_NUMBER() over (order by Billtime DESC) as NUM 
									FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=192.168.100.2;UID=sa;PWD=testsql',
									WeiShopDB_empty.dbo.i_EntSubInfoNew) AS a 
									LEFT JOIN 
									(
											SELECT GOODSGUID,BRANCHGUID,ORGGUID FROM SHOP_BRANCH_STOCKCOUNT sbs1 GROUP BY GOODSGUID,BRANCHGUID,ORGGUID
									) sbs ON sbs.GOODSGUID=a.ItemCode AND sbs.ORGGUID=a.EntGuid AND sbs.BRANCHGUID=a.SubCode
									WHERE Billtime>=@Updatetime and EntGuid=@Orgguid
								) t WHERE NUM BETWEEN @start_index AND @end_index
					)
					--打开游标
					OPEN STOCK_CURSOR;
					--开始循环游标变量
					FETCH next from STOCK_CURSOR into @Guid,@SubCode,@ItemCode,@StockQuantity,@UnitPrice,@GOODSGUID,@IS_EXISTS,@RECORD_NUM;
					WHILE (@@FETCH_STATUS =0)
						BEGIN
							IF (@IS_EXISTS = 1)
								BEGIN
								--更新
									SET @do_update_sql = @do_update_sql+'UPDATE SHOP_BRANCH_STOCKCOUNT SET STOCKCOUNT='+CAST(CAST(@StockQuantity as INT) AS VARCHAR(50))+',USERPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+',MARKETPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+'
									WHERE GOODSGUID='+@GOODSGUID+' AND BRANCHGUID='+@SubCode+';';

								END
							ELSE
								BEGIN
								--插入
									DECLARE @RECORD_GUID VARCHAR(50)=NEWID();
									IF ( ISNULL(@do_insert_sql, '') = '')
										BEGIN
											SET @do_insert_sql = @do_insert_sql+'SELECT '+@RECORD_GUID+','+@Orgguid+','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(50))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+','+@GOODSGUID+','+@SubCode;
										END
									ELSE
										BEGIN
											SET @do_insert_sql = @do_insert_sql+' UNION ALL SELECT '+@RECORD_GUID+','+@Orgguid+','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(50))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(50))+','+@GOODSGUID+','+@SubCode;
										END
								END
							--让游标指向下一条记录
							FETCH next from STOCK_CURSOR into @Guid,@SubCode,@ItemCode,@StockQuantity,@UnitPrice,@GOODSGUID,@IS_EXISTS,@RECORD_NUM;
						END

					SET @i = @i+1;
					/***************************执行一遍更新或者插入语句*********************************/
					IF(ISNULL(@do_update_sql, '') <> '')
						BEGIN
							exec sp_executesql @temp=@do_update_sql; --如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数*
							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = '更新门店库存记录失败:SQL='--+@do_update_sql;	
									--GOTO SQLERR1;
								END
						END
					IF(ISNULL(@do_insert_sql, '') <> '')
						BEGIN
							SET @do_insert_sql = 'INSERT INTO SHOP_BRANCH_STOCKCOUNT(GUID,ORGGUID,STOCKCOUNT,USERPRICE,MARKETPRICE,GOODSGUID,BRANCHGUID) '+@do_insert_sql;
							--如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数*
							exec sp_executesql @do_insert_sql; 
							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = '插入门店库存记录失败:SQL='--+@do_insert_sql;	
									--GOTO SQLERR1;
								END
						END
					Commit Tran; --提交事务
					--关闭游标
					CLOSE STOCK_CURSOR;
					--销毁游标
					DEALLOCATE STOCK_CURSOR;
				END
		END
	ELSE
		BEGIN
			SELECT @ReturnMsg = 'shujuweikong';
		END
	SET NOCOUNT OFF; 
	Set @ReturnMsg='操作成功';
	Set @ReturnValue='1';
	return;
	/*************************************异常处理*********************************************************************/
	SQLErr1:
	RollBack Transaction;
	Set @ReturnValue = '-1';
	Return;
END



方案3:优化了方案2的一些内容
CREATE PROCEDURE [dbo].[UPDATE_STOCK]
	(
		@Updatetime  varchar(50),
		@Orgguid  varchar(50),
		@ReturnMsg    varchar(1000) output,         -- 返回提示或错误信息
		@ReturnValue    varchar(50) output        -- 返回提示或错误信息
	)
AS
	declare @SERVER varchar(500);
	declare @UID varchar(200);
	declare @PWD varchar(200);
BEGIN
	SET NOCOUNT ON; --如果想只获取输出参数,而不被存储过程里面的exec影响则需加上该设置,关闭影响的行数才行
	declare @NUM NUMERIC(10,0); --记录个数
	declare @per_page NUMERIC(10,0) = 500; --每页记录个数,经测试每页500个时间最快,而且超过1000多个好像就会出问题
	DECLARE @temp VARCHAR(500);
	SET @SERVER='192.168.100.2';
	SET @UID='sa';
	SET @PWD='testsql';
	--远程查询库存信息
	--获取记录的总个数
	SELECT @NUM=COUNT(Guid) FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=192.168.100.2;UID=sa;PWD=testsql',
  WeiShopDB_empty.dbo.i_EntSubInfoNew) AS a WHERE Billtime>=@Updatetime and EntGuid=@Orgguid;

	DECLARE @i INT = 0; --循环计数
	DECLARE @now_time VARCHAR(25) = CONVERT(varchar(25), GETDATE(), 21); --当前时间

	IF(@NUM > 0)
		BEGIN
			--循环查询库存记录并进行更新,如果之前库存已经存在则更新,否则插入
			WHILE ( @i < CEILING(@NUM/@per_page) )
				BEGIN
					
					DECLARE @do_insert_sql nvarchar(MAX) = ''; --最后执行的insert sql语句
					DECLARE @do_update_sql nvarchar(MAX) = ''; --最后执行的update sql语句
					DECLARE @start_index INT = @i*CAST(@per_page AS INT)+1; --开始的索引值
					DECLARE @end_index INT = CAST(@start_index as INT)+CAST(@per_page AS INT)-1; --结束的索引值
					
					DECLARE @SubCode VARCHAR(50),@ItemCode VARCHAR(50),@StockQuantity NUMERIC(18,4),@UnitPrice NUMERIC(18,6),@IS_EXISTS INT,@RECORD_NUM INT;
				
					DECLARE STOCK_CURSOR CURSOR LOCAL SCROLL for (
							SELECT SubCode,ItemCode,StockQuantity,UnitPrice,IS_EXISTS,NUM FROM 
								(
									SELECT a.SubCode,a.ItemCode,ISNULL(StockQuantity, 0) StockQuantity,ISNULL(UnitPrice, 0) UnitPrice,
										case when ISNULL(sbs.GOODSGUID, 0)=0 then 0 else 1 end as IS_EXISTS,ROW_NUMBER() over (order by UnitPrice ASC) as NUM 
									FROM OPENROWSET('MSDASQL','DRIVER={SQL Server};SERVER=192.168.100.2;UID=sa;PWD=testsql',
									WeiShopDB_empty.dbo.i_EntSubInfoNew) AS a 
									LEFT JOIN 
									(
											SELECT GOODSGUID,BRANCHGUID,ORGGUID FROM SHOP_BRANCH_STOCKCOUNT sbs1 GROUP BY GOODSGUID,BRANCHGUID,ORGGUID
									) sbs ON sbs.GOODSGUID=a.ItemCode AND sbs.ORGGUID=a.EntGuid AND sbs.BRANCHGUID=a.SubCode
									WHERE Billtime>=@Updatetime and EntGuid=@Orgguid
								) t WHERE NUM BETWEEN @start_index AND @end_index
					)
					Begin Tran;--开启事务
					--打开游标
					OPEN STOCK_CURSOR;
					--开始循环游标变量
					FETCH next from STOCK_CURSOR into @SubCode,@ItemCode,@StockQuantity,@UnitPrice,@IS_EXISTS,@RECORD_NUM;
					WHILE (@@FETCH_STATUS =0)
						BEGIN
							IF (@IS_EXISTS = 1)
								BEGIN
								--更新
									SET @do_update_sql = @do_update_sql+'UPDATE SHOP_BRANCH_STOCKCOUNT SET STOCKCOUNT='+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+',USERPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+',MARKETPRICE='+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+',UPDATETIME='''+@now_time+''' 
									WHERE GOODSGUID='+@ItemCode+' AND BRANCHGUID='+@SubCode+';';

								END
							ELSE
								BEGIN
								--插入
									DECLARE @RECORD_GUID VARCHAR(50)=NEWID();
									IF ( ISNULL(@do_insert_sql, '') = '')
										BEGIN
											SET @do_insert_sql = @do_insert_sql+'SELECT '''+@RECORD_GUID+''','''+@Orgguid+''','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','''+@ItemCode+''','''+@SubCode+''','''+@now_time+'''';
										END
									ELSE
										BEGIN
											SET @do_insert_sql = @do_insert_sql+' UNION ALL SELECT '''+@RECORD_GUID+''','''+@Orgguid+''','+CAST(CAST(@StockQuantity as INT) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','+CAST(CAST(@UnitPrice as NUMERIC(18,2)) AS VARCHAR(20))+','''+@ItemCode+''','''+@SubCode+''','''+@now_time+'''';
										END
								END
							--让游标指向下一条记录
							FETCH next from STOCK_CURSOR into @SubCode,@ItemCode,@StockQuantity,@UnitPrice,@IS_EXISTS,@RECORD_NUM;
						END

					SET @i = @i+1;
					/***************************执行一遍更新或者插入语句*********************************/
					IF(ISNULL(@do_update_sql, '') <> '')
						BEGIN
							exec sp_executesql @do_update_sql; --如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数

							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = 'update record error';	
									GOTO SQLERR1;
								END
						END

					IF(ISNULL(@do_insert_sql, '') <> '')
						BEGIN
							SET @do_insert_sql = 'INSERT INTO SHOP_BRANCH_STOCKCOUNT(GUID,ORGGUID,STOCKCOUNT,USERPRICE,MARKETPRICE,GOODSGUID,BRANCHGUID,UPDATETIME) '+@do_insert_sql;

							--如果不关闭NOCOUNT这里会影响输出,因为他会返回影响的行数
							exec sp_executesql @do_insert_sql; 
							IF @@ROWCOUNT = 0 OR @@ERROR<>0
								BEGIN
									SELECT @ReturnMsg = 'insert record error';	
									GOTO SQLERR1;
								END
						END

					Commit Tran; --提交事务
					--关闭游标
					CLOSE STOCK_CURSOR;
					--销毁游标
					DEALLOCATE STOCK_CURSOR;

				END
			Set @ReturnMsg='success';
		END
	ELSE
		BEGIN
			Set @ReturnMsg = 'data empty';
		END
	SET NOCOUNT OFF; 
	Set @ReturnValue='1';
	return;
	/*************************************异常处理*********************************************************************/
	SQLErr1:
	RollBack Transaction;
	Set @ReturnValue = '-1';
	Return;
END

下面是调用该存储过程的php代码:
//首先需要连接到远程连接的数据库开启Ad Hoc Distributed Queries参数,我下面是其中的部分主要代码,框架用的ci框架

public function testCunchu($updatetime){
	$config = array(); //数据库配置选项
	$db1 = $this->load->database($config,true);

	//开启分布式查询参数
	$on_advanced = "exec sp_configure 'show advanced options',1";
	$on_ad = "exec sp_configure 'Ad Hoc Distributed',1";
	$off_ad = "exec sp_configure 'Ad Hoc Distributed',0";
	$on_advanced = "exec sp_configure 'show advanced options',0";
	$reconf = 'reconfigure';

	$db1->query($on_advanced);
	$db1->query($reconf);
	$db1->query($on_ad);
	$db1->query($reconf);

	//调用存储过程
	$r = $this->doStoredProcedure($updatetime);
	//关闭分布式查询参数
	$db1->query($off_ad);
	$db1->query($reconf);
	$db1->query($off_advanced);
	$db1->query($reconf);
}

//调用存储过程*
public function doStoredProcedure($Updatetime){
	$Orgguid = $_SESSION['orgguid'] //组织机构号
	$this->load->mode('f_goods_model');
	$this->f_goods_model->init_db();
	$return = array();
	$serverName = $this->db->hostname;//服务器名称
	$connectionInfo = array(
			'UID' => $this->db->username, //用户名
			'PWD' => $this->db->password, //密码
			'Database' => $this->db->database //数据库名
		);
	$conn = sqlsrv_connect($serverName,$connectionInfo);

	if($conn == false)
	{
		$return = array(
				'status' => 0,
				'msg' =>'无法连接'
			);
		return $return;
	}

	//调用存储过程
	$tsql_callSP = "
			begin
				declare @Updatetime varchar(50) = '".$Updatetime."';
				declare @Orgguid varchar(50)='".$Orgguid."';
				declare @ReturnMsg varchar(500);
				declare @ReturnValue varchar(500);

				exec dbo.UPDATE_STOCK @Updatetime,@Orgguid,@ReturnMsg output,@ReturnValue output;
			end
		";
	$stmt3 = sqlsrv_query($conn,$tsql_callSP);

	if($stmt3 === false){
		$return = array(
				'status' => 0,
				'msg' => '调用存储过程错误'
			);
		return $return;
	}

	while($row = sqlsrv_fetch_array($stmt3,SQLSRV_FETCH_ASSOC)){
		$result = $row;
	}

	if(!empty($result))
	{
		$return = array(
				'status' => 1,
				'msg' => $ReturnMsg
			);
	}else{
		$return = array(
				'status' => 0,
				'msg' => '出现错误'
			);
	}
	//关闭释放连接
	sqlsrv_free_stmt($stmt3);
	sqlsrv_close($conn);
	return $return;
}


你可能感兴趣的:(php,数据库)