0

私のストアドプロシージャは次のとおりです。

CREATE PROCEDURE [dbo].[USP_GET_MBR_DWN_DTL] (@MBR_ID VARCHAR(50), @LEG_POS CHAR)    
AS BEGIN    

WITH CTE(MBR_ID, MBR_LEG_POS, MBR_NM, MBR_JOIN_TT, LVL, MBR_UPR_ID, MBR_SPR_ID) AS            
(            
        SELECT
            CAST(MBR_ID AS VARCHAR(100)) as MBR_ID,
            MBR_LEG_POS, MBR_NM, MBR_JOIN_TT,
            CAST(MBR_UPR_ID AS VARCHAR(100)) as MBR_UPR_ID,
            CAST(MBR_SPR_ID AS VARCHAR(100)) as MBR_SPR_ID,
            LVL=0
        FROM MBR_MST
        WHERE MBR_UPR_ID=@MBR_ID and MBR_LEG_POS=@LEG_POS     

        UNION ALL            

        SELECT
           CAST(M.MBR_ID AS VARCHAR(100)) as MBR_ID,
           M.MBR_LEG_POS,M.MBR_NM,M.MBR_JOIN_TT,
           CAST(M.MBR_UPR_ID AS VARCHAR(100)) as MBR_UPR_ID,
           CAST(M.MBR_SPR_ID AS VARCHAR(100)) as MBR_SPR_ID,
           LVL+1
        FROM MBR_MST M
            JOIN CTE F ON CAST(M.MBR_UPR_ID AS VARCHAR(100))=CAST(F.MBR_ID AS VARCHAR(100))       
)    
SELECT * FROM CTE ORDER BY LVL, MBR_JOIN_TT OPTION (MAXRECURSION 0)    
END  

しかし、私が走るとき

USP_GET_MBR_DWN_DTL 'Flife1', 'A'

それはエラーを示しています

メッセージ 245、レベル 16、状態 1、手順 USP_GET_MBR_DWN_DTL、行 5
varchar 値 'Flife1' をデータ型 int に変換するときに変換に失敗しました。

4

3 に答える 3

1

次の修正を試してください。

--code before the union
FROM MBR_MST 
WHERE CAST(MBR_UPR_ID AS VARCHAR(100)) =@MBR_ID AND MBR_LEG_POS=@LEG_POS           
UNION ALL
--rest of the code
于 2013-08-19T08:53:49.137 に答える
0

UNION 内の 2 つのテーブルの違いを強調するための便利なクエリを次に示します。

SELECT 
    c1.ordinal_position, 
    c1.table_name, 
    c1.data_type, 
    c1.column_name, 
    c2.column_name,
    c2.data_type,
    c2.table_name
FROM INFORMATION_SCHEMA.COLUMNS c1
LEFT JOIN (
    SELECT ordinal_position, column_name, table_name,data_type
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = N'TABLE_ONE') c2 on c1.ordinal_position = c2.ordinal_position
where c1.TABLE_NAME = N'TABLE_TWO'
and (c1.column_name <> c2.column_name
    or c1.data_type <> c2.data_type)
order by c1.ordinal_position asc;
于 2016-03-05T00:57:57.187 に答える