我有点卡住了...我试图从查询中获取输出并根据单个条件将其分解为多个文件。我收到将 varchar 类型转换为 int 的错误,我不知道为什么。在 SQL Server 2008 中工作...
DECLARE @LOOP AS INT;
DECLARE @SQL AS VARCHAR(MAX);
DECLARE @BCP AS VARCHAR(MAX);
DECLARE @COUNTER AS INT;
DECLARE @FILENAME AS VARCHAR(MAX);
SET @COUNTER='1'
SELECT @LOOP = COUNT(DISTINCT LIST_ID) FROM DATA_TABLE
WHERE STATUS='2' AND LIST_ID IS NOT NULL ;
SET @SQL=(SELECT CUSTOMER_NO FROM CUSTOMER A, DATA_TABLE B
WHERE A.CUSTOMER_ID=B.CUSTOMER_ID AND A.STATUS='2' AND LIST_ID='+@LOOP+');
SET @FILENAME='QUERYOUT C:\Projects\FILE_"'+@LOOP+'.TXT'
WHILE @COUNTER<=@LOOP
BEGIN
SELECT
@BCP='BCP "'+@SQL+'+'+@FILENAME+''
SET @COUNTER=@COUNTER+1
END
GO
我得到的错误是:
Msg 245, Level 16, State 1, Line 10
Conversion failed when converting the varchar value '+@LOOP+' to data type int.
我正在尝试使用 LOOP 值让我知道每个文件的内容。例如,LOOP='1' 意味着文件包含与 LIST_ID='1' 关联的客户记录
对错误的想法?