通过存储过程从数据库中检索值

时间:2019-03-13 10:52:09

标签: sql-server stored-procedures

我有一个带有表'crigsStaffActivity'的数据库。我设法检索值并显示它。但是结果并没有达到我的预期。

用于创建具有值的表的脚本

CREATE TABLE [dbo].[crigsStaffActivity]
(
    [projectRef] [varchar](200) NOT NULL,
    [activityLine] [int] NOT NULL,
    [cost] [real] NOT NULL,
    [staffCost] [varchar](500) NOT NULL
) ON [PRIMARY]
GO

INSERT [dbo].[crigsStaffActivity] ([projectRef], [activityLine], [cost], [staffCost]) 
VALUES (N'MRC/CRIGS/330', 1, 1, N'ProjectLeader'),
       (N'MRC/CRIGS/330', 2, 2, N'Research Collaborators'),
       (N'MRC/CRIGS/330', 1, 1, N'Project Collaborators'),
       (N'MRC/CRIGS/330', 2, 2, N'Project Collaborators'),
       (N'MRC/CRIGS/330', 2, 3, N'Research/Project Assistant');

ALTER TABLE [dbo].[crigsStaffActivity] 
    ADD CONSTRAINT [DF__crigsStaff__cost__5535A963] DEFAULT ((0)) FOR [cost]
GO

表的屏幕截图:

enter image description here

我的存储过程:

ALTER PROCEDURE [dbo].[getCrigsStaffActivity] 
    @ProjectRef AS VARCHAR(1000),
    @staffCost AS varchar(1000)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @val AS VARCHAR(2000);
    SET @val='';

    DECLARE @temp AS VARCHAR(2000);
    DECLARE @activityLine AS VARCHAR(2000);
    DECLARE @cost AS VARCHAR(2000);
    DECLARE @ref as CURSOR;   

    SET @ref = CURSOR FOR
        SELECT activityLine, cost
        FROM crigsStaffActivity
        WHERE projectRef = @ProjectRef
          AND staffCost = @staffCost

    OPEN @ref;

    FETCH NEXT FROM @ref INTO @activityLine, @cost;

    WHILE @@FETCH_STATUS = 0
    BEGIN
        FETCH NEXT FROM @ref INTO @activityLine, @cost;
        SET @val = CONCAT(@activityLine,',',@cost);
    END

    CLOSE @ref;
    DEALLOCATE @ref;

    SELECT @val AS ref;
END

执行存储过程时,会得到以下结果:

ref
----
2,2

问题是,对于项目合作者,我有两个重复的值,因此我需要得到如下结果:

ref 
-----
1,1
2,2

我的问题是如何获得上述结果。

此存储过程的更好结果可能是:

ref
-----------------------------
1,1,ProjectLeader
2,2,Research collaborators
1,1,Project collaborators
2,2,Project collaborators
2,3,Research/Project Assistant

2 个答案:

答案 0 :(得分:1)

如果我理解正确的话。您不需要使用光标。

ALTER PROCEDURE [dbo].[getCrigsStaffActivity] 
    @ProjectRef AS VARCHAR(1000),
    @staffCost AS varchar(1000)
AS
BEGIN
    SET NOCOUNT ON;

    SELECT CONCAT(activityLine,',', cost,',', staffCost) as ref
        FROM crigsStaffActivity
        WHERE projectRef = @ProjectRef
          AND staffCost = @staffCost


END

答案 1 :(得分:0)

请尝试执行此过程。

USE [***]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[getCrigsStaffActivity] 
@ProjectRef as VARCHAR(1000),
@staffCost as varchar(1000)
AS
BEGIN
SET NOCOUNT ON;
Declare @val as varchar(2000);
set @val='';
Declare @temp as Varchar(2000);
Declare @activityLine as Varchar(2000);
Declare @cost as Varchar(2000);
DECLARE @ref as CURSOR;

SET @ref = CURSOR FOR
SELECT DISTINCT activityLine, cost
FROM crigsStaffActivity
where projectRef = @ProjectRef
and staffCost = @staffCost

OPEN @ref;

FETCH NEXT FROM @ref INTO @activityLine, @cost;

WHILE @@FETCH_STATUS = 0
BEGIN
FETCH NEXT FROM @ref INTO @activityLine, @cost;
set @val = CONCAT(@activityLine,',',@cost);
END

CLOSE @ref;
DEALLOCATE @ref;

select @val as ref;
END