具有临时表SQL的Codeigniter

时间:2016-09-23 07:12:46

标签: sql sql-server codeigniter stored-procedures

我正在尝试使用Côte d'Ivoire创建查询。我能够创建它,当我运行它时没有问题。这是My StoredProcedure

...
$upd = "UPDATE adverts SET ad_country = :countries WHERE ad_id = :id";
$upq = $pdo->prepare($upd);
$upq->bindParam(':countries', $country);
$upq->bindParam(':id', $id);
$upq->execute();
...

这是我如何运行它

Stored Procedure

这是resullt

enter image description here

确定。问题是当我试图在Codeigniter中调用我的SP时。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE SP_Penilaian
    @Nip varchar(50)
AS
BEGIN

    select * INTO #tMP from historyposition where nip = ''+@Nip+'' order by approveddate desc

declare @NewPosition varchar(50), @NewPositionLast varchar(50), @ApproveDate datetime, @ApproveDateLast datetime
select top 1 @NewPositionLast = NewPosition, @ApproveDateLast=ApprovedDate from #tmp
WHILE EXISTS (SELECT * FROM #tMP)
BEGIN
    select top 1 @NewPosition = NewPosition, @ApproveDate=ApprovedDate from #tmp
    if (@NewPosition = @NewPositionLast)
    begin 
        set @NewPositionLast = @NewPosition 
        set @ApproveDateLast = @ApproveDate
    end 
    else
    begin
        break
    end
    delete top(1) #tMP
END
select  @NewPositionLast 'LatesUpgradePosition' , @ApproveDateLast 'LatesUpgradeDate'
END
GO

然后我试图用这个

检查我的数据
sp_penilaian '1500060'

但我得到空数据。那么如何在CodeIgniter中使用临时表?或者,如果我不能使用临时表,我可以创建表并将其放入我的存储过程中吗?

抱歉我的英语不好。

2 个答案:

答案 0 :(得分:4)

SET NOCOUNT ON;

之后尝试添加BEGIN
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE SP_Penilaian
    @Nip varchar(50)
AS
BEGIN

SET NOCOUNT ON; /*<--Add here*/

    select * INTO #tMP from historyposition where nip = ''+@Nip+'' order by approveddate desc

declare @NewPosition varchar(50), @NewPositionLast varchar(50), @ApproveDate datetime, @ApproveDateLast datetime
select top 1 @NewPositionLast = NewPosition, @ApproveDateLast=ApprovedDate from #tmp
WHILE EXISTS (SELECT * FROM #tMP)
BEGIN
    select top 1 @NewPosition = NewPosition, @ApproveDate=ApprovedDate from #tmp
    if (@NewPosition = @NewPositionLast)
    begin 
        set @NewPositionLast = @NewPosition 
        set @ApproveDateLast = @ApproveDate
    end 
    else
    begin
        break
    end
    delete top(1) #tMP
END
select  @NewPositionLast 'LatesUpgradePosition' , @ApproveDateLast 'LatesUpgradeDate'
END
GO

答案 1 :(得分:0)

看起来问题不在于实际的存储过程,而在于你调用它的方式。试试:

$nip = '1500060';
$sp = "CALL SP_Penilaian(?)";

要将nip传递给存储过程,您可以使用CodeIgniter的数据库查询功能,如下所示:

$filt_outlet = $this->db->query($sp, array('nip'=>$nip));