Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文件中循环SQL命令_Sql_Sql Server - Fatal编程技术网

在文件中循环SQL命令

在文件中循环SQL命令,sql,sql-server,Sql,Sql Server,我有一个SQL文件,看起来像这样(很明显,真正的东西有点长,实际上是做一些事情:) 我有一个“强制”和“鱼”值的文件 Mandatory,Fish 1,3 0,4 1,4 1,3 1,7 我需要编写一个程序,生成一个SQL文件(或多个文件),以便DBO针对数据库运行。但我不太确定如何解决这个问题 干杯您通常应该更喜欢基于集合的解决方案。我不知道完整的解决方案会是什么样子,但从一开始您就给出了: declare @Values table (

我有一个SQL文件,看起来像这样(很明显,真正的东西有点长,实际上是做一些事情:)

我有一个“强制”和“鱼”值的文件

  Mandatory,Fish
     1,3
     0,4
     1,4
     1,3
     1,7
我需要编写一个程序,生成一个SQL文件(或多个文件),以便DBO针对数据库运行。但我不太确定如何解决这个问题


干杯

您通常应该更喜欢基于集合的解决方案。我不知道完整的解决方案会是什么样子,但从一开始您就给出了:

declare @Values table (Mandatory int,Fish int)
insert into @Values(Mandatory,Fish) values
(1,3),
(0,4),
(1,4),
(1,3),
(1,7),

;with Prices as (
    select
        Mandatory,
        Fish,
        CASE
            WHEN Mandatory = 0 THEN f.PriceID
            ELSE 55 /* Calculation for Mandatory = 1? */
        END as InitialPriceID
    from
        @Values v
            left join /* Or inner join? */
        Fishes f
            on
                v.Fish = f.Fish
) select * from Prices

您的目标应该是一次性计算所有结果,而不是尝试“循环”每次计算。SQL通过这种方式工作得更好。

您通常应该更喜欢基于集合的解决方案。我不知道完整的解决方案会是什么样子,但从一开始您就给出了:

declare @Values table (Mandatory int,Fish int)
insert into @Values(Mandatory,Fish) values
(1,3),
(0,4),
(1,4),
(1,3),
(1,7),

;with Prices as (
    select
        Mandatory,
        Fish,
        CASE
            WHEN Mandatory = 0 THEN f.PriceID
            ELSE 55 /* Calculation for Mandatory = 1? */
        END as InitialPriceID
    from
        @Values v
            left join /* Or inner join? */
        Fishes f
            on
                v.Fish = f.Fish
) select * from Prices

您的目标应该是一次性计算所有结果,而不是尝试“循环”每次计算。SQL以这种方式工作得更好。

冒着在C#或类似语言中过度简化事情的风险,您可以使用字符串处理方法:

class Program
{
    static void Main(string[] args)
    {
        var sb = new StringBuilder();

        foreach(var line in File.ReadLines(@"c:\myfile.csv"))
        {
            string[] values = line.Split(',');

            int mandatory = Int32.Parse(values[0]);
            int fish = Int32.Parse(values[1]);

            sb.AppendLine(new Foo(mandatory, fish).ToString());
        }

        File.WriteAllText("@c:\myfile.sql", sb.ToString());
    }

    private sealed class Foo
    {
        public Foo(int mandatory, int fish)
        {
            this.Mandatory = mandatory;
            this.Fish = fish;
        }

        public int Mandatory { get; private set; }
        public int Fish { get; set; }

        public override string ToString()
        {
            return String.Format(@"DECLARE @Mandatory int = {0}
DECLARE @Fish int = {1}

DECLARE @InitialPriceID int
if @Mandatory= 
begin
select @InitialPriceID = priceID from Fishes where FishID = @Fish
end
", this.Mandatory, this.Fish);
        }
    }
}

冒着过度简化C#或类似语言的风险,您可以使用字符串处理方法:

class Program
{
    static void Main(string[] args)
    {
        var sb = new StringBuilder();

        foreach(var line in File.ReadLines(@"c:\myfile.csv"))
        {
            string[] values = line.Split(',');

            int mandatory = Int32.Parse(values[0]);
            int fish = Int32.Parse(values[1]);

            sb.AppendLine(new Foo(mandatory, fish).ToString());
        }

        File.WriteAllText("@c:\myfile.sql", sb.ToString());
    }

    private sealed class Foo
    {
        public Foo(int mandatory, int fish)
        {
            this.Mandatory = mandatory;
            this.Fish = fish;
        }

        public int Mandatory { get; private set; }
        public int Fish { get; set; }

        public override string ToString()
        {
            return String.Format(@"DECLARE @Mandatory int = {0}
DECLARE @Fish int = {1}

DECLARE @InitialPriceID int
if @Mandatory= 
begin
select @InitialPriceID = priceID from Fishes where FishID = @Fish
end
", this.Mandatory, this.Fish);
        }
    }
}

有很多文章介绍如何通过t-sql读取文本文件,检查是否可以将输入文件的格式更改为xml,然后可以检查

有很多文章介绍如何通过t-sql读取文本文件,检查是否可以将输入文件的格式更改为xml,然后您可以选中

您可以使用SQL Server的导入向导导入文件,然后将值编写到以后需要的任何位置。正如Bridge所说,或者您可以使用脚本语言(Bash/PHP/Ruby/Python或您熟悉的任何语言)读取列表,生成文件,运行文件并处理它。您可以使用SQL Server的导入向导导入文件,然后将值编写到以后需要的任何位置。正如Bridge所说,或者您也可以使用脚本语言(Bash/PHP/Ruby/Python或您熟悉的任何语言)来读取列表、生成文件、运行文件并处理它。