无法在参数中获取变量值

时间:2013-09-09 11:36:37

标签: sql sql-server-2008 tsql

我没有在select语句参数中获得变量rsp2的值。

 DECLARE @RSP  varchar(50)
  DECLARE @RSP2 varchar(50)
 SET @RSP =  '(<DL>,<DM>)'
SELECT @RSP  
 SET @RSP2 = REPLACE(REPLACE(REPLACE(REPLACE(@RSP,'(',''),')',''),'<',''''),'>','''')
SELECT @RSP2 
    SELECT [F_YEAR] FROM tbl1 WHERE 
                 RSP  IN (@RSP2)

3 个答案:

答案 0 :(得分:1)

假设您希望将f_year的值分配给@rsp变量。

DECLARE @RSP  varchar(50)
      , @RSP2 varchar(50);

 SET @RSP = '(<DL>,<DM>)';
 SET @RSP2 = REPLACE(REPLACE(REPLACE(REPLACE(@RSP,'(',''),')',''),'<',''''),'>','''');

SET @RSP = (
  SELECT Max(f_year)
  FROM   tbl1
  WHERE  RSP  = @RSP2;
);

SELECT @RSP  As rsp
     , @RSP2 As rsp2;

答案 1 :(得分:0)

试试这个。

DECLARE @RSP  varchar(50)
DECLARE @RSP2 varchar(50)
SET @RSP =  '(<DL>,<DM>)'
SELECT @RSP  
SET @RSP2 = REPLACE(REPLACE(REPLACE(REPLACE(@RSP,'(',''),')',''),'<',''''),'>','''')
SELECT @RSP2 
EXEC('SELECT [F_YEAR] FROM tbl1 WHERE 
             RSP  IN ('+@RSP2+')')

希望这会对你有所帮助

由于

答案 2 :(得分:0)

DECLARE @RSP  varchar(50) = '(<DL>,<DM>)'
DECLARE @RSP2 varchar(50)
-- next line has been changed as well
SET @RSP2 = REPLACE(REPLACE(REPLACE(REPLACE(@RSP,'(',''),')',''),'<',''),'>','')
SELECT @RSP RSP, @RSP2 RSP2 

SELECT [F_YEAR] FROM tbl1 
WHERE RSP in (
SELECT t.c.value('.', 'VARCHAR(20)')
FROM (
SELECT x = CAST('<t>' + 
    REPLACE(@RSP2, ',', '</t><t>') + '</t>' AS XML)
) a
CROSS APPLY x.nodes('/t') t(c))