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

简单语句就是出不来,贴出来请指点!急啊
declare   @varYF   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   @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+   substring( '+@varYF+ ',1,4)   + '  
  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+substring( '+@varYF+ ',1,4)   + '   B   WHERE   A.WLBH=B.WLBMDM
DROP   TABLE     #TB_TEMP   '  

print   @varSql  

结果显示:

(1   row   affected)
(1   row   affected)
(1   row   affected)
(1   row   affected)
(1   row   affected)
Server   Message:     Number     2747,   Severity     16
Server   'sjcs ',   Line   21:
Incorrect   position   specifier   in   format   string   of   PRINT   command.  



------解决方案--------------------
--try
declare @varYF 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 @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+ substring(@varYF,1,4) + '
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+substring(@varYF,1,4) + ' B WHERE A.WLBH=B.WLBMDM
DROP TABLE #TB_TEMP '

print @varSql

------解决方案--------------------
加号前后如果不是变量,那么就用引号!
------解决方案--------------------
兩個地方都要處理

declare @varYF 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 @varXMBH= '200083215 '
set @varZRZX = '21011324 '
set @varKMBH= '1002 '