无法在所选查询上传递和附加@variable

时间:2016-09-27 07:09:25

标签: c# sql-server sql-server-2008 stored-procedures sql-server-2005

Exec usp_RmsExecuteValidationRule 1

当我们@RULE_QUERY时,它返回SELECT GDMID AS GDM_ID from @FIPSName+CADMIN1 WHERE INTPRIORITY IS NULL ,即:

LCountry

这里我故意添加了@ FIPSName + As前缀,因为我有另一个表FIPSFIPS ---- 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 列:

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 

使用循环我可以使用Lcountry的ObjectID获取数据。

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

此查询返回@FIPSName。

问题

我想要返回值@FIPName应该追加到Rule_Query返回值。 并且也可以执行相同的值。

例如:

{{1}}

1 个答案:

答案 0 :(得分:0)

据我了解你想要执行

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

以上查询将为您提供结果。

我认为您不需要使用sp_executesql。