我正在将文件夹中的文件名转换为临时表。然后我使用游标对每个文件名执行某些操作。当我尝试用REPLACE
替换文件名中的.bak字符串时,它无法给出错误 - 必须声明标量变量“@FILENEME”。为什么?如何解决?
代码 -
CREATE TABLE ##FILES(
Name varchar(200),
depth numeric,
isFile numeric
)
--Assumption - the sp xp_dirtree has these columns
- subdirectory, depth, file
INSERT INTO ##FILES
EXEC xp_dirtree 'C:\Program Files\Microsoft SQL Server\
MSSQL10_50.SS2008\MSSQL\Backup\', 10, 1
DECLARE @FILENAME AS VARCHAR(200)
DECLARE @db AS VARCHAR(200)
DECLARE C CURSOR FOR
SELECT Name
FROM ##FILES
WHERE isFile = 1
OPEN C
FETCH NEXT FROM C INTO @FILENAME
WHILE @@FETCH_STATUS = 0
BEGIN
SET @db = REPLACE(@FILENEME, '.bak', '')
print @db
FETCH NEXT FROM C INTO @FILENAME
END
CLOSE C
DEALLOCATE C
答案 0 :(得分:3)
REPLACE(@FILENEME, '.bak', '')
中的简单拼写错误
使用此REPLACE(@FILENAME, '.bak', '')
CREATE TABLE ##FILES(
Name varchar(200),
depth numeric,
isFile numeric
)
--Assumption - the sp xp_dirtree has these columns
- subdirectory, depth, file
INSERT INTO ##FILES
EXEC xp_dirtree 'C:\Program Files\Microsoft SQL Server\
MSSQL10_50.SS2008\MSSQL\Backup\', 10, 1
DECLARE @FILENAME AS VARCHAR(200)
DECLARE @db AS VARCHAR(200)
DECLARE C CURSOR FOR
SELECT Name
FROM ##FILES
WHERE isFile = 1
OPEN C
FETCH NEXT FROM C INTO @FILENAME
WHILE @@FETCH_STATUS = 0
BEGIN
SET @db = REPLACE(@FILENAME, '.bak', '')
print @db
FETCH NEXT FROM C INTO @FILENAME
END
CLOSE C
DEALLOCATE C