如何解决这个常见的 SQL 问题

发布于 2024-10-24 04:01:00 字数 1600 浏览 6 评论 0原文

已经很久没有遇到过这个问题了,当我寻找解决方案时,我找不到一个。我认为这在 SQL 中称为重载。基本上,当我在此 SQL 中的任何参数有“”(空字符串)时,我不想在数据库中设置值...

注意:我想在 SQL 级别执行此操作,而不是在 C# 级别执行此操作因为那样就很马虎了。

string Sql = "IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) "
            + "INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) "
            + "UPDATE tbl_FileSystemReferences SET "
            + "UploadDir=@UploadDir, "
            + "ThumbnailDir=@ThumbnailDir, "
            + "ArchiveDir=@ArchiveDir, "
            + "RealDir=@RealDir, "
            + "FlashDir=@FlashDir, "
            + "AssociatedFilesDir=@AssociatedFilesDir, "
            + "EnableArchiving=@EnableArchiving, "
            + "AppWideDir=@AppWideDir, "
            + "FFmpegDir=@FFmpegDir, "
            + "InstallationDir=@InstallationDir ";

SqlCommand Command = new SqlCommand(Sql);
Command.Parameters.AddWithValue("@UploadDir", f.UploadDir);
Command.Parameters.AddWithValue("@ThumbnailDir", f.ThumbnailDir);
Command.Parameters.AddWithValue("@ArchiveDir", f.ArchiveDir);
Command.Parameters.AddWithValue("@RealDir", f.RealDir);
Command.Parameters.AddWithValue("@FlashDir", f.FlashDir);
Command.Parameters.AddWithValue("@AssociatedFilesDir", f.AssociatedFilesDir);
Command.Parameters.AddWithValue("@EnableArchiving", f.EnableArchiving);
Command.Parameters.AddWithValue("@AppWideDir", f.AppWideDir);
Command.Parameters.AddWithValue("@FFmpegDir", f.FFmpegDir);
Command.Parameters.AddWithValue("@InstallationDir", f.InstallationDir);

ExecuteNonQuery(Command);

我知道我曾经用存储过程来做到这一点,我只是不记得如何做到这一点(我认为这称为重载)....

干杯,

Haven't come across this in ages and when I searched for the solution I couldn't find one. I think its called overloading in SQL. Basically when I have "" (an empty string) for any parameter in this SQL I don't want to set a value in the database...

NOTE: I want to do it at a SQL level not do it at a C# level because its sloppy that way.

string Sql = "IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences) "
            + "INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null) "
            + "UPDATE tbl_FileSystemReferences SET "
            + "UploadDir=@UploadDir, "
            + "ThumbnailDir=@ThumbnailDir, "
            + "ArchiveDir=@ArchiveDir, "
            + "RealDir=@RealDir, "
            + "FlashDir=@FlashDir, "
            + "AssociatedFilesDir=@AssociatedFilesDir, "
            + "EnableArchiving=@EnableArchiving, "
            + "AppWideDir=@AppWideDir, "
            + "FFmpegDir=@FFmpegDir, "
            + "InstallationDir=@InstallationDir ";

SqlCommand Command = new SqlCommand(Sql);
Command.Parameters.AddWithValue("@UploadDir", f.UploadDir);
Command.Parameters.AddWithValue("@ThumbnailDir", f.ThumbnailDir);
Command.Parameters.AddWithValue("@ArchiveDir", f.ArchiveDir);
Command.Parameters.AddWithValue("@RealDir", f.RealDir);
Command.Parameters.AddWithValue("@FlashDir", f.FlashDir);
Command.Parameters.AddWithValue("@AssociatedFilesDir", f.AssociatedFilesDir);
Command.Parameters.AddWithValue("@EnableArchiving", f.EnableArchiving);
Command.Parameters.AddWithValue("@AppWideDir", f.AppWideDir);
Command.Parameters.AddWithValue("@FFmpegDir", f.FFmpegDir);
Command.Parameters.AddWithValue("@InstallationDir", f.InstallationDir);

ExecuteNonQuery(Command);

I know there is a way I used to do this with stored procedure I just cant remember how (I think it's called overloading)....

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

稀香 2024-10-31 04:01:00

您可以创建一个存储过程而不是将命令作为文本传递吗?

这样,您可以将“UploadDir=@UploadDir”等每一行分解为自己的变量,并且仅在它不为空或不为空字符串时将其添加到命令中

Can you create a stored procedure rather than passing the command as text?

That way you can break each of the lines like "UploadDir=@UploadDir," into its own variable and only add it to the command if it is not null or not empty string

梦魇绽荼蘼 2024-10-31 04:01:00

一种方法是在存储过程中,您将在其中接收所有这些参数,然后在查询之前:

  • 您允许传递 null
  • 如果每个参数为空,则将它们转换为 null,如下所示:

    select @UploadDir = null where @UploadDir = ''

您将为所有参数执行此操作,然后更新查询:

IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences)
INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null)
UPDATE tbl_FileSystemReferences SET
UploadDir=coalesce(@UploadDir, UploadDir),
ThumbnailDir=coalesce(@ThumbnailDir, ThumbnailDir),
ArchiveDir=coalesce(@ArchiveDir, ArchiveDir),
RealDir=coalesce(@RealDir, RealDir),
FlashDir=coalesce(@FlashDir, FlashDir),
AssociatedFilesDir=coalesce(@AssociatedFilesDir, AssociatedFilesDir),
EnableArchiving=coalesce(@EnableArchiving, EnableArchiving),
AppWideDir=coalesce(@AppWideDir, AppWideDir),
FFmpegDir=coalesce(@FFmpegDir, FFmpegDir),
InstallationDir=coalesce(@InstallationDir, InstallationDir)

one way would be on a stored procedure, where you would receive all those parameters, then before the query either:

  • you allow to pass null
  • you convert each parameter to null if they are empty as:

    select @UploadDir = null where @UploadDir = ''

you would do that for all your parameters, then on update query:

IF NOT EXISTS (SELECT * FROM tbl_FileSystemReferences)
INSERT INTO tbl_FileSystemReferences (UploadDir) VALUES (null)
UPDATE tbl_FileSystemReferences SET
UploadDir=coalesce(@UploadDir, UploadDir),
ThumbnailDir=coalesce(@ThumbnailDir, ThumbnailDir),
ArchiveDir=coalesce(@ArchiveDir, ArchiveDir),
RealDir=coalesce(@RealDir, RealDir),
FlashDir=coalesce(@FlashDir, FlashDir),
AssociatedFilesDir=coalesce(@AssociatedFilesDir, AssociatedFilesDir),
EnableArchiving=coalesce(@EnableArchiving, EnableArchiving),
AppWideDir=coalesce(@AppWideDir, AppWideDir),
FFmpegDir=coalesce(@FFmpegDir, FFmpegDir),
InstallationDir=coalesce(@InstallationDir, InstallationDir)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文