I'm trying to use a view to create an ADO.NET entity using a view. However, that view does not have a single column that is NOT NULL.
One thing that occurred to me was to create a NOT NULL column on the view to use as a 'primary key' for the view. This worked, but the field is still reported as NULL.
Is there a way to force or trick SQL Server to report that view column as NOT NULL?
Imagine the view as something like:
CREATE VIEW vwSample WITH SCHEMABINDING
AS
SELECT ID = convert(uniqueidentifier, /* some computed value */)
,Field1
,Field2
,Field3
FROM tbSample
Note: Before saying that I can edit the entity XML to do such thing, I'm asking this because I have a VERY large number of entities to be created this way.
Go to the Object Explorer, Tables View, Right click on the table you want to change, Select Alter Table option, Select Columns option in the left panel, Then check the not null checkboxes in the right, then click ok.
You can add a not null column at the time of table creation or you can use it for an existing table. In the above table, we have declared Id as int type that does not take NULL value. If you insert NULL value, you will get an error. Here is the query to add a not null column in an existing table using alter command.
Firstly, to answer your question:
You don't want to use newid()
to determine your id field, since this will be recalculated every time you use the view. Really, data integrity is the biggest issue.
select
row_number() over (order by someStaticAndUniqueFieldLikeCreateDate) as ID,
Field1,
Field2,
Field3
from
tblA
order by
someStaticAndUniqueFieldLikeCreateDate
This only works if you're ordering on a field that will have a consistent ordering that will append new rows, such as a CreateDate
field. If you don't have this, that ID is subject to change. Now, if you only need these ID's at runtime, and there's nothing that permanently links to them, row_number
will be just peachy keen. If you have unreliable data, there's no way to have a reliable ID field unless you create an additional table and use triggers to populate it.
Secondly, be careful with that with schemabinding
. It's dangerous if used as a kludge. Remember that as soon as you create a view with schemabinding
, you cannot alter the schema of any underlying table at all. This means you can't make a varchar(50)
column a varchar(100)
without dropping and re-adding all views that have with schemabinding
enabled. Yes, you can index a view that's schemabound, but there are definitely tradeoffs that need to get taken into account before you go lock, stock, and barrel.
I don't know if it's worth the effort, but I think creating a function that returns Temporary Table with NOT NULL fields should work for the computed value. ex.
Create function fnSTestample()
returns @Test TABLE
(
tableId varchar(100) not null
)
WITH SCHEMABINDING
as
begin
insert @Test(tableID)
select 'some computed value' from dbo.someTable
return
end
and then you select that function in the view.
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With