0
ALTER PROCEDURE [dbo].[usp_RmsExecuteValidationRule]   
    @nRuleId INT  
AS 
BEGIN   
    -- Local variables   
    DECLARE @sqlstat AS NVARCHAR(MAX)   
    DECLARE @params AS NVARCHAR(MAX)   
    DECLARE @RULE_QUERY NVARCHAR(MAX)  
    DECLARE @FIPS varchar(5) 

    SET @sqlstat = N'SELECT @RULE_QUERY=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = @nRuleId'   
    --SET @sqlstat = N'SELECT ' + @RULE_QUERY + '=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = ' + @nRuleId 

    SET @params = N'@nRuleId INT, @RULE_QUERY NVARCHAR(MAX) OUTPUT'   

    EXEC sp_executesql @sqlstat, @params, @nRuleId = @nRuleId, @RULE_QUERY= @RULE_QUERY OUTPUT 

    -- Output 
    SELECT @RULE_QUERY    
END 

當我們Exec usp_RmsExecuteValidationRule 1,它返回@RULE_QUERY即:無法通過,並追加@Variable選定查詢

SELECT GDMID AS GDM_ID from @FIPSName+CADMIN1 WHERE INTPRIORITY IS NULL 

在這裏,我有意添加@ FIPSName +作爲前綴,因爲我有另一個表LCountry與一個FIPS柱:

FIPS 
---- 
GM 
FR 
UK 
AN 
BE 
BU 
BL 
BR 
BH 
CA 
AU 
VT 
CI 
CH 
CO 
CS 
EZ 
AS 
DA 
EC 
SP 
AR 
BD 
BB 
BF 
AV 
AA 
AC 
DO 
CU 
CJ 
DR 
GP 
GJ 
HA 
JM 
ST 
RQ 
SC 
MB 
MH 
XN 
XM 
XJ 
VC 
VI 
TK 
TD 
TB 
RN 
XL 
XK 
VQ 
GR 
GT 
GQ 
HK 
HO 
HU 
ID 
EI 
IS 
GZ 
XC 
WE 
IN 
IT 
SM 
JA 
LS 
LU 
MX 
NU 
NL 
NO 
NZ 
PM 
PE 
RP 
PL 
PO 
RO 
SW 
SI 
LO 
ES 
TU 
TW 
SZ 
VE 
SN 
SB 
PF 
PG 
PC 
NE 
NF 
MQ 
US 
SX 
TE 
TT 
WQ 
BQ 
DQ 
BS 
CK 
CQ 
CR 
AY 
AT 
BV 
LQ 
KQ 
JQ 
JU 
KT 
IO 
IP 

使用循環我能夠獲取使用Lcountry的對象ID數據。

DECLARE @LoopCounter INT , @MaxBcountryObjId INT, 
     @FIPSName NVARCHAR(100) 
SELECT @LoopCounter = min(OBJECTID) , @MaxBcountryObjId = max(OBJECTID) 
FROM dbo.LCOUNTRY 

WHILE(@LoopCounter IS NOT NULL 
     AND @LoopCounter <= @MaxBcountryObjId) 
BEGIN 
    SELECT @FIPSName = FIPS 
    FROM dbo.LCOUNTRY WHERE OBJECTID = @LoopCounter 

    PRINT @FIPSName 
    SET @LoopCounter = @LoopCounter + 1 

此查詢返回@FIPSName。

問題

我想這個返回值@FIPName應該Rule_Query返回值追加。 並且也可以執行相同的值。

例如:

SELECT GDMID AS GDM_ID from @FIPSName+CADMIN1 WHERE INTPRIORITY IS NULL 
Changes Into 

SELECT GDMID AS GDM_ID from GM+CADMIN1 WHERE INTPRIORITY IS NULL 
Changes Into 

GDMID 
------   (The above query must be executed and Return GDMID.) 
1198 
+0

@戈登善意地建議解決方案..謝謝。 – Chetan

+0

@vkp善意地建議解決方案..謝謝 – Chetan

+0

@ rory.ap善意地建議解決方案..謝謝 – Chetan

回答

0

我的理解要執行

SELECT @RULE_QUERY=RULE_QUERY from GdmValidationRuleMaster where RULE_ID = @nRuleId 

上面的查詢會給你的結果。

我認爲你不需要使用sp_executesql。