使用 For /f 循环在批处理中运行 sqlcmd
我试图在批处理过程中使用 For 循环来多次执行 sqlcmd 。 For 循环应该解析一个 txt 文件,其中每一行都是数据库中一项研究的名称。 sqlcmd 查询应该提取适当的数据并使用研究名称填充 csv 文件。如果我在内部声明变量,sqlcmd 会自行生成一个 csv。批处理过程的工作原理是,它解析文本文件并为其中的每个研究生成一个 csv,但它不会填充文件。这是 sqlcmd 查询...
DECLARE @StudyName nvarchar(255)
SET @StudyName='$(StudyName)'
DECLARE @TblTVM TABLE(StudyId int, Animal nvarchar(100), DATE varchar(100), LL float, LWi float, LTVM float)
INSERT INTO @TblTVM (StudyId, Animal, DATE, LL,LWi, LTVM)
SELECT
S.StudyId,
A.PermanentId AS 'Animal',
(CAST(Datepart(Month, V.PerformDateTime) AS varchar(2))) + '/' + (CAST(Datepart(Day, V.PerformDateTime) AS varchar(2))) + '/' + (CAST(Datepart(Year, V.PerformDateTime) AS varchar(4))) AS 'DATE',
V.Length AS 'LL',
V.Width AS 'LWi',
V.Volume AS 'LTVM'
FROM TblStudy S
INNER JOIN tblAnimals A ON S.StudyId = A.StudyID
INNER JOIN TblTumorVolume V ON A.AnimalId = V.AnimalId
INNER JOIN TblImplants I ON V.ImplantId = I.ImplantID
WHERE
S.StudyNumber LIKE @StudyName AND
I.ImplSite LIKE N'Left';
SELECT
L.Animal,
L.DATE,
LL,
LWi,
LTVM
FROM @TblTVM L
ORDER BY
2,
1
这是 .bat 文件...
@echo off
set /p dir="What file?"
FOR /f %%X in (%dir%) do (
SET "StudyName=%%X"
SQLCMD -d StudyLog_Summary -i C:\Tools\2CSVQuery.sql -o C:\Tools\CSVs\%%X.csv -W -s","
)
任何有助于确定我的 csv 文件为空(即完成 0 行)的帮助都会很棒。
谢谢, 杰夫
I'm trying to use a For loop in a batch process to execute a sqlcmd multiple times. The For loop is supposed to parse through a txt file where each line is the name of a Study in a database. The sqlcmd query is supposed to pull the appropriate data and populate a csv file with the name of the study. The sqlcmd works by itself making a single csv if I declare the variable internally. The batch process works in that it parses through text file and makes a csv for each study in it but it doesn't populate the files. Here is the sqlcmd query...
DECLARE @StudyName nvarchar(255)
SET @StudyName='$(StudyName)'
DECLARE @TblTVM TABLE(StudyId int, Animal nvarchar(100), DATE varchar(100), LL float, LWi float, LTVM float)
INSERT INTO @TblTVM (StudyId, Animal, DATE, LL,LWi, LTVM)
SELECT
S.StudyId,
A.PermanentId AS 'Animal',
(CAST(Datepart(Month, V.PerformDateTime) AS varchar(2))) + '/' + (CAST(Datepart(Day, V.PerformDateTime) AS varchar(2))) + '/' + (CAST(Datepart(Year, V.PerformDateTime) AS varchar(4))) AS 'DATE',
V.Length AS 'LL',
V.Width AS 'LWi',
V.Volume AS 'LTVM'
FROM TblStudy S
INNER JOIN tblAnimals A ON S.StudyId = A.StudyID
INNER JOIN TblTumorVolume V ON A.AnimalId = V.AnimalId
INNER JOIN TblImplants I ON V.ImplantId = I.ImplantID
WHERE
S.StudyNumber LIKE @StudyName AND
I.ImplSite LIKE N'Left';
SELECT
L.Animal,
L.DATE,
LL,
LWi,
LTVM
FROM @TblTVM L
ORDER BY
2,
1
And here is the .bat file...
@echo off
set /p dir="What file?"
FOR /f %%X in (%dir%) do (
SET "StudyName=%%X"
SQLCMD -d StudyLog_Summary -i C:\Tools\2CSVQuery.sql -o C:\Tools\CSVs\%%X.csv -W -s","
)
Any help in figuring my the csv files are empty (ie. 0 lines completed) would be great.
Thanks,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论