Showing posts with label null. Show all posts
Showing posts with label null. Show all posts

Monday, March 26, 2012

IT WORKED!

Hello everyone,

i'm using an excel source where i get my excel rows using a query, I'd like to replace possible null values with some other data(a zero value or a empty string for example), that's because i'm performing a transformation into a sql server table wich doesn't accept null values for some columns.

Is there any function to convert a null value to another one? I used the sql server's CASE function, but it didn't work. Any suggestions?

thanks a lot.

Have you tried using Data Conversion transform...

you could use an expression like

IsNull(Column) ? ValueifNull : ValueifNotNull

See some examples here:

http://msdn2.microsoft.com/en-us/library/ms141184.aspx

|||Thanks a lot, the code and the article helped a lot.

I used an Derived Column Transformation to parse null values to zeros with the expression. The good thing is that i don't loose the mapping to the old columns. that's great.

Additionally this article helps to understand Derived Column Transformation:
http://msdn2.microsoft.com/en-us/library/ms141069.aspx

regards amigo|||

So, you got it!

That's great.

Friday, March 23, 2012

it cant be that easy

sql2k
There is no way this can be.
CREATE TABLE [dbo].[PKauthors] (
[au_id] [int] NOT NULL ,
[au_lname] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[au_fname] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[phone] [char] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
[address] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[city] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[state] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[zip] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[contract] [bit] NOT NULL
) ON [PRIMARY]
GO
CREATE TABLE [dbo].[PKtitleauthor] (
[au_id] [int] NOT NULL ,
[title_id] [tid] NOT NULL ,
[au_ord] [tinyint] NULL ,
[royaltyper] [int] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PKauthors] WITH NOCHECK ADD
CONSTRAINT [PK_PKauthors] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[PKtitleauthor] WITH NOCHECK ADD
CONSTRAINT [PK_PKtitleauthor] PRIMARY KEY CLUSTERED
(
[au_id]
) ON [PRIMARY]
GO
select a.*,ta.title_id from PKauthors a
inner join PKtitleauthor ta on a.au_id = ta.au_id
This query shows a Clustered Index Scan.
But when I change the query:
select a.*,ta.title_id from PKauthors a
inner join PKtitleauthor ta on a.au_id = ta.au_id
and a.au_id >= 0
I get a S. Could that really speed things up?
TIA, ChrisRChris,
What you see looks reasonable, but there is no reason to think a
scan is any slower than a s, if the s identifies the same
rows of the table. The following two queries return
the same 830 rows, yet the plan for the first is a scan, and for the
second it is a s. The actual work done is identical.
select * from Northwind..Orders
select * from Northwind..Orders
where OrderID > 0
Steve Kass
Drew University
ChrisR wrote:

>sql2k
>There is no way this can be.
>CREATE TABLE [dbo].[PKauthors] (
> [au_id] [int] NOT NULL ,
> [au_lname] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [au_fname] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [phone] [char] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [address] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [city] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [state] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [zip] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [contract] [bit] NOT NULL
> ) ON [PRIMARY]
>GO
>CREATE TABLE [dbo].[PKtitleauthor] (
> [au_id] [int] NOT NULL ,
> [title_id] [tid] NOT NULL ,
> [au_ord] [tinyint] NULL ,
> [royaltyper] [int] NULL
> ) ON [PRIMARY]
>GO
>ALTER TABLE [dbo].[PKauthors] WITH NOCHECK ADD
> CONSTRAINT [PK_PKauthors] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
>GO
>ALTER TABLE [dbo].[PKtitleauthor] WITH NOCHECK ADD
> CONSTRAINT [PK_PKtitleauthor] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
>GO
>
>select a.*,ta.title_id from PKauthors a
>inner join PKtitleauthor ta on a.au_id = ta.au_id
>This query shows a Clustered Index Scan.
>But when I change the query:
>select a.*,ta.title_id from PKauthors a
>inner join PKtitleauthor ta on a.au_id = ta.au_id
>and a.au_id >= 0
>I get a S. Could that really speed things up?
>TIA, ChrisR
>
>|||To elaborate on what Steve said, anytime a condition in the where clause
indicates a starting or stopping position, the plan says it is sing, even
if it is scanning EVERYTHING from that point onward. This is an extreme
situation, where the condition is saying to stop at the first value greater
than 0, which just happens to be the first row. So the work done will be
essentially the same.
HTH
--
Kalen Delaney
SQL Server MVP
www.SolidQualityLearning.com
"Steve Kass" <skass@.drew.edu> wrote in message
news:uCMfKW$ZFHA.1456@.TK2MSFTNGP15.phx.gbl...
> Chris,
> What you see looks reasonable, but there is no reason to think a
> scan is any slower than a s, if the s identifies the same
> rows of the table. The following two queries return
> the same 830 rows, yet the plan for the first is a scan, and for the
> second it is a s. The actual work done is identical.
> select * from Northwind..Orders
> select * from Northwind..Orders
> where OrderID > 0
> Steve Kass
> Drew University
> ChrisR wrote:
>|||To add a subtle point here...
In this particular case, I think that the s can be considered as less
efficient than the scan.
If you look at the plan for the first query, the scan of PK_PKauthors is not
ordered (no mention of ordered forward or backward next to the index name):
|--Nested Loops(Inner Join, OUTER REFERENCES:([a].[au_id]))
|--Clustered Index
Scan(OBJECT:([tempdb].[dbo].[PKauthors].[PK_PKauthors] AS [a]))
|--Clustered Index
S(OBJECT:([tempdb].[dbo].[PKtitleauthor].[PK_PKtitleauthor] AS [ta]),
SEEK:([ta].[au_id]=[a].[au_id]) ORDERED FORWARD)
What you see is basically a table scan where the optimizer can choose any
technique to grab the data, without needing to guarantee any order. Usually,
this means scanning the extents based on physical order on disk using IAM
bitmaps.
While the plan for the second query which says a "s," as Steve and Kalen
mentioned is just a s to the first point that matches the filter, and
then performing an ordered forward scan of all qualifying rows. In this
case, it's all table rows. This type of s can be referred to as a partial
scan, and in this case the "partial" happens to be "all".
|--Nested Loops(Inner Join, OUTER REFERENCES:([a].[au_id]))
|--Clustered Index
S(OBJECT:([tempdb].[dbo].[PKauthors].[PK_PKauthors] AS [a]),
SEEK:([a].[au_id] >= 0) ORDERED FORWARD)
|--Clustered Index
S(OBJECT:([tempdb].[dbo].[PKtitleauthor].[PK_PKtitleauthor] AS [ta]),
SEEK:([ta].[au_id]=[a].[au_id]) ORDERED FORWARD)
What I'm trying to say is:
1. You can identify that both queries ultimately perform a full scan of the
table (clustered index)
2. The first does it unordered, while the second does it ordered
Scanning in order has a cost. In this case, it's scanning the leaf of the
index following the linked list (as opposed to physical order). The
difference between the performance of the two will probably be determined by
the level of fragmentation of the index.
Your conclusion in this case should probably be, don't add the artificial
filter assuming it would improve performance. If anything, it would probably
hurt performance.
BG, SQL Server MVP
www.SolidQualityLearning.com
"ChrisR" <ChrisR@.noEmail.com> wrote in message
news:On0mc6%23ZFHA.3536@.TK2MSFTNGP10.phx.gbl...
> sql2k
> There is no way this can be.
> CREATE TABLE [dbo].[PKauthors] (
> [au_id] [int] NOT NULL ,
> [au_lname] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [au_fname] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [phone] [char] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [address] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [city] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [state] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [zip] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [contract] [bit] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[PKtitleauthor] (
> [au_id] [int] NOT NULL ,
> [title_id] [tid] NOT NULL ,
> [au_ord] [tinyint] NULL ,
> [royaltyper] [int] NULL
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[PKauthors] WITH NOCHECK ADD
> CONSTRAINT [PK_PKauthors] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[PKtitleauthor] WITH NOCHECK ADD
> CONSTRAINT [PK_PKtitleauthor] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
> GO
>
> select a.*,ta.title_id from PKauthors a
> inner join PKtitleauthor ta on a.au_id = ta.au_id
> This query shows a Clustered Index Scan.
> But when I change the query:
> select a.*,ta.title_id from PKauthors a
> inner join PKtitleauthor ta on a.au_id = ta.au_id
> and a.au_id >= 0
> I get a S. Could that really speed things up?
> TIA, ChrisR
>|||Thanks to all. I had assumed that if things were that easy, everyone would
use them. But was always under the impression a Scan was always worse than a
S.
CR
"ChrisR" <ChrisR@.noEmail.com> wrote in message
news:On0mc6%23ZFHA.3536@.TK2MSFTNGP10.phx.gbl...
> sql2k
> There is no way this can be.
> CREATE TABLE [dbo].[PKauthors] (
> [au_id] [int] NOT NULL ,
> [au_lname] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [au_fname] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [phone] [char] (12) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL ,
> [address] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [city] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [state] [char] (2) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [zip] [char] (5) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
> [contract] [bit] NOT NULL
> ) ON [PRIMARY]
> GO
> CREATE TABLE [dbo].[PKtitleauthor] (
> [au_id] [int] NOT NULL ,
> [title_id] [tid] NOT NULL ,
> [au_ord] [tinyint] NULL ,
> [royaltyper] [int] NULL
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[PKauthors] WITH NOCHECK ADD
> CONSTRAINT [PK_PKauthors] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
> GO
> ALTER TABLE [dbo].[PKtitleauthor] WITH NOCHECK ADD
> CONSTRAINT [PK_PKtitleauthor] PRIMARY KEY CLUSTERED
> (
> [au_id]
> ) ON [PRIMARY]
> GO
>
> select a.*,ta.title_id from PKauthors a
> inner join PKtitleauthor ta on a.au_id = ta.au_id
> This query shows a Clustered Index Scan.
> But when I change the query:
> select a.*,ta.title_id from PKauthors a
> inner join PKtitleauthor ta on a.au_id = ta.au_id
> and a.au_id >= 0
> I get a S. Could that really speed things up?
> TIA, ChrisR
>

Wednesday, March 7, 2012

Issue with FORMAT use in textboxes when null value

SQL 2005 Dev, VS 2005

I use the following in my VS-created reports:

FORMAT("ShortDate", Fields!someField.value)

However, when the field returns a null field what displays in the tex box instead is the words "ShortDate". Can this be corrected?

Thanks in advance

Please reverse the order of the arguments, and use DateTime format strings as shown in the MSDN documentation:

* Standard datetime formats:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp

* Custom datetime formats:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconstandarddatetimeformatstrings.asp

In your case, you should try e.g. =Format(Fields!someField.Value, "d")

Alternatively, you can set the textbox Value property to =Fields!someValue.Value and set the Format property of the textbox to the desired format code.

-- Robert

Issue when not storing null db values.

Hello:
Because of the possible issues associated with storing null values in the
db, the development team of which I am a part opted to avoid using them
when possible. This has resulted in an interesting issue -
Because we are using surrogate GUID values as primary keys, if a foreign key
value does not point to a related record, an "empty" or "not selected" value
must be stored. To accomplish this requirement we are representing empty
records with the GUID value '{00000000-0000-0000-0000-000000000000}'. So
far so good.
But here's the catch. Because we allow the database to enforce ref
integerity, a record must be created in the referenced primary key table
that corresponds with the empty record. Any any foreign key references in
our "empty" record must also contain empty guid values, which means that
empty records must be created in those related tables, etc. etc. etc.
Now, all of this seems to be working but it can be rather tedious to
maintain all of this additional data - almost all of our tables must now
have contain an "empty" record. Has me wondering if we may have made a
wrong turn at Albaquerke.
Any thoughts? Have others encountered a similar issue? Just want to make
sure we are on the right track . . .
Thanks,
Chris> Has me wondering if we may have made a wrong turn at Albaquerke.
Yea - turn back before you get to the border.
Albeit NULLs can be a pain, NULLs are certainly appropriate for unknown or
unspecified values. Too many nulls can be an indicator of a db design
issue. You should never need to create a 'dummy' row because you are using
a non-null value as a FK. That's an easy but seriously flawed kludge.
Hope this helps.
Dan Guzman
SQL Server MVP
"ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
news:OaWrwqz$FHA.360@.TK2MSFTNGP09.phx.gbl...
> Hello:
> Because of the possible issues associated with storing null values in the
> db, the development team of which I am a part opted to avoid using them
> when possible. This has resulted in an interesting issue -
> Because we are using surrogate GUID values as primary keys, if a foreign
> key value does not point to a related record, an "empty" or "not selected"
> value must be stored. To accomplish this requirement we are representing
> empty records with the GUID value
> '{00000000-0000-0000-0000-000000000000}'. So far so good.
> But here's the catch. Because we allow the database to enforce ref
> integerity, a record must be created in the referenced primary key table
> that corresponds with the empty record. Any any foreign key references in
> our "empty" record must also contain empty guid values, which means that
> empty records must be created in those related tables, etc. etc. etc.
> Now, all of this seems to be working but it can be rather tedious to
> maintain all of this additional data - almost all of our tables must now
> have contain an "empty" record. Has me wondering if we may have made a
> wrong turn at Albaquerke.
> Any thoughts? Have others encountered a similar issue? Just want to make
> sure we are on the right track . . .
> Thanks,
> Chris
>
>
>|||Wow...GUID's for primary key values!!!
You better hope that you never have to match those up by hand when debugging
an application.|||Thanks for your input, Dan. Will have to consider other options.
This brings up a related issue. Generally, are "1 to 0-1" relationships
better handled with a nullable foreign key or should the "zero/one" data be
stored in a separate table so that data will only be stored if applicable?
Chris
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:Ovz$G1z$FHA.3928@.tk2msftngp13.phx.gbl...
> Yea - turn back before you get to the border.
> Albeit NULLs can be a pain, NULLs are certainly appropriate for unknown or
> unspecified values. Too many nulls can be an indicator of a db design
> issue. You should never need to create a 'dummy' row because you are
> using a non-null value as a FK. That's an easy but seriously flawed
> kludge.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
> news:OaWrwqz$FHA.360@.TK2MSFTNGP09.phx.gbl...
>|||> This brings up a related issue. Generally, are "1 to 0-1" relationships
> better handled with a nullable foreign key or should the "zero/one" data
> be stored in a separate table so that data will only be stored if
> applicable?
Perhaps I don't understand your question correctly so you may want to post a
DDL example. It seems to me that one has a nullable FK then the
non-mandatory data are already in a separate table.
Hope this helps.
Dan Guzman
SQL Server MVP
"ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
news:ObNCj$2$FHA.356@.TK2MSFTNGP12.phx.gbl...
> Thanks for your input, Dan. Will have to consider other options.
> This brings up a related issue. Generally, are "1 to 0-1" relationships
> better handled with a nullable foreign key or should the "zero/one" data
> be stored in a separate table so that data will only be stored if
> applicable?
> Chris
> "Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
> news:Ovz$G1z$FHA.3928@.tk2msftngp13.phx.gbl...
>|||diagram 1:
Parents
--
ParentID
ParentDescription
ChildID (nullable)
Children
--
ChildID
ChildDescription
diagram 2:
Parents
--
ParentID
ParentDescription
ParentChildren
--
ParentID
ChildID
Children
--
ChildID
ChildDescription
Dan, I guess diagram 2 would illustrate what I was trying to describe.
The first diagram contains the nullable field we were initially discussing,
but in the second diagram the parent/child relationship is stored in a
separate table - ParentChildren. An entry in this table would only be made
if necessary (therefore eliminating the need to use a null value).
Was curious as to whether this would be considered the correct way to model
a "1 to 0/1" relationship. All of the database books, I have discuss the
common relationships (1-1, 1 - many, many - many), but don't touch upon this
scenario.
Thanks!
Chris
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:eSMsI73$FHA.1676@.TK2MSFTNGP09.phx.gbl...
> Perhaps I don't understand your question correctly so you may want to post
> a DDL example. It seems to me that one has a nullable FK then the
> non-mandatory data are already in a separate table.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
> news:ObNCj$2$FHA.356@.TK2MSFTNGP12.phx.gbl...
>|||"ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
news:uQv0ys$$FHA.3864@.TK2MSFTNGP12.phx.gbl...
> diagram 1:
> Parents
> --
> ParentID
> ParentDescription
> ChildID (nullable)
>
> Children
> --
> ChildID
> ChildDescription
>
> diagram 2:
> Parents
> --
> ParentID
> ParentDescription
>
> ParentChildren
> --
> ParentID
> ChildID
>
> Children
> --
> ChildID
> ChildDescription
> Dan, I guess diagram 2 would illustrate what I was trying to describe.
> The first diagram contains the nullable field we were initially
> discussing, but in the second diagram the parent/child relationship is
> stored in a separate table - ParentChildren. An entry in this table would
> only be made if necessary (therefore eliminating the need to use a null
> value).
> Was curious as to whether this would be considered the correct way to
> model a "1 to 0/1" relationship. All of the database books, I have
> discuss the common relationships (1-1, 1 - many, many - many), but don't
> touch upon this scenario.
> Thanks!
> Chris
I've built a database for managing daycares.
I use the table structure in diagram2 to map parent/child although in my
case it's more of a contactChild table.
In the contactChild table, there is a column which identifies the
relationship between the child and the contact
(mother/father/grandfather/aunt).
So a contact can be the father of one child and the uncle of another.
In your case, if you are entering only parents, I guess it's better to keep
that information in the parent table (still using diagram2)..
I don't think that a person can be one child's father and the mother of
another.
Although in today's world, I wouldn't be surprised.|||I'll assume that these aren't real entity names and are named for your
parent/child relationship example.
Let's take a closer look at the first model:
--diagram 1:
CREATE TABLE Parents
(
ParentID int NOT NULL,
ParentDescription varchar(50) NOT NULL,
ChildID int NULL
)
CREATE TABLE Children
(
ChildID int NOT NULL,
ChildDescription varchar(50) NOT NULL
)
ALTER TABLE Parents
ADD CONSTRAINT PK_Parents
PRIMARY KEY (ParentID)
ALTER TABLE Children
ADD CONSTRAINT PK_Children
PRIMARY KEY (ChildID)
ALTER TABLE Parents
ADD CONSTRAINT FK_Parents_Children
FOREIGN KEY (ChildID)
REFERENCES Children(ChildID)
This will permit orphaned children (child with no parent). If you need
referential integrity, you could remove ChildID from the Parents entity and
add ParentID to the Children entity with as a FK. This would implement your
1-0/1 cardinality, provide referential integrity and provide effective space
physical space utilization.
--diagram 1a:
CREATE TABLE Parents
(
ParentID int NOT NULL,
ParentDescription varchar(50) NOT NULL
)
CREATE TABLE Children
(
ChildID int NOT NULL,
ChildDescription varchar(50) NOT NULL,
ParentID int NOT NULL
)
ALTER TABLE Parents
ADD CONSTRAINT PK_Parents
PRIMARY KEY (ParentID)
ALTER TABLE Children
ADD CONSTRAINT PK_Children
PRIMARY KEY (ChildID)
ALTER TABLE Children
ADD CONSTRAINT FK_Children_Parents
FOREIGN KEY (ParentID)
REFERENCES Parents(ParentID)
The second model as I understand it:
--Diagram 2:
CREATE TABLE Parents
(
ParentID int NOT NULL,
ParentDescription varchar(50) NOT NULL
)
CREATE TABLE Children
(
ChildID int NOT NULL,
ChildDescription varchar(50) NOT NULL
)
CREATE TABLE ParentChildren
(
ParentID int NOT NULL,
ChildID int NOT NULL,
)
ALTER TABLE Parents
ADD CONSTRAINT PK_Parents
PRIMARY KEY (ParentID)
ALTER TABLE Children
ADD CONSTRAINT PK_Children
PRIMARY KEY (ChildID)
ALTER TABLE ParentChildren
ADD CONSTRAINT PK_ParentChildren
PRIMARY KEY (ParentID)
ALTER TABLE ParentChildren
ADD CONSTRAINT FK_ParentChildren_Parents
FOREIGN KEY (ParentID)
REFERENCES Parents(ParentID)
ALTER TABLE ParentChildren
ADD CONSTRAINT FK_ParentChildren_Children
FOREIGN KEY (ChildID)
REFERENCES Children(ChildID)
This also permits orphaned children. The only value over diagram 1a that I
see is that you could more easily modify this schema to support a many
relationship. Normally, an associative table like ParentChildren is used to
normalize a many-to-many or when additional attributes describe the
relationship.
Hope this helps.
Dan Guzman
SQL Server MVP
"ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
news:uQv0ys$$FHA.3864@.TK2MSFTNGP12.phx.gbl...
> diagram 1:
> Parents
> --
> ParentID
> ParentDescription
> ChildID (nullable)
>
> Children
> --
> ChildID
> ChildDescription
>
> diagram 2:
> Parents
> --
> ParentID
> ParentDescription
>
> ParentChildren
> --
> ParentID
> ChildID
>
> Children
> --
> ChildID
> ChildDescription
> Dan, I guess diagram 2 would illustrate what I was trying to describe.
> The first diagram contains the nullable field we were initially
> discussing, but in the second diagram the parent/child relationship is
> stored in a separate table - ParentChildren. An entry in this table would
> only be made if necessary (therefore eliminating the need to use a null
> value).
> Was curious as to whether this would be considered the correct way to
> model a "1 to 0/1" relationship. All of the database books, I have
> discuss the common relationships (1-1, 1 - many, many - many), but don't
> touch upon this scenario.
> Thanks!
> Chris
> "Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
> news:eSMsI73$FHA.1676@.TK2MSFTNGP09.phx.gbl...
>|||On Mon, 12 Dec 2005 11:39:53 -0500, ChrisB wrote:

>Hello:
>Because of the possible issues associated with storing null values in the
>db, the development team of which I am a part opted to avoid using them
>when possible. This has resulted in an interesting issue -
(snip)
Hi Chris,
In fact, there are not so many issues with null values.
There are issues with handling missing data, though. But since missing
data is a fact of life, we'll have to make do as good as we can.
There are three ways to handle missing data.
1. Use nulls. This is the special value that is defined in SQL for
exactly this reason: to represent missing data. Most of the behaviour of
null is geared towards giving you the "best" behaviour wrt missing data.
2. Use a self-chosen symbol to represent missing data. This doesn't
solve any of the issues with missing data. Instead, it makes life worse
becuase the SQL engine is NOT taught to handle whatever you use to
represent missing data as missing. You'll have to write lots of special
code to make sure that the special values is excluded whenever they need
to be excluded.
3. Use seperate tables for all values that might be missing. If a
persons name, age and sex are always known but shoe size and hair colour
might be missing, use one table to store name, ageand sex, a second
table to store shoe size and a third for haid colour. This rids you of
the nulls AND of the need to represent missing data by another special
value. It also rids you of some of the idiosyncrasies of missing data.
But you'll also find that you often need to have all the values
together, so you use outer joins to combine the tables - and then the
NULLS will be back too.
Option 2 is not a serious option. Since the problem is not the null
symbol but the missing data that this symbol represents, choosing a
different symbol to represent missing data solves nothing.
Option 1 or 3 is more a matter of taste - and of course, if performance
matters you'll want to test them both.
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Dan:
Thanks very much for your detailed examples. After giving your posts some
thought, I've decided to go with the 1a design you suggested.
Chris
"Dan Guzman" <guzmanda@.nospam-online.sbcglobal.net> wrote in message
news:%23vwv40AAGHA.3928@.tk2msftngp13.phx.gbl...
> I'll assume that these aren't real entity names and are named for your
> parent/child relationship example.
> Let's take a closer look at the first model:
> --diagram 1:
> CREATE TABLE Parents
> (
> ParentID int NOT NULL,
> ParentDescription varchar(50) NOT NULL,
> ChildID int NULL
> )
> CREATE TABLE Children
> (
> ChildID int NOT NULL,
> ChildDescription varchar(50) NOT NULL
> )
> ALTER TABLE Parents
> ADD CONSTRAINT PK_Parents
> PRIMARY KEY (ParentID)
> ALTER TABLE Children
> ADD CONSTRAINT PK_Children
> PRIMARY KEY (ChildID)
> ALTER TABLE Parents
> ADD CONSTRAINT FK_Parents_Children
> FOREIGN KEY (ChildID)
> REFERENCES Children(ChildID)
> This will permit orphaned children (child with no parent). If you need
> referential integrity, you could remove ChildID from the Parents entity
> and add ParentID to the Children entity with as a FK. This would
> implement your 1-0/1 cardinality, provide referential integrity and
> provide effective space physical space utilization.
> --diagram 1a:
> CREATE TABLE Parents
> (
> ParentID int NOT NULL,
> ParentDescription varchar(50) NOT NULL
> )
> CREATE TABLE Children
> (
> ChildID int NOT NULL,
> ChildDescription varchar(50) NOT NULL,
> ParentID int NOT NULL
> )
> ALTER TABLE Parents
> ADD CONSTRAINT PK_Parents
> PRIMARY KEY (ParentID)
> ALTER TABLE Children
> ADD CONSTRAINT PK_Children
> PRIMARY KEY (ChildID)
> ALTER TABLE Children
> ADD CONSTRAINT FK_Children_Parents
> FOREIGN KEY (ParentID)
> REFERENCES Parents(ParentID)
> The second model as I understand it:
> --Diagram 2:
> CREATE TABLE Parents
> (
> ParentID int NOT NULL,
> ParentDescription varchar(50) NOT NULL
> )
> CREATE TABLE Children
> (
> ChildID int NOT NULL,
> ChildDescription varchar(50) NOT NULL
> )
> CREATE TABLE ParentChildren
> (
> ParentID int NOT NULL,
> ChildID int NOT NULL,
> )
> ALTER TABLE Parents
> ADD CONSTRAINT PK_Parents
> PRIMARY KEY (ParentID)
> ALTER TABLE Children
> ADD CONSTRAINT PK_Children
> PRIMARY KEY (ChildID)
> ALTER TABLE ParentChildren
> ADD CONSTRAINT PK_ParentChildren
> PRIMARY KEY (ParentID)
> ALTER TABLE ParentChildren
> ADD CONSTRAINT FK_ParentChildren_Parents
> FOREIGN KEY (ParentID)
> REFERENCES Parents(ParentID)
> ALTER TABLE ParentChildren
> ADD CONSTRAINT FK_ParentChildren_Children
> FOREIGN KEY (ChildID)
> REFERENCES Children(ChildID)
> This also permits orphaned children. The only value over diagram 1a that
> I see is that you could more easily modify this schema to support a many
> relationship. Normally, an associative table like ParentChildren is used
> to normalize a many-to-many or when additional attributes describe the
> relationship.
> --
> Hope this helps.
> Dan Guzman
> SQL Server MVP
> "ChrisB" <pleasereplytogroup@.thanks.com> wrote in message
> news:uQv0ys$$FHA.3864@.TK2MSFTNGP12.phx.gbl...
>

Friday, February 24, 2012

Issue inserting null value into a formview/gridview control

Hi,

My formview or gridview control stops updating or deleting a record once the record has a null value.

I have table tblTest with the following

pkID int NOT NULL **IDENTITY COLUMN**
string1 varchar(30)
string2 varchar(30)

I then create a SqlDataSource with the statement:

Select * From [tblTest]

I have the insert, update and delete statements generated, and choose optimistic concurrency. I add a couple records of dummy data.

I then drag a Formview control onto the page, and bind it to the SqlDataSource I just created. I then fire it up in my browser, and I can then update, insert and delete records. However, as soon as I update a record with a null value, I can no longer update or delete that record.

So, if I had a record in my FormView like:

string1: foo
string2: bar

I can update and delete normally. And when I update to:

string1: foo
string2:

the database correctly inserts a null value into string2. However, once that null is in the record, I can't change anything about the record. If I try to delete the record, the FormView will then display the previous record, but I can still page to the record that should have been deleted, and it still exists in the db. If I try to update the record, the edits I make will not keep and the process will fail silently.

What am I doing wrong? Should i be binding to a different object?

Regards,

Chris

Do you set your DataKeyNames for the formview? ( I thought you did). Anyway, here is a working copy and you may find your isseue by yourself.

<asp:FormView ID="fv1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="id"
AllowPaging="true" >

<EditItemTemplate>

col1:

<asp:TextBox ID="col1TextBox" runat="server" Text='<%# Bind("col1") %>'></asp:TextBox><br />
col2:

<asp:TextBox ID="col2TextBox" runat="server" Text='<%# Bind("col2") %>'></asp:TextBox><br />

<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"

Text="Update"
</asp:LinkButton
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

Text="Cancel"
</asp:LinkButton
</EditItemTemplate>
<InsertItemTemplate
col1:

<asp:TextBox ID="col1TextBox" runat="server" Text='<%# Bind("col1") %>'></asp:TextBox><br />
col2:

<asp:TextBox ID="col2TextBox" runat="server" Text='<%# Bind("col2") %>'></asp:TextBox><br /

<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"

Text="Insert"
</asp:LinkButton
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"

Text="Cancel"
</asp:LinkButton
</InsertItemTemplate>
<ItemTemplate
id:

<asp:Label ID="idLabel" runat="server" Text='<%# Eval("id") %>'></asp:Label><br /
col1:

<asp:Label ID="col1_Label" runat="server" Text='<%# Bind("col1") %>'></asp:Label><br />
col2:
<asp:Label ID="col2_Label" runat="server" Text='<%# Bind("col2") %>'></asp:Label><br /
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"

Text="Delete"
</asp:LinkButton
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"

Text="Edit"
</asp:LinkButton
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="New"

Text="New"
</asp:LinkButton
</ItemTemplate>

</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn1 %>"
DeleteCommand="DELETE FROM [test1] WHERE [id] = @.id"
InsertCommand="INSERT INTO [test1] ( [col1], [col2]) VALUES ( @.col1, @.col2)"
SelectCommand="SELECT [id], [col1], [col2] FROM [test1]"
UpdateCommand="UPDATE [radios] SET [col1] = @.col1, [col2] = @.col2 WHERE [id] = @.id">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<UpdateParameters>

<asp:Parameter Name="col1" Type="String" />
<asp:Parameter Name="col2" Type="String" />
<asp:Parameter Name="id" Type="Int32" />
</UpdateParameters>
<InsertParameters>

<asp:Parameter Name="col1" Type="String" />
<asp:Parameter Name="col2" Type="String" />
</InsertParameters>
</asp:SqlDataSource>

|||

Ok, our SqlDataSources differ somewhat. Was your auto-generated, or did you create it manually? The following code was generated for me:

============================================================================

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues" ConnectionString="<%$ ConnectionStrings:ConnectionString%>" DeleteCommand="DELETE FROM [tblTest] WHERE [id] = @.original_id AND [col1] = @.original_col1 AND [col2] = @.original_col2" InsertCommand="INSERT INTO [tblTest] ([col1], [col2]) VALUES (@.col1, @.col2)" OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [tblTest]" UpdateCommand="UPDATE [tblTest] SET [col1] = @.col1, [col2] = @.col2 WHERE [id] = @.original_id AND [col1] = @.original_col1 AND [col2] = @.original_col2"> <DeleteParameters> <asp:Parameter Name="original_id" Type="Int32" /> <asp:Parameter Name="original_col1" Type="String" /> <asp:Parameter Name="original_col2" Type="String" /> </DeleteParameters> <UpdateParameters> <asp:Parameter Name="col1" Type="String" /> <asp:Parameter Name="col2" Type="String" /> <asp:Parameter Name="original_id" Type="Int32" /> <asp:Parameter Name="original_col1" Type="String" /> <asp:Parameter Name="original_col2" Type="String" /> </UpdateParameters> <InsertParameters> <asp:Parameter Name="col1" Type="String" /> <asp:Parameter Name="col2" Type="String" /> </InsertParameters> </asp:SqlDataSource>
=============================================================================================
 Specifically, my update and delete statements have different, more complicated WHERE clauses. When I change the WHERE clause so that it only uses the ID, the formview control works as it should:
 DeleteCommand="DELETE FROM [tblTest] WHERE [id] = @.original_id"

UpdateCommand

="UPDATE [tblTest] SET [col1] = @.col1, [col2] = @.col2 WHERE [id] = @.original_id"

Am I generating my statements incorrectly? It's bizarre that the SqlDataSource auto-generated command statements choke once they start working with null fields...

Regards,


Chris

|||

Hello,

Both of the codes are generated by the IDE, but you checked the "Use Optimistic Concurrency" to get the more complicated where clause. You may need find more information to use this. If you don't check this one, you will have the same code as I have.

HTH.

|||

That did it, thanks so much!

Regards,

Chris

|||The wizard portion that generates SQL Strings for optimistic queries has that bug. You can still use the wizard to generate the basic query, but you'll need to go in and modify it if any of the fields can be null.

Monday, February 20, 2012

isql problem with null values

I am having a problem running a stored procedure using isql to launch the procedure.

My stored procedure contains a statement like:

select @.myvar = count(*) from mytable where mycolumn is null

when I run the procedure from query analyser a value is set in @.myvar showing the correct number of null values from mytable which is output to another table where I can see it.

Without making any changes, when I run the identical procedure using isql to execute the procedure @.myvar gets set to zero. The procedure cannot find the null values in mytable

I have tried setting ANSI_NULLS on and off inside the procedure but still get the same result.

Can someone please tell me what is going on, and how I might get isql to show me how many null values exist in the table?I don't seems to have anyproblem with this

USE Northwind
GO

CREATE TABLE myTable99(Col1 int)
GO

INSERT INTO myTable99(Col1)
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT null UNION ALL
SELECT null UNION ALL
SELECT null
GO

CREATE PROC mySproc99 @.Rows int OUTPUT AS
SELECT @.Rows = COUNT(*) FROM myTable99 WHERE Col1 IS NULL
GO

DECLARE @.Rows int
EXEC mySproc99 @.Rows OUTPUT
SELECT @.Rows

DECLARE @.cmd varchar(8000), @.sql varchar(8000)
SELECT @.sql = 'DECLARE @.Rows int EXEC mySproc99 @.Rows OUTPUT SELECT @.Rows'
SELECT @.cmd = 'osql -U <username> -P <password> -S <servername> -d Northwind -Q "' + @.sql + '"'
EXEC master..xp_cmdshell @.cmd
GO

DROP PROC mySproc99
DROP TABLE myTable99
GO|||Which SQL Server are you running? If SQL 2000, which campatibility level?|||Brett,

Thanks for your reply, I'll try out your script tomorrow morning when I get into the office. And let you know the result.

The reason I'm confused, is I set up my script using osql and it worked fine on our test server, It didn't run at all on production, then I found that it did run if I changed osql to isql, but with the quirk noted. I'm sure it is something to do with the OLE DB config, but I am not sure where to start looking for differences. Also the osql version might work if I could get ODBC set up on production, but I'm not sure how to go about doing that.

Anyway, thanks again for all your help and I'd be grateful for any further suggestions.|||I don't have tons of experience with xp_cmdshell but my results from the example script above look pretty strange:

www.sullivanstreet.us/query.jpg