以下是我的存储过程示例,有人可以帮我解决以下错误:
'Msg 22027, Level 15, State 1, Line 0
Usage: EXECUTE xp_fileexist <filename> [, <file_exists INT> OUTPUT]
Msg 245, Level 16, State 1, Procedure TextImpoted01, Line 39
Conversion failed when converting the varchar value 'No' to data type int.'
USE [test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[TextImpoted01]
AS
SET NOCOUNT ON;
DECLARE @SQL NVARCHAR(100)
SELECT TOP (1) @SQL = total FROM DealCounter
DECLARE @path NVARCHAR(max)
SET @path = 'c:\temp\TextImpoted'+ @SQL + '.txt'
DECLARE @UpdateSQL NVARCHAR(100)
SET @UpdateSQL = N'UPDATE DealCounter SET total = total + 1'
DECLARE @LOOP NVARCHAR(100)
SELECT TOP (1) @LOOP = total FROM DealCounter
DECLARE @bulk_cmd NVARCHAR(max);
DECLARE @isExists INT
WHILE @SQL <= @LOOP + 1000
BEGIN
SET @path = 'c:\temp\TextImpoted'+ @SQL + '.txt'
exec master.dbo.xp_fileexist @path ,isExists out
SELECT case @isExists
when 1 then 'Yes'
else 'No'
end as isExists
if @isExists = 'No'
BREAK
else
SET @bulk_cmd = 'BULK INSERT TextImpoted FROM '''+ @path + '''
WITH
(
FIELDTERMINATOR = ' + ''';''' + ', ROWTERMINATOR = ' + '''\n'''+ ')';
EXEC sp_executesql @bulk_cmd
EXECUTE sp_executesql @UpdateSQL;
set @SQL = @SQL + 1;
END
答案 0 :(得分:1)
你的问题实际上并不涉及循环。
您在@isExists
声明integer
:
DECLARE @isExists INT
在这里,您将其与varchar
值进行比较。
if @isExists = 'No'
这会产生您已经看过的错误。
还要注意这行代码:
exec master.dbo.xp_fileexist @path ,isExists out
肯定应该是@isExists
,而你却错过了@
答案 1 :(得分:1)
首先:exec master.dbo.xp_fileexist @path ,isExists out
的输出变量缺少@
符号,应为exec master.dbo.xp_fileexist @path , @isExists out
。
第二:错误来自于尝试将INT变量@isExists与case语句中的char值进行比较。
case
块是多余的,但可以替换为:
EXEC master.dbo.xp_fileexist @path ,@isExists OUT
IF (@isExists = 1)
BEGIN
(... your logic here ... )
END