IF OBJECT_ID('PGetConnInfo') IS NOT NULL
BEGIN
    DROP PROC PGetConnInfo
END
GO

CREATE PROC PGetConnInfo
(
    @KeyWord VARCHAR(1000)
)
AS
BEGIN
    CREATE TABLE #Info
    (
        FMchId INT,
        FMchNo VARCHAR(200),
        FMchName VARCHAR(200),
        FServer VARCHAR(200),
        FDatabase VARCHAR(200),
        FWindowLogin BIT,
        FUser VARCHAR(200),
        FPassword  VARCHAR(200)
    )
    
    INSERT #Info(FMchId,FMchNo,FMchName)
    SELECT FClientId,FClientNo,FClientName
      FROM TClientInfo 
     WHERE FClientNo = @KeyWord
    
    IF @@ROWCOUNT = 0
    BEGIN
        INSERT #Info(FMchId,FMchNo,FMchName)
        SELECT FClientId,FClientNo,FClientName
          FROM TClientInfo 
         WHERE FClientNo LIKE '%' + @KeyWord + '%'
        IF @@ROWCOUNT = 0
        BEGIN
            INSERT #Info(FMchId,FMchNo,FMchName)
            SELECT FClientId,FClientNo,FClientName
              FROM TClientInfo 
             WHERE FClientName LIKE '%' + @KeyWord + '%'
        END
    END
    
    DECLARE @MchId INT
    DECLARE List CURSOR FOR 
     SELECT FMchId FROM #Info
    OPEN LIST 
    FETCH List INTO @MchId
    WHILE @@FETCH_STATUS = 0
    BEGIN
        DECLARE @Str VARCHAR(8000)
        DECLARE @Temp VARCHAR(8000)
        
        SELECT @Str = FConnectionString FROM TClientServerConfig WHERE FClientId = @MchId
        SELECT @Temp = SUBSTRING(@Str,CHARINDEX('Data Source=',@Str) + LEN('Data Source='),LEN(@Str))
        DECLARE @Server VARCHAR(1000)
        SELECT @Server = SUBSTRING(@Temp,1,CHARINDEX(';',@Temp)-1) 
        
        IF @Server = '(local)' OR @Server = '127.0.0.1'
        BEGIN
            SELECT @Server = FAddress FROM TServerInfo WHERE FServerId  =(SELECT FServerId FROM TClientInfo WHERE FClientId=@MchId)
        END
        
        UPDATE #Info SET FServer = @Server WHERE FMchId = @MchId
        
        SELECT @Temp = SUBSTRING(@Str,CHARINDEX('Initial Catalog=',@Str) + LEN('Initial Catalog='),LEN(@Str))
        UPDATE #Info SET FDatabase = SUBSTRING(@Temp,1,CHARINDEX(';',@Temp)-1) WHERE FMchId = @MchId
        
        SELECT @Temp = SUBSTRING(@Str,CHARINDEX('Integrated Security=',@Str) + LEN('Integrated Security='),LEN(@Str))
        UPDATE #Info SET FWindowLogin = SUBSTRING(@Temp,1,CHARINDEX(';',@Temp)-1) WHERE FMchId = @MchId
        
        IF EXISTS(SELECT 1 FROM #Info WHERE FMchId= @MchId AND FWindowLogin = 0)
        BEGIN
            SELECT @Temp = SUBSTRING(@Str,CHARINDEX('User Id=',@Str) + LEN('User Id='),LEN(@Str))
            UPDATE #Info SET FUser = SUBSTRING(@Temp,1,CHARINDEX(';',@Temp)-1) WHERE FMchId = @MchId
            
            SELECT @Temp = SUBSTRING(@Str,CHARINDEX('Password=',@Str) + LEN('Password='),LEN(@Str))
            UPDATE #Info SET FPassword = SUBSTRING(@Temp,1,CHARINDEX(';',@Temp)-1) WHERE FMchId = @MchId    
        END
        
        FETCH List INTO @MchId
    END

    SELECT * FROM #Info
END
GO

EXEC Main.dbo.PGetConnInfo 'glamg'

/*
SELECT C.FAddress,SUBSTRING(FConnectionString,CHARINDEX(FConnectionString,'Data Source='), FROM TClientServerConfig AS A,TClientInfo AS B,TServerInfo AS C
WHERE C.FServerId = B.FServerId
AND A.FClientId = B.FClientId
AND B.FClientNo = 'glamg'


Data Source=(local);Initial Catalog=GuanLing_AiMeiGe;Integrated Security=True;Pooling=True;Min Pool Size=1;Max Pool Size=20;Connect Timeout=60;Load Balance Timeout=30;Application Name=GLAMG

*/