473,395 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Stored Procedure and multiple keys which could be NULL

Gaz
I have a table which has 10 columns which make up the secondary key. 1
or more of these columns can be set but the remaining columns in the
secondary key will be null. For example :
id k1 k2 k3 k4 k5 k6 k7 k8 k9 k10 data
-------------------------------------------------
0 1 1 - - - - - - - - test0
1 1 1 1 - - - - - - - test1
2 1 1 2 - - - - - - - test2
3 1 1 3 - - - - - - - test3
4 1 1 3 1 - - - - - - test4
5 1 2 1 - - - - - - - test5
6 1 2 2 - - - - - - - test6

Each row represents a node in a tree structure, the secondary key
columns point to which node in the tree. The above example has one
node in tree branch 1, three nodes in tree branch 1-1, one node in
branch 1-1-3 and two nodes in branch 1-2.

My question is how can I write a single stored procedure to return
only the nodes in a given tree branch without needing logic based on
looking for NULL parameters. I have written the stored procedure
below. If I want to retrieve all nodes in branch 1-1 (ie; test1, test2
and test3) then I want be able to call :

execute "mysp Test" 1, 1

But this only returns a single record, test0. The reason for this is
obvious if you look at the stored procedure. What I really need is way
of saying

execute "mysp Test" 1, 1, NOT NULL

so that it returns all records in branch 1-1 where Key3 is NOT NULL
but all subsequent keys are null, ie; Key4-Key10 are NULL. The stored
procedure must work with any number of secondary keys though, so I
could use it to call :

execute "mysp Test" 1, 1, 3, NOT NULL

which would retrieve test4.

Can anybody help me out here?

Thanks for reading.
Here's the stored procedure as it stands :

CREATE PROCEDURE [dbo].[mysp Test]
( @key1 smallint,
@key2 smallint=NULL,
@key3 smallint=NULL,
@key4 smallint=NULL,
@key5 smallint=NULL,
@key6 smallint=NULL,
@key7 smallint=NULL,
@key8 smallint=NULL,
@key9 smallint=NULL,
@key10 smallint=NULL ) AS

select * from TreePositionTest
where
key1=@key1 and
key2=@key2 and
key3=@key3 and
key4=@key4 and
key5=@key5 and
key6=@key6 and
key7=@key7 and
key8=@key8 and
key9=@key9 and
key10=@key10
GO
Jul 23 '05 #1
3 1738
Get a copy of TREES & HIERARCHIES IN SQL for several other methods that
model trees. I think you will find that the nested sets model will run
a order of magnitude faster as your tree grows.

Jul 23 '05 #2

I have since had a rethink and have decided to have a ParentId column
which links nodes to the parent branch. I will also have a Sequence
column to provide ordering of nodes. When inserting or deleting nodes I
will have to update the subsequent nodes' Sequence column (although for
insertions I could stagger the Sequence number so I only have to update
the next node if the sequences overlap) but it will only be for that
particular branch so it won't be a problem as I don't envisage more
than ten nodes per branch although even a few hundred still won't be a
problem.

--CELKO-- wrote:
Get a copy of TREES & HIERARCHIES IN SQL for several other methods that model trees. I think you will find that the nested sets model will run a order of magnitude faster as your tree grows.


Jul 23 '05 #3
Gaz (oo******@yahoo.co.uk) writes:
My question is how can I write a single stored procedure to return
only the nodes in a given tree branch without needing logic based on
looking for NULL parameters. I have written the stored procedure
below. If I want to retrieve all nodes in branch 1-1 (ie; test1, test2
and test3) then I want be able to call :

execute "mysp Test" 1, 1

But this only returns a single record, test0. The reason for this is
obvious if you look at the stored procedure.
Hm, your procedure should not really return anything at all. Obviously
you are running with ANSI_NULLS off, and that gives you a single row.
But ANSI_NULLS is a legacy option, and you should be running with it
ON.
What I really need is way of saying

execute "mysp Test" 1, 1, NOT NULL

so that it returns all records in branch 1-1 where Key3 is NOT NULL
but all subsequent keys are null, ie; Key4-Key10 are NULL. The stored
procedure must work with any number of secondary keys though, so I
could use it to call :
You will have to use some value as a wild card, for instance -1.
Here's the stored procedure as it stands :

CREATE PROCEDURE [dbo].[mysp Test]
( @key1 smallint,
@key2 smallint=NULL,
@key3 smallint=NULL,
...
@key10 smallint=NULL ) AS

select * from TreePositionTest
where
key1=@key1 and
key2=@key2 and
key3=@key3 and
...
key10=@key10
GO


SELECT * FROM TreePositionTest
WHERE (key1 = @key1 OR key1 IS NULL AND @key1 IS NULL OR @key1 = -1)
-- etc.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
I came across an article in SQL Mag about Crosstab Queries. It works great in Query Analyzer, but I'm stuck on how to use it in an Access ADP. I need to use it as a Recordsource in a form and...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
45
by: John | last post by:
Hi When developing vb.bet winform apps bound to sql server datasource, is it preferable to use SELECTs or stored procedure to read and write data from/to SQL Server? Why? Thanks Regards
7
by: Dabbler | last post by:
I'm using an ObjectDataSource with a stored procedure and am getting the following error when trying to update (ExecuteNonQuery): System.Data.SqlClient.SqlException: Procedure or Function...
1
by: deepdata | last post by:
Hi, I am trying to fetch data from db2 (express version) database by calling stored procedure. I have tried to use both cursor and for loop but still i am getting error. --======Start...
2
by: karups | last post by:
Hi, I've got listbox in my .aspx page where the users can make multiple selection. So, Users can select any number of items in listbox, I have to take value from items and pass it to stored...
4
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine...
4
by: yin_n_yang74 | last post by:
I am new to SQL and SQL Server world. There must be a simple solution to this, but I'm not seeing it. I am trying to create a crystal report (v8.5) using a stored procedure from SQL Server...
4
by: gamaz | last post by:
Hi, I am trying to work on a stored procedure that will work with multiple database. I have a prototype of multiple databases. Those are named as the following: ts2_aldkm_app, ts2_aldkp_app,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.