使用 For /f 循环在批处理中运行 sqlcmd

发布于 2025-01-07 07:04:19 字数 1450 浏览 1 评论 0原文

我试图在批处理过程中使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文