且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

MS SQL中的默认约束问题

更新时间:2023-02-04 11:06:31

插入托运人(shippersName,发货日期)值('''','''')
insert into shippers (shippersName, shippeddate) values ('''','''')

 

这完全符合您的要求.您不是在使用默认值,而是在提供值,然后系统会尝试使用这些值.您为什么还要使用此SQL?我没有任何理由.

This is doing exactly what you told it to.  Instead of using the default values, YOU are providing values, which the system then tries to make sense of.  Why would you bother using this SQL ? I don''t see any reason for it whatsoever.

这些值从何而来?您如何构建SQL,以及如何使其安全?

Where do the values come from ? How are you building the SQL, and how do you make it secure ?


创建表时指定的默认值是在插入时不传递任何值时设置的.

因此,您不能使此程序化,以确保您将值传递为NULL,它将自动检测到它.

唯一可能的解决方法是保留默认值,并为该表定义一个触发器.在触发器内操纵值(例如CASE WHEN INSERTED.shippersName IS NULL THEN ''ashok'' ELSE INSERTED.shippersName END)

***避免这种情况.如果遇到这种情况,我将创建一个存储过程以将其(而不是通常插入的一批语句)插入数据库中,并在其中设置逻辑.

做适合自己的事情.
干杯.
Default value that you specify while creating the table is set when you dont pass any value while inserting.

So, you cant make this programmatic to ensure that you pass value as NULL and it will detect it automatically.

The only possible wayout from this, is leave the Default value, and define a Trigger for the table. Manipulate the value inside trigger (such as CASE WHEN INSERTED.shippersName IS NULL THEN ''ashok'' ELSE INSERTED.shippersName END)

But it is better to avoid this. If I was in your situation, I would have created a Stored Procedure to insert this( rather a batch of statement that commonly inserted) in the database and would have set the logic inside that.

Do whatever suits you.
Cheers.


这应该有效:
INSERT shippers DEFAULT VALUES

来源: http://articles.techrepublic.com.com/5100-10878_11-5794899.html