简体   繁体   English

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

[英]Microsoft SQL Server Stored Procedure with a single field

After three days of intensive Googling, trying, failing, bargaining and getting angry. 经过三天的密集搜索,尝试,失败,讨价还价和生气。 I have accepted my fate and decided to ask you. 我已经接受了命运,决定问你。 I will try to be as clear as possible here. 我会在这里尽量清楚。 .

I am trying to select a single field from the database (via a SP) here are some of the things I have tried: 我试图从数据库中选择一个字段(通过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'
),

^Returns 2 Strings ^返回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

also tried using a Temporary table with both ways 还尝试通过两种方式使用临时表
CREATE TABLE #Temp ....... INSERT INTO SELECT
and
SELECT ...... INTO #Temp

ONLY links between the damn tables 该死的桌子之间只有联系

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)

NOTE The only value in the table T_DossierDetail that is Distinct is the combination of DetailCode and DetailSubCode 注意T_DossierDetail中唯一的唯一值是DetailCode和DetailSubCode的组合

If anyone can help me out with this one, I would be so greatfull 如果有人可以帮我解决这个问题,我会非常高兴

EDIT 1.1 编辑1.1

Hope this helps... 希望这可以帮助...

(Usable) values for T_DossierDetail: T_DossierDetail的(可用)值:

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

(Usable) values for T_DossierMain: T_DossierMain的(可用)值:

OrdNr,T_DossierMain.DossierCode  
BVR13-0710, 015402  

(Usable) values for T_DossierMain: T_DossierMain的(可用)值:

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

All values are returned as strings, not int, double, float or anything STRINGS ONLY (for some reason), FYI: I'm just an intern 所有值都以字符串形式返回,而不是int,double,float或仅包含STRINGS(由于某种原因)的任何内容,仅供参考:我只是一个实习生

“涉及”表

Here is an example of key not correctly used : 这是密钥使用不正确的示例:

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

You have 2 rows because you have a dossier with 2 details rows. 您有2行,因为您有一个包含2条明细行的档案。 That's the way a join works: it takes all the rows from left table (=dossier details, 2 rows) and match them with the right table (=main dossier, 1 row). 这就是联接的工作方式:它将左侧表格中的所有行(=档案详细信息,2行)与右侧表格中的所有行进行匹配(=主要档案,1行)。

You have too add a new condition, to filter condition your data in a way to keep only one row from dossier detail: 您还添加了一个新条件,以某种方式过滤条件数据,以使档案详细信息仅保留一行:

    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'

PS You put twice some JOIN conditions => it is not necessary... 附注:您将一些JOIN条件放了两次=>没必要...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM