插入“空” (字面意思)存储过程参数

发布于 2024-08-29 10:20:09 字数 1476 浏览 4 评论 0原文

我试图将单词“Null”(字面意思)插入到存储过程的参数中。由于某种原因,SqlServer 似乎认为我的意思是 NULL 而不是“Null”。如果我检查

IF @LastName IS NULL // Test: Do stuff

then 它会绕过它,因为参数不为空。

但是当我这样做时:

INSERT INTO Person (<params>) VALUES (<stuff here>, @LastName, <more stuff here>); // LastName is 'Null'

它会爆炸说 LastName 不接受空值。 我真的很讨厌有这个姓氏,但有人这样做了......而且它正在轰炸该应用程序。我们使用 SubSonic 2.0(是的,它相当旧,但升级很痛苦)作为我们的 DAL 并逐步执行它,我发现它确实正确创建了参数(据我所知)。

我尝试创建一个临时表来查看是否可以手动复制它,但它似乎工作得很好。这是我创建的示例:

DECLARE @myval VARCHAR(50)
SET @myval = 'Null'
CREATE TABLE #mytable( name VARCHAR(50))
INSERT INTO #mytable VALUES (@myval)
SELECT * FROM #mytable
DROP table #mytable

关于如何解决此问题有什么想法吗?

编辑:设置 LastName 的方法是这样的 -- myPerson.LastName = textBoxLastName.Text;

编辑:在使用 SqlServer Profiler 时,我发现它正在发送 null。有两件事很奇怪,我想知道为什么我的“IF @lastName IS NULL”没有触发......但现在我必须进一步查看 SubSonic,看看是否有什么东西在最后一刻发生了我没有意识到的变化。我将进一步调查此事并报告。

编辑:唯一的约束是默认值“”。

编辑:好的,我已经确认 SubSonic 正在尝试聪明地玩。

 if (param.ParameterValue == null || Utility.IsMatch(param.ParameterValue.ToString(), "null"))

使用 Profiler 的好主意,我完全忘记了。

最终编辑:值得注意的是,为了将来的参考,SubSonic 在数据提供程序的最后一刻替换了它(在我的例子中是 SqlDataProvider)。在调用 Execute Scalar 的 DataService.cs 中,参数尚未调整。当它为 DataProvider 运行 ExecuteScalar 时,它们会进行调整,DataProvider 是一个抽象类(因此 Postgre 和 Oracle 将各自拥有自己的代码位)。具体来说,SubSonic.SqlDataProvider.AddParams 就是我的问题。

I'm trying to insert the word "Null" (literally) in to a parameter for a stored procedure. For some reason SqlServer seems to think I mean NULL and not "Null". If I do a check for

IF @LastName IS NULL // Test: Do stuff

Then it bypasses that because the parameter isn't null.

But when I do:

INSERT INTO Person (<params>) VALUES (<stuff here>, @LastName, <more stuff here>); // LastName is 'Null'

It bombs out saying that LastName doesn't accept nulls.
I would seriously hate to have this last name, but someone does... and it's bombing the application. We're using SubSonic 2.0 (yeah, it's fairly old but upgrading is painful) as our DAL and stepping through it, I see it does create the parameters properly (for what I can tell).

I've tried creating a temp table to see if I could replicate it manually but it seems to work just fine. Here is the example I create:

DECLARE @myval VARCHAR(50)
SET @myval = 'Null'
CREATE TABLE #mytable( name VARCHAR(50))
INSERT INTO #mytable VALUES (@myval)
SELECT * FROM #mytable
DROP table #mytable

Any thoughts on how I can fix this?

edit: The method the LastName is set is this -- myPerson.LastName = textBoxLastName.Text;

edit: Upon using SqlServer Profiler, I see that it IS sending null. Two things that's weird, I wonder why my "IF @lastName IS NULL" isn't firing off... but now I have to look at SubSonic further to see if something is changing last minute I wasn't aware of. I'll investigate this further and report back.

edit: The only constraint is a default value of ''.

edit: Ok, I've confirmed it IS SubSonic trying to play smart.

 if (param.ParameterValue == null || Utility.IsMatch(param.ParameterValue.ToString(), "null"))

Good idea for using the Profiler, I entirely forgot about that.

final edit: It's worth noting, fur future reference, that SubSonic replaces this at the last minute in the data provider (in my case SqlDataProvider). In The DataService.cs where it calls Execute Scalar, the parameters have not been adjusted yet. They get adjusted when it runs the ExecuteScalar for the DataProvider, which is an abstract class (so Postgre and Oracle will each have their own bits of code). Specifically, the SubSonic.SqlDataProvider.AddParams is what bit me.

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

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

发布评论

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

评论(2

家住魔仙堡 2024-09-05 10:20:09

值得注意的是,为了将来的参考,SubSonic 在数据提供程序的最后一刻替换了它(在我的例子中是 SqlDataProvider)。在调用 Execute Scalar 的 DataService.cs 中,参数尚未调整。当它为 DataProvider 运行 ExecuteScalar 时,它们会进行调整,DataProvider 是一个抽象类(因此 Postgre 和 Oracle 将各自拥有自己的代码位)。具体来说,SubSonic.SqlDataProvider.AddParams 就是我的问题。

It's worth noting, fur future reference, that SubSonic replaces this at the last minute in the data provider (in my case SqlDataProvider). In The DataService.cs where it calls Execute Scalar, the parameters have not been adjusted yet. They get adjusted when it runs the ExecuteScalar for the DataProvider, which is an abstract class (so Postgre and Oracle will each have their own bits of code). Specifically, the SubSonic.SqlDataProvider.AddParams is what bit me.

毁我热情 2024-09-05 10:20:09

请尝试这个:

哎呀......我完全误解了这个问题。我认为数据库列中应该设置一些约束。请尝试直接在数据库中编写插入查询,看看是否会崩溃。

我尝试了这个,它按预期爆炸了:

DECLARE @myval VARCHAR(50)
SET @myval = 'null'
CREATE TABLE #mytable( name VARCHAR(50) NULL CHECK (name <> 'NULL'))
INSERT INTO #mytable VALUES (@myval)
SELECT * FROM #mytable WHERE name is not null
DROP table #mytable

HTH

Please try this:

Oops...I totally misunderstood the question. I think there should be some constraints set in the database column. Please try to write the insert query directly in DB and see if it bombs out.

I tried this and it bombs out as expected:

DECLARE @myval VARCHAR(50)
SET @myval = 'null'
CREATE TABLE #mytable( name VARCHAR(50) NULL CHECK (name <> 'NULL'))
INSERT INTO #mytable VALUES (@myval)
SELECT * FROM #mytable WHERE name is not null
DROP table #mytable

HTH

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文