使用视图的过程不会返回正确的名称

时间:2015-10-07 17:14:40

标签: sql-server tsql stored-procedures

我试图创建一个使用与其他表和另一个View连接的过程,我这样做了:

ALTER PROCEDURE [dbo].[spSearchClicStatus]


@iClic int = NULL

AS

SELECT
        SC.dtDateCreated,V.USER_NAME, SC.iEDV, SC.sComments, sDescription, SC.iStatus
    FROM
        T_STATUS_CLIC SC INNER JOIN T_STATUS S
            ON SC.iSTATUS = S.iSTATUS,
        T_STATUS_CLIC SCC  INNER JOIN V_EMPLOYEES_MANAGER V
        on  SCC.iEDV = V.USER_EDV

    WHERE
        SC.iClic = @iClic
    ORDER BY 
        SC.dtDateCreated



GO

问题出在V.USER_NAME栏目中,我希望此专栏获取他的EDV人员的姓名(这是人员ID),但是如果只获取最后一个人的姓名,就像这个截图

enter image description here

它获取了最后一个人的名字,在第8行,但我希望每一行都有当前EDV的名称,我做错了什么?

现在我设法让它工作,我这样做了:

SELECT
        SC.dtDateCreated,(select USER_NAME from V_EMPLOYEES_MANAGER where USER_EDV = SC.iEDV) as User_name,
        SC.iEDV, SC.sComments, (select sDescription from T_STATUS where iStatus = SC.iStatus) as Status_Desc, SC.iStatus
    FROM
        T_STATUS_CLIC SC             
   WHERE        
         SC.iClic = 101025


    ORDER BY 
        SC.dtDateCreated

1 个答案:

答案 0 :(得分:1)

我删除了那个交叉加入......

Public Function teste(Optional ByVal consulta As String, Optional ByVal cbAR_valor As String, Optional ByVal cbAR_valor2 As String)

Dim DB As ADODB.Connection
Dim RST As ADODB.Recordset
Dim Query As ADODB.Command
Dim parametro1 As ADODB.Parameter
Dim parametro2 As ADODB.Parameter

On Error GoTo trataErro

DB_Dir = ThisWorkbook.Path & "\" & "DB2.accdb"

Set DB = New ADODB.Connection
DB.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & DB_Dir & ";Persist Security Info=False"

Set Query = New ADODB.Command
Set Query.ActiveConnection = DB
Query.CommandText = consulta
Query.CommandType = adCmdStoredProc

' Pego o valor do param. e adiciono no command
Set parametro1 = Query.CreateParameter("AR1", adChar, adParamInput, 255)
Query.Parameters.Append parametro1
parametro1.Value = cbAR_valor

' Pego o valor do param. e adiciono no command
Set parametro2 = Query.CreateParameter("AR2", adChar, adParamInput, 255)
Query.Parameters.Append parametro2
parametro2.Value = cbAR_valor2


Set RST = Query.Execute

a = RST.RecordCount

linha = 1
coluna = 1
ActiveWorkbook.Sheets("Plan2").Cells(CInt(linha), CInt(coluna)).CopyFromRecordset RST

DB.Close
Set DB = Nothing

Exit Function

trataErro:
    MsgBox ("Erro: " & Err.Description)


End Function