Showing posts with label following. Show all posts
Showing posts with label following. Show all posts

Friday, March 30, 2012

How to rename a column in SqlCE?

When I execute EXEC sp_rename 'PATHSPECIMEN.PVVALUE', 'PVPROC', 'COLUMN', I am getting the following error:
Major Error 0x80004005, Minor Error 25621
> EXEC sp_rename 'PATHSPECIMEN.PVVALUE', 'PVPROC', 'COLUMN'
The specified argument value for the procedure is not valid. [ Argument # = 3,Name of procedure(if known) = sp_rename ]

Could some one guide me where I am wrong?
I am executing this query against SqlCE in Sql Server 2005 Management Studio.

The sp_rename procedure is not avialable in SQL CE!

In Sql Server 2005 Management Studio you have to create a new column with the new name, then you have to update it with the values from your old column and then you have to delete your old column. The last action is difficult if the column is (part of) an index.

ALTER TABLE YOURTABLENAME ADD PVPROC INTEGER (or your datatype)
UPDATE YOURTABLENAME SET PVPROC = PATHSPECIMEN.PVVALUE
ALTER TABLE School DROP COLUMN PATHSPECIMEN.PVVALUE

I did it yesterday myself in this way!

Anne-Jan Tuinstra

|||Hi,

Thanks for your prompt reply and for the nice solution.

As you said that column is a part of index, how to go to resolve this case?.

Thanks
GSR|||

I think you/ I have to remove the index.

Up till now it didn't work with the manager.

My table now has one column to many!!!!!

How to remove unwanted characters in SQL?

Hi all,

I have a stored procedure that generates the following SQL WHERE clause

UserName LIKE 'Adi234%' AND Fname LIKE 'David%' AND LName LIKE 'Justin%' AND

It is good except that it i can not remove the last AND which is not neccessary at the end of the clause.

I want to remove the last AND that come up at the end, my code places AND after each data field(UserName, Fname, Lname) .

Thanks

a quick and dirty way: add

1=1

after the query (which always is goodWink

|||

In your stored procedure, you can use a Substring to remove the end AND:

Assume @.strSQL is the SQL statement string you are returning now. Add this line:

SET @.strSQL=SUBSTRING(@.strSQL, 0,LEN(@.strSQL)-3)

|||

Thanks Limno, that works great

Monday, March 26, 2012

How to remove a file [dbcc shrinkfile(filename,empty) is not working]

I tried to remove a file from a filegroup by using the following two comamnd
1) dbcc SHRINKFILE('FileName1', EMPTYFILE)
2) ALTER DATABASE PED_PROD REMOVE FILE FileName1

but I am getting the following error message

Server: Msg 5042, Level 16, State 1, Line 1
The file ''FileName1'' cannot be removed because it is not empty.

Please help in this

Thanks in Advance,
SateeshHowdy

What version of SQL are you using?

Cheers

SG

Friday, March 23, 2012

How to refer to calculated field ?

When I do the following sql statement, I got the error "Invalid column name
'buy'."
SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
Ddif = case when [buy]=0 then 1 else 2 end --> error here
FROM tbl1
How can I refer to Buy in the same sql statement ?
Thanks.That is not a calculated field, it is a column alias. You cannot refer it in
other clauses in the same select statement unless it is nested in a derived
table. Your options are:
SELECT col,
< expression using col >
FROM ( SELECT CASE WHEN col = 'buy'
THEN 1 ELSE 2
END
FROM tbl ) t ( col ) ;
You can also reuse the same expression to avoid the derived table construct
as well.
Anith|||You can't. You'll have to use a derived table. Something like:
select DerivedTable.Buy
,Ddif = case when DerivedTable.Buy=0 then 1 else 2 end
from (
SELECT Buy = case when [col1]='BUY' then 1 else 2 end
FROM tbl1
) DerivedTable
Of course this is untested.
ML
http://milambda.blogspot.com/|||Your first 'BUY' statement is pretty simple - just 1 or 2 based on col1 =
BUY, so how about :
SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
Ddif = case when [col1]<>'BUY' then 1 else 2 end
FROM tbl1
Or something like that. In other words, build the logic for Ddif back to
the source column unaltered by the first case statement.
However, there may be a problem in your logic in terms of what happens first
and second, etc. See what you think:
If you are saying:
For COL1 if 'BUY' then 1 else 2
then after that is done there will be 0 rows where BUY = 0, the only options
are 1 or 2.
So the dDif column will always be 2 no matter what.
"fniles" wrote:
> When I do the following sql statement, I got the error "Invalid column nam
e
> 'buy'."
> SELECT Buy = case when [col1]='BUY' then 1 else 2 end,
> Ddif = case when [buy]=0 then 1 else 2 end --> error here
> FROM tbl1
> How can I refer to Buy in the same sql statement ?
> Thanks.
>
>|||Thank you everybody for your help.

> You can also reuse the same expression to avoid the derived table
> construct
How can I reuse the same expression ? In my case, I will need to reuse the
column alias in more than 1 places in the same sql statement.
For ex:
SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
ABC = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:OamRSFf%23FHA.328@.TK2MSFTNGP14.phx.gbl...
> That is not a calculated field, it is a column alias. You cannot refer it
> in other clauses in the same select statement unless it is nested in a
> derived table. Your options are:
> SELECT col,
> < expression using col >
> FROM ( SELECT CASE WHEN col = 'buy'
> THEN 1 ELSE 2
> END
> FROM tbl ) t ( col ) ;
> You can also reuse the same expression to avoid the derived table
> construct as well.
> --
> Anith
>|||I am wondering since I will do this in a stored procedure and I need to use
the derived column more than once in the same sql statement, would it be
possible to use a temporary table for the derived column, and if it is how
can I do that ? Thanks.
The sql statement:
SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
ABC = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>'0'))
"fniles" <fniles@.pfmail.com> wrote in message
news:%23SD3Bsn%23FHA.360@.TK2MSFTNGP09.phx.gbl...
> Thank you everybody for your help.
>
> How can I reuse the same expression ? In my case, I will need to reuse the
> column alias in more than 1 places in the same sql statement.
> For ex:
> SELECT right('0000'+ convert(varchar(4),COL1),4) AS BUY,
> ABC = case when BUY>0 then
> (
> Left(BUY,charindex('.',BUY)-1)*32
> +
> substring(BUY,charindex('.',BUY)+1,2)*2
> +
> case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
> )
> else Null
> end
>
> "Anith Sen" <anith@.bizdatasolutions.com> wrote in message
> news:OamRSFf%23FHA.328@.TK2MSFTNGP14.phx.gbl...
>|||Please post a simplified version of your table schema with a few sample data
& expected results. Include a brief explanation of what you are trying to
accomplish here.
Based on the code snippet you posted, it looks mostly like some formatting
issue that, in general, can be handled differently, perhaps even outside the
database server.
Anith|||I am actually trying to convert Access db to SQL Server, and the query I
posted was a part of a longer query that I need to convert.
CREATE TABLE tblA (
Price float NULL,
CC varchar(50) NULL,
Account varchar(50) NULL,
Quantity smallint NULL,
:
)
CREATE TABLE tblB (
CC varchar(50) NULL,
:
)
CREATE TABLE tblC (
Account varchar(50) NULL,
:
)
SELECT LEFT(convert(varchar(8),Price),8)+'0000'
,8) AS BUY,
HClose = case when BUY>0 then
(
Left(BUY,charindex('.',BUY)-1)*32
+
substring(BUY,charindex('.',BUY)+1,2)*2
+
case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
)
else Null
end
from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>'0'))
Example:
tblA
Price CC Account
6 1212.25 ES 12345
6 0 ES 12345
1 1585.5 NQ 12345
1 51 NQ 12345
tblB
Symbol
ES
NQ
tblC
Account
12345
Result:
Buy HClose Account
ES 1212.2500 77618 12345
ES 0.0000 12345
NQ 1585.5000 101540 12345
NQ 51.0000 3352 12345
I am also having a problem converting a floating number to a 4 decimal
floating number: ex: from 1212.25 to 1212.2500, 0 to 0.0000, etc.
In my Stored Procedure, can I just derived the column Buy in a temp table,
and refer it in the next sql statement ?
Thanks.
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
> Please post a simplified version of your table schema with a few sample
> data & expected results. Include a brief explanation of what you are
> trying to accomplish here.
> Based on the code snippet you posted, it looks mostly like some formatting
> issue that, in general, can be handled differently, perhaps even outside
> the database server.
> --
> Anith
>|||Just to add, the derived column HClose is also going to be used again in the
same query.
This is from the Access query:
IIf([buy]=0,[Hprice]-[HCLOSE],[HCLOSE]-[Hprice]) AS Hdif
"fniles" <fniles@.pfmail.com> wrote in message
news:etFhZ7o%23FHA.1676@.TK2MSFTNGP09.phx.gbl...
>I am actually trying to convert Access db to SQL Server, and the query I
>posted was a part of a longer query that I need to convert.
> CREATE TABLE tblA (
> Price float NULL,
> CC varchar(50) NULL,
> Account varchar(50) NULL,
> Quantity smallint NULL,
> :
> )
> CREATE TABLE tblB (
> CC varchar(50) NULL,
> :
> )
> CREATE TABLE tblC (
> Account varchar(50) NULL,
> :
> )
> SELECT LEFT(convert(varchar(8),Price),8)+'0000'
,8) AS BUY,
> HClose = case when BUY>0 then
> (
> Left(BUY,charindex('.',BUY)-1)*32
> +
> substring(BUY,charindex('.',BUY)+1,2)*2
> +
> case when substring(BUY,charindex('.',BUY)+3,1) = 5 then 1 else 0 end
> )
> else Null
> end
> from (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
> tblA.Account = tblC.Account
> WHERE (((tblA.Quantity)<>'0'))
> Example:
> tblA
> Price CC Account
> 6 1212.25 ES 12345
> 6 0 ES 12345
> 1 1585.5 NQ 12345
> 1 51 NQ 12345
> tblB
> Symbol
> ES
> NQ
>
> tblC
> Account
> 12345
>
> Result:
> Buy HClose Account
> ES 1212.2500 77618 12345
> ES 0.0000 12345
> NQ 1585.5000 101540 12345
> NQ 51.0000 3352 12345
>
> I am also having a problem converting a floating number to a 4 decimal
> floating number: ex: from 1212.25 to 1212.2500, 0 to 0.0000, etc.
> In my Stored Procedure, can I just derived the column Buy in a temp table,
> and refer it in the next sql statement ?
> Thanks.
>
> "Anith Sen" <anith@.bizdatasolutions.com> wrote in message
> news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
>|||This is a part of the original Access query that I need to convert to SQL
Server:
SELECT Format([tblA].[PRICE],"0.0000") AS BUY,
IIf(Val([BUY])>0,(Left$([BUY],InStr([BUY],".")-1)*32+Mid$([BUY],InStr([BUY],".")+1,2))*2+(IIf(Mid$([BUY],InStr(
[BUY],".")+3,1)=5,1,0)),Null)
AS HClose,
IIf([buy]=0,1-[HCLOSE],[HCLOSE]-1) AS Hdif,...
FROM (tblA LEFT JOIN tblB ON tblA.CC = tblB.CC) LEFT JOIN tblC ON
tblA.Account = tblC.Account
WHERE (((tblA.Quantity)<>"0"));
"Anith Sen" <anith@.bizdatasolutions.com> wrote in message
news:uSBRY5n%23FHA.3104@.TK2MSFTNGP15.phx.gbl...
> Please post a simplified version of your table schema with a few sample
> data & expected results. Include a brief explanation of what you are
> trying to accomplish here.
> Based on the code snippet you posted, it looks mostly like some formatting
> issue that, in general, can be handled differently, perhaps even outside
> the database server.
> --
> Anith
>sql

Monday, March 12, 2012

How to rebuild the master database.?

Installed SQLSERVER 2005 EVAL copy in my server and trying to rebuild the master database and it is not working.

I tried the following command.

start /wait setup.exe /qn INSTANCENAME=MSSQLSERVER REINSTALL=SQL_Engine REBUILDDATABASE=1 SAPWD=RAJESH

Any thoughts?

Thanks

First off, can you help me understand why you need to rebuild master instead of restoring from backup? Rebuilding master is typically done when it is too corrupt to allow the instance to start.

If the instance is running, that would probably prevent the procedure above from working. It isn't intended for running instances.

If your instance is down and it still doesn't work, please post the exact error message and we'll see what we can find out.

Friday, February 24, 2012

How to query?

In the following query, @.CusVenRefLike is a string entered by user, it is by default equal to '%' so that the query return all string when user enter nothing.

SELECT Voucher.VoucherID, Voucher.VoucherRef, Customer.CustomerRef
FROM Voucher
LEFT Join Customer on Voucher.CustomerID=Customer.CustomerID
WHERE Customer.CustomerRef LIKE @.CusVenRefLike

I find that the resulting dataset contain ONLY rows that Voucher.CustomerID<>-1. The reason is that when Voucher.CustomerID= -1, Customer.CustomerRef=NULL which is not included in Customer.CustomerRef LIKE '%' filter.

How can I include those row with Voucher.CustomerID=-1?

Thanks

You need to add additional constraints to your query, depending on exactly how you want it to work.

A simple approach is to use Coalesce(CustomerRef, '' ) to force the null into an empty string. This allows the match to procede. In fact, if there is no special meaning to the CustomerRef being NULL you might want to add a default to the column.

If you want to include nulls in any search, then add Or CustomerRef Is Null to the Where clause. You could also add Or (CustomerRef Is Null and @.CusVenRefLike = '%').

|||

SELECT Voucher.VoucherID, Voucher.VoucherRef, Customer.CustomerRef
FROM Voucher
LEFT Join Customer on Voucher.CustomerID=Customer.CustomerID
WHERE Customer.CustomerRef LIKE @.CusVenRefLike

or Customer.CustomerRef is null

|||

Can do it as this as well..

SELECT Voucher.VoucherID, Voucher.VoucherRef, Customer.CustomerRef
FROM Voucher
LEFT Join Customer on Voucher.CustomerID=Customer.CustomerID
WHERE isnull(Customer.CustomerRef ,'') LIKE @.CusVenRefLike

How to query this?

Hello,
Long time I am puzzled over the following task. This problem should be
similar to many systems, and it would be interesting to know what are best
practices to solve this.
The setup:
Table, that contains log of events. Typical fields would be: 1) uniqueID; 2)
evtTime; 3)evtType. Then there are some additional fields that carry
additional logged event data.
Sample data:
ID evtTime evtType
1 10-11-2005 A
2 13-11-2005 B
3 20-11-2005 C
4 22-11-2005 B
Now, if I want to perform analysis on this data, typical need would be to
analyze sequence of event types. In the sample data above, the sequence is
A-B-C-B (ordering by date-time). I find it very complex to create a query
that would let me find sequence of events by given pattern.
For example, I could use query to discover the next element in sequence and,
thus, use this expression for querying:
SELECT evtType, (SELECT TOP 1 evtType FROM SampleData WHERE
evtTime>Level1.evtTime ORDER BY logDate) AS nextType FROM SampleData AS
Level1
It would be possible to extract also the second next event type in sequence
in similar fashion, but I am afraid that the complexity of the overall query
would be unreasonable and performance would be slow. Plus, if I want also,
for example, count days when the "event type A was in force", I need to add
nex subquery for the evtTime:
SELECT evtType, evtTime, (SELECT TOP 1 evtType FROM SampleData WHERE
evtTime>Level1.evtTime ORDER BY logDate) AS nextType, (SELECT TOP 1 evtTime
FROM SampleData WHERE evtTime>Level1.evtTime ORDER BY logDate) AS nextTime
FROM SampleData AS Level1
Only then I could find the sequences that, for example, have "event A in
force at least 10 days, then followed by event B, and then by C".
How this standard need could be solved so that I don't need to create large
temporary tables, populated with data by raw code?
PavilsOn Tue, 15 Nov 2005 16:30:55 +0200, Pavils Jurjans wrote:
(snip)
>The setup:
>Table, that contains log of events. Typical fields would be: 1) uniqueID; 2
)
>evtTime; 3)evtType. Then there are some additional fields that carry
>additional logged event data.
>Sample data:
>ID evtTime evtType
>1 10-11-2005 A
>2 13-11-2005 B
>3 20-11-2005 C
>4 22-11-2005 B
>Now, if I want to perform analysis on this data, typical need would be to
>analyze sequence of event types. In the sample data above, the sequence is
>A-B-C-B (ordering by date-time). I find it very complex to create a query
>that would let me find sequence of events by given pattern.
(snip)
>Only then I could find the sequences that, for example, have "event A in
>force at least 10 days, then followed by event B, and then by C".
Hi Pavils,
If this is your typical reporting need, then I recommend that you
transform the data from events to durations (or states, or whatever term
is appropriate in your case).
CREATE TABLE Durations
(StartDate smalldatetime NOT NULL,
EndDate smalldatetime DEFAULT NULL,
EventType char(1) NOT NULL,
PRIMARY KEY (StartDate),
UNIQUE (EndDate),
FOREIGN KEY (EndDate) REFERENCES Durations (StartDate),
CHECK(EndDate > StartDate)
)
In most cases, I'd make Durations a view instead of a table. This might
well be an exception to that rule. The view could be slow, since it
would require a self-join. If your queries would then involve joining
the view to itself, you'd effectively be joining the base table to an
exponential increasing number of copies of itself.
Durations as indexed view would be ideal, but I don't think it's
possible to write a query that computes the Durations correctly without
breaking the limitations for indexed views.
That leaves you with two options for how to populate the table:
1. Remove the Events table; change the stored procedure or front-end
code that currently popultes the Events table to make the correct
changes to the Durations table, or
2. Keep the Events table; leave front-end and stored procedures
unchanges, but add insert, update and delete triggers on the Events
table to copy over all changes to the Durations table.
Once you have the Durations table, the query you give in your final
example would be:
SELECT d1.StartDate, d2,StartDate, d3.StartDate, d3.EndDate
FROM Durations AS d1
INNER JOIN Durations AS d2
ON d2.StartDate = d1.EndDate
INNER JOIN Durations AS d3
ON d3.StartDate = d2.EndDate
WHERE d1.EndDate >= DATEADD(day, 10, d1.StartDate)
AND d1.EventType = 'A'
AND d2.EventType = 'B'
AND d3.EventType = 'C'
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)|||Thanks, Hugo for you input.
I studied it carefully. To extend my proposed sample to my real case, I must
add that the events are registered for large number of subjects, so the
events table also has reference to subject ID. Your query then could look
like
SELECT d1.StartDate, d2,StartDate, d3.StartDate, d3.EndDate
FROM Durations AS d1
INNER JOIN Durations AS d2
ON d2.StartDate = d1.EndDate AND d2.subID=d1.subID
INNER JOIN Durations AS d3
ON d3.StartDate = d2.EndDate AND d3.subID=d2.subID
WHERE d1.EndDate >= DATEADD(day, 10, d1.StartDate)
AND d1.EventType = 'A'
AND d2.EventType = 'B'
AND d3.EventType = 'C'
Perhaps that just adds another load to the view. Building a temp table could
be solution for some standard reports, but the data are updated every minute
and some reports are asking another criteria on the series, so perhaps view
or dynamically generated query, while potentially slow, could be the most
realistic alternative.
However, I am now a bit worried that it seems that theoretically there could
be cases when the dates of succeeding events could be identical, so
apparently I need another order field. It seems that the best (could easily
be used for self-joins) would be carrying reference to previous event ID,
but it involves difficulty how to order the events in the right order by
simple SQL server means. The only option left then is a clumsy "orderNumber"
that will be a hassle to populate and it will be even hasslier to insert a
new event in existing series. Lots of code will be required to keep that
structure intact with no errors in numeration. Perhaps I need to weigh how
much I need a query that would give ordered list of events vs doing this
ordering by code.
Another concern is making self-joins vs using subqueries. I could get the
fields from succeeding event quite easily, and also getting fields from next
succeeding event seems no problem. The question is only about how effective
is this. Perhaps only tests will show this. I am sort of dismayed that so
few tools are built in MSSQL to analyse record series - that is typical need
in complex analysis and caching all the analysis sample in application
memory for proprietary code analysis sometimes is very ineffective.
Regards,
Pavils Jurjans|||On Mon, 21 Nov 2005 19:37:59 +0200, Pavils Jurjans wrote:

>Thanks, Hugo for you input.
>I studied it carefully. To extend my proposed sample to my real case, I mus
t
>add that the events are registered for large number of subjects, so the
>events table also has reference to subject ID. Your query then could look
>like
>SELECT d1.StartDate, d2,StartDate, d3.StartDate, d3.EndDate
>FROM Durations AS d1
>INNER JOIN Durations AS d2
> ON d2.StartDate = d1.EndDate AND d2.subID=d1.subID
>INNER JOIN Durations AS d3
> ON d3.StartDate = d2.EndDate AND d3.subID=d2.subID
>WHERE d1.EndDate >= DATEADD(day, 10, d1.StartDate)
>AND d1.EventType = 'A'
>AND d2.EventType = 'B'
>AND d3.EventType = 'C'
Hi Pavils,
Yes, that's how you would change it. I'd probably also add the subject
to the SELECT clause.

>Perhaps that just adds another load to the view.
Depends. With the right indexes it *might* even speed things up.

> Building a temp table could
>be solution for some standard reports, but the data are updated every minut
e
>and some reports are asking another criteria on the series, so perhaps view
>or dynamically generated query, while potentially slow, could be the most
>realistic alternative.
Yes.
But if that's to slow, then consider permanently storing both the
beginning and the end of the events. There are various ways to do this:
1. Change it in whatever currently produces the data.
2. Use a stored procedure to handle insertions; add code to that SP to
fill the end date of the "previous" event.
3. Create a trigger on the table that will populate end dates as
appropriate.

>However, I am now a bit worried that it seems that theoretically there coul
d
>be cases when the dates of succeeding events could be identical, so
>apparently I need another order field.
Or you could store the time in addition to the date. The smalldatetime
datatype stores date + time with a precision of minutes; the datetime
datatype even has a precision of 3/1000th of a second.

>It seems that the best (could easily
>be used for self-joins) would be carrying reference to previous event ID,
If you choose that, then remove the the end date. I included that to
serve as a reference to the previous event. If you use a different
reference, there's no need to keep the end date.
However, don't ever rely on a system-generated identifier to serve as
the only key for a table. If the "real" key is clumsy, you can add a
system-generated identifier to serve as a "surrogate" key.

>but it involves difficulty how to order the events in the right order by
>simple SQL server means.
Are the events inserted in chronological order? In that case, it's quite
easy to have the system generate an ID value that will keep increasing.
That would make ordering trivial.
(snip)
> I am sort of dismayed that so
>few tools are built in MSSQL to analyse record series - that is typical nee
d
>in complex analysis and caching all the analysis sample in application
>memory for proprietary code analysis sometimes is very ineffective.
You might want to investigate Analysis Services. I must admit that the
only thing I know about it is it's name (in some circles, that's enough
to qualify as "expert"...). But who knows - maye it's just the tool for
your needs!
Best, Hugo
--
(Remove _NO_ and _SPAM_ to get my e-mail address)

how to query the using recursive?

Hello,

I have the following tables:

Article(articleID,CategoryID,ArticleTitle)Categories(categoryID,ParentID,CategoryTitle)

I am trying to retrieve themain category ID for a specific article ID.

For example lets say I have this data:

Article:

1, 10 , "some title"2,10,"some title"3,11,"some title"

Categories:

1, NULL , "some title"2, 1, "some title"10, 2, "some title"11, 10 , "some title"

In this example I want to know who is the main category of article 3.

The query should return the answer:1

Thats because:

    The article ID 3 is inside category 11.Parent for category 11 is 10.Parent for category 10 is 2. Parent for category 2 is 1and Parent for category 1 is NULL, which means category 1 has no parents and it is the main category.Query will return article id, category id, main_category_id, ArticleTitle, CategoryTitle (some union between 2 tables)

Do you have any suggestions for such query?

Thanks all.

check this post for a non-recursive technique for storing and traversing hierarchical data in sql:http://forums.asp.net/p/1084407/1611195.aspx#1611195

the technique is more fully described here:http://www.developerfusion.co.uk/show/4633/2/