日期:2014-05-18  浏览次数:20399 次

简单问题又发一帖,用变量替换后不出结果,为啥?请赐教
declare   @varYF   VARCHAR(4)
declare   @varYEAR   VARCHAR(4)
declare   @varXMBH   VARCHAR(9)
declare   @varZRZX   varchar(200)
declare   @varKMBH   VARCHAR(200)
declare   @intBMJC   int
declare   @varSql   varchar(2000)
set   @varYF   = '200601 '
set   @varYEAR= '2006 '
set   @varXMBH= '200083215 '
set   @varZRZX   = '21011324 '
set   @varKMBH= '1002 '
set   @intBMJC=4
set   @varSql= 'SELECT   SUBSTRING(WLBMDM,1,4)   AS   WLBH,   SUM(QCJFYE)   AS   QCJFYE   INTO   #TB_TEMP  
FROM   XT_FZB_ '+@varXMBH+   @varYEAR+ '  
  WHERE   ZRZX   like   ' ' '+@varZRZX   + '% ' '   AND   KMBH   LIKE   ' ' '+@varKMBH   + '% ' '   AND   YF= ' ' '+@varYF   + ' ' '  
AND   WLBMDM   IS   NOT   NULL     GROUP   BY   SUBSTRING(WLBMDM,1,4)  
SELECT   A.WLBH,B.WLBMMC   FROM   #TB_TEMP   A,   XT_BMK_ '+@varXMBH+@varYEAR+ '   B   WHERE   A.WLBH=B.WLBMDM '  

/*   set   @varSql= 'SELECT   SUBSTRING(WLBMDM,1,4)   AS   WLBH,   SUM(QCJFYE)   AS   QCJFYE   INTO   #TB_TEMP  
FROM   XT_FZB_2000832152006  
  WHERE   ZRZX   like   ' '21011324% ' '   AND   KMBH   LIKE   ' '1002% ' '   AND   YF= ' '200601 ' '  
AND   WLBMDM   IS   NOT   NULL     GROUP   BY   SUBSTRING(WLBMDM,1,4)  
SELECT   A.WLBH,B.WLBMMC   FROM   #TB_TEMP   A,   XT_BMK_2000832152006   B   WHERE   A.WLBH=B.WLBMDM '     */

exec(@varSql)


------解决方案--------------------
检查一下这句话

SELECT SUBSTRING(WLBMDM,1,4) AS WLBH, SUM(QCJFYE) AS QCJFYE INTO #TB_TEMP
FROM XT_FZB_2000832152006
WHERE ZRZX like '21011324% ' AND KMBH LIKE '1002% ' AND YF= '2006 '
AND WLBMDM IS NOT NULL GROUP BY SUBSTRING(WLBMDM,1,4)
SELECT A.WLBH,B.WLBMMC FROM #TB_TEMP A, XT_BMK_2000832152006 B WHERE A.WLBH=B.WLBMDM
------解决方案--------------------
沒看到錯誤,錯誤提示還是那個?
------解决方案--------------------
单引号错误
------解决方案--------------------
你把它拆开执行不好吗?前面到
select 等号右边 as aa 应该得到一个字符串
SELECT SUBSTRING(WLBMDM,1,4) AS WLBH, SUM(QCJFYE) AS QCJFYE INTO #TB_TEMP
FROM XT_FZB_2000832152006
WHERE ZRZX like '21011324% ' AND
KMBH LIKE '1002% ' AND
YF= '2006 ' AND
WLBMDM IS NOT NULL
GROUP BY SUBSTRING(WLBMDM,1,4)
结束,exec(xxx)
然后
SELECT A.WLBH,B.WLBMMC F

现在你拼出的sql最后面是SELECT A.WLBH,B.WLBMMC F而不是group by就结束,是两个语句
------解决方案--------------------
用 print @varSql 将动态的Sql语句打印出来看看,和没有拼的语句比较一下有什么区别


like ' ' '+@varZRZX + '% ' ' AND KMBH LIKE ' ' '+@varKMBH + '% ' '

这句话的嫌疑最大,因为不知道 @varZRZX 变量中是连接以后到底是什么
------解决方案--------------------
这样试试
declare @varYF VARCHAR(4)