如何解决这个常见的 SQL 问题
已经很久没有遇到过这个问题了,当我寻找解决方案时,我找不到一个。我认为这在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个存储过程而不是将命令作为文本传递吗?
这样,您可以将“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
一种方法是在存储过程中,您将在其中接收所有这些参数,然后在查询之前:
如果每个参数为空,则将它们转换为 null,如下所示:
select @UploadDir = null where @UploadDir = ''
您将为所有参数执行此操作,然后更新查询:
one way would be on a stored procedure, where you would receive all those parameters, then before the query either:
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: