dbTalk Databases Forums  

Correct syntax please

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Correct syntax please in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
M Joomun
 
Posts: n/a

Default Correct syntax please - 10-14-2010 , 07:48 AM






Hello,

I'm building a Windows Forms App with VB.NET using SQL Server 2008 R2.

I've got a search form where users can search for records from the db.
The App calls a stored procedure which executes the sql below. The form
has two search criteria users have requested, 'labno' and 'DateAssayReq'.

The App works well for the date criteria and will find the correct
record(s), but will not return any records whatever I enter for the
labno, even though I know the labno exists in the db.

Can someone see what I've done wrong?

TIA.


<SQL>

@labno nvarchar(6) = NULL,
@DateAssayReq date


AS

SELECT assayid, DateAssayReq, LabNo, ReasonRepeat,
PersonReq, RepeatedYN, DateRepeated,
DtRepeatDy, PersonRepeat, ReasonNotRepeated, LIMSUpdateYN

FROM tblassay

WHERE (datalength(@labno) = 0 OR LabNo = @labno)
AND (DataLength(@DateAssayReq) = 0 OR DateAssayReq = @DateAssayReq)

</SQL>

Reply With Quote
  #2  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Correct syntax please - 10-14-2010 , 10:07 AM






M Joomun (m.a.joomun (AT) qmul (DOT) ac.uk) writes:
Quote:
I've got a search form where users can search for records from the db.
The App calls a stored procedure which executes the sql below. The form
has two search criteria users have requested, 'labno' and 'DateAssayReq'.

The App works well for the date criteria and will find the correct
record(s), but will not return any records whatever I enter for the
labno, even though I know the labno exists in the db.

Can someone see what I've done wrong?
The part with datalength looks funky. "DataLength(@DateAssayReq)" will
never be 0. It will be 3 or NULL.

Use:

WHERE (@labno IS NULL OR LabNo = @labno)
AND (@DateAssayReq IS NULL OR DateAssayReq = @DateAssayReq)


Also make sure that you pass NULL for search conditions that do not apply.

--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx

Reply With Quote
  #3  
Old   
M Joomun
 
Posts: n/a

Default Re: Correct syntax please - 10-18-2010 , 03:32 AM



On 14/10/2010 16:07, Erland Sommarskog wrote:
Quote:
M Joomun (m.a.joomun (AT) qmul (DOT) ac.uk) writes:
I've got a search form where users can search for records from the db.
The App calls a stored procedure which executes the sql below. The form
has two search criteria users have requested, 'labno' and 'DateAssayReq'.

The App works well for the date criteria and will find the correct
record(s), but will not return any records whatever I enter for the
labno, even though I know the labno exists in the db.

Can someone see what I've done wrong?

The part with datalength looks funky. "DataLength(@DateAssayReq)" will
never be 0. It will be 3 or NULL.

Use:

WHERE (@labno IS NULL OR LabNo = @labno)
AND (@DateAssayReq IS NULL OR DateAssayReq = @DateAssayReq)


Also make sure that you pass NULL for search conditions that do not apply.

Thanks for your response. The syntax you gave was what I was trying
originally and there's no difference in the results returned; correct
results if 'DateAssayReq' is used as search criteria, no results
returned if labno is used, either on it's own or in conjunction with
'DateAssayReq'.

Any other ideas?

Reply With Quote
  #4  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: Correct syntax please - 10-18-2010 , 07:19 AM



On Mon, 18 Oct 2010 09:32:39 +0100, M Joomun wrote:

Quote:
Thanks for your response. The syntax you gave was what I was trying
originally and there's no difference in the results returned; correct
results if 'DateAssayReq' is used as search criteria, no results
returned if labno is used, either on it's own or in conjunction with
'DateAssayReq'.

Any other ideas?
Hi M Joomun,

That might be caused by a mismatch between the data type of the
parameter and the data type of the column.
Can you post the exact structure of the table, as a CREATE TABLE
statement? (You can use the generate function in SSMS to get this, or if
you have your table creation scripts in source control, you can use
those).

--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Reply With Quote
  #5  
Old   
M Joomun
 
Posts: n/a

Default Re: Correct syntax please - 10-19-2010 , 03:42 AM



On 18/10/2010 13:19, Hugo Kornelis wrote:
Quote:
On Mon, 18 Oct 2010 09:32:39 +0100, M Joomun wrote:

Thanks for your response. The syntax you gave was what I was trying
originally and there's no difference in the results returned; correct
results if 'DateAssayReq' is used as search criteria, no results
returned if labno is used, either on it's own or in conjunction with
'DateAssayReq'.

Any other ideas?

Hi M Joomun,

That might be caused by a mismatch between the data type of the
parameter and the data type of the column.
Can you post the exact structure of the table, as a CREATE TABLE
statement? (You can use the generate function in SSMS to get this, or if
you have your table creation scripts in source control, you can use
those).

Hello,

Thanks for the reply. As requested, below is the structure of the table
in question. Not sure if you wanted everything the create statement put
out, but shout if you do:

CREATE TABLE [dbo].[tblAssay](
[AssayID] [int] IDENTITY(1,1) NOT NULL,
[ADtdate] [datetime] NULL,
[ADtTime] [datetime] NULL,
[LabNo] [nvarchar](6) NULL,
[AssayTy] [smallint] NULL,
[ReasonRepeat] [tinyint] NULL,
[DateAssRequested] [datetime] NULL,
[TimeAssRequested] [datetime] NULL,
[PersonReq] [smallint] NULL,
[RepeatedYN] [smallint] NULL,
[DateRepeated] [datetime] NULL,
[DtRepeatDy] [nvarchar](9) NULL,
[PersonRepeat] [smallint] NULL,
[ReasonNotRepeated] [smallint] NULL,
[LIMSUpdateYN] [smallint] NULL,
[DateAssayReq] [date] NULL,
CONSTRAINT [aaaaatblAssay_PK] PRIMARY KEY NONCLUSTERED

Reply With Quote
  #6  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Correct syntax please - 10-19-2010 , 04:58 PM



M Joomun (m.a.joomun (AT) qmul (DOT) ac.uk) writes:
Quote:
Thanks for the reply. As requested, below is the structure of the table
in question. Not sure if you wanted everything the create statement put
out, but shout if you do:

CREATE TABLE [dbo].[tblAssay](
[AssayID] [int] IDENTITY(1,1) NOT NULL,
[ADtdate] [datetime] NULL,
[ADtTime] [datetime] NULL,
[LabNo] [nvarchar](6) NULL,
[AssayTy] [smallint] NULL,
[ReasonRepeat] [tinyint] NULL,
[DateAssRequested] [datetime] NULL,
[TimeAssRequested] [datetime] NULL,
[PersonReq] [smallint] NULL,
[RepeatedYN] [smallint] NULL,
[DateRepeated] [datetime] NULL,
[DtRepeatDy] [nvarchar](9) NULL,
[PersonRepeat] [smallint] NULL,
[ReasonNotRepeated] [smallint] NULL,
[LIMSUpdateYN] [smallint] NULL,
[DateAssayReq] [date] NULL,
CONSTRAINT [aaaaatblAssay_PK] PRIMARY KEY NONCLUSTERED
So far it looks good. But I think we need to see the whole picture.

So can you produce a script that includes:

1) Some INSERT statements.
2) The full code for the stored procedures.
3) Some calls to the stored procedure that demonstrates the problem?


--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.