具有单个字段的Microsoft SQL Server存储过程

时间:2013-12-13 13:21:19

标签: sql sql-server stored-procedures

经过三天密集的谷歌搜索,尝试,失败,讨价还价和生气。我已经接受了我的命运并决定问你。我会尽量在这里尽可能清楚。

我试图从数据库中选择一个字段(通过SP)这里有一些我尝试过的事情:

Subquerywise:

(
    SELECT dbo.T_MemoDetail.Info
    FROM dbo.T_MemoDetail
    INNER JOIN dbo.T_DossierDetail AS T_DossierDetail
            ON T_DossierDetail.MemoGrpId = dbo.T_MemoDetail.MemoGrpId
        INNER JOIN dbo.T_DossierMain AS T_DossierMain
            ON T_DossierDetail.DossierCode = T_DossierMain.DossierCode
    WHERE T_DossierDetail.MemoGrpId = dbo.T_MemoDetail.MemoGrpId AND
    T_DossierDetail.DossierCode = T_DossierMain.DossierCode AND
    T_DossierMain.OrdNr = 'BVR13-0710'
),

^返回2个字符串

SELECT 
    /*Selects a lot more, but i do not want to paste the whole SP here*/
    T_MemoDetail.Info **Note that this isn't a subquery**
    /*Selecting more data beneath here*/
FROM dbo.T_DossierMain
    INNER JOIN dbo.T_DossierDetail AS t10 
        ON t10.DossierCode = t1.DossierCode
    INNER JOIN dbo.T_MemoDetail AS T_MemoDetail
        ON T_MemoDetail.MemoGrpId = t10.MemoGrpId AND
        t10.DossierCode = t1.DossierCode

还尝试使用两种方式的临时表
CREATE TABLE #Temp ....... INSERT INTO SELECT

SELECT ...... INTO #Temp

只有这些该死的表之间的链接

T_DossierDetail.MemoGrpId = dbo.T_MemoDetail.MemoGrpId AND  
T_DossierDetail.DossierCode = T_DossierMain.DossierCode AND  
T_DossierMain.OrdNr BETWEEN @OrdFrom AND @OrdTo (filled with textboxes in FastReport)

注意T_DossierDetail中唯一不同的值是DetailCode和DetailSubCode的组合

如果有人可以帮我解决这个问题,我会非常感激。

编辑1.1

希望这会有所帮助......

T_DossierDetail的

(可用)值:

MemoGrpId,DossierCode,DetailCode,DetailSubCode  
5468, 015402,010,000  
5469, 015402,020,000  
T_DossierMain的

(可用)值:

OrdNr,T_DossierMain.DossierCode  
BVR13-0710, 015402  
T_DossierMain的

(可用)值:

T_MemoDetail.MemoGrpId, T_MemoDetail.Info  
5468, [CompanyName] Logo  
5469, [CompanyName] Logo  

所有值都以字符串形式返回,而不是int,double,float或任何STRINGS(由于某种原因),仅供参考:我只是一名实习生

The "Involves" tables

2 个答案:

答案 0 :(得分:0)

以下是未正确使用密钥的示例:

declare @table1 table (key1 int, key2 int, value1 int)
declare @table2 table (key1 int, key2 int, key3 int, value2 int)

insert into @table1 values (1,1,1)
insert into @table1 values (1,2,2)

insert into @table2 values (1,1,1,1)

select t2.value2 
from @table1 t1
inner join @table2 t2 on t1.key1 = t2.key1
where t2.key3 = 1

答案 1 :(得分:0)

您有2行,因为您有一个包含2个详细信息行的档案。这就是连接的工作方式:它从左表中获取所有行(=档案详细信息,2行),并将它们与右表(=主档案,1行)匹配。

您还添加了一个新条件,以过滤条件数据的方式只保留一行信息详细信息:

    SELECT
        MD.Info
    FROM dbo.T_MemoDetail MD
        INNER JOIN dbo.T_DossierDetail DD ON DD.MemoGrpId = MD.MemoGrpId
        INNER JOIN dbo.T_DossierMain DM ON DD.DossierCode = DM.DossierCode
    WHERE 
        DM.OrdNr = 'BVR13-0710'

P.S。你把两个JOIN条件加了两次=>没有必要......