Showing posts with label convert. Show all posts
Showing posts with label convert. Show all posts

Friday, March 23, 2012

Question about Nested Set Model - Hierarchical Data in SQL Server 2000

Hi,

I would like to convert my adjancey set model to a nested set model with a left and right.

I understood this part.

Root Node is always with a a Left = 1, right = 2*(Select Count(*) from Table)

How do the leaf node get the left and right numbers.

I was reading an article of Joe Celko regarding the Nested Sets in SQL and I could not understand how do the employee below Albert(who is top level manager) get their node numbers.

Adjanceny Model
--
Emp Boss

Albert Null
Bert Albert
Chuck Albert
Donna Chuck
Eddie Chuck
Fred Chuck

Nested Set Model
-
Emp Left Right
Albert 1 12
Bert 2 3
Chuck 4 11
Donna 5 6
Eddie 7 8
Fred 9 10

If anyone could kindly explain this to me, then I can try converting my table
to a nested set model to prevent recursion.

Thanks.

set nocount on
create table #chain
( seq integer not null primary key,
emp char (20),
chain varchar (20),
lft integer,
rgt integer
)

declare @.begDt datetime set @.begDt = getdate();

with organization (depth, emp, boss, chain)
as
( select 1 as depth,
emp,
boss,
cast(rtrim(emp) as varchar (400))
from tree
where boss is null
union all
select a.depth + 1 as depth,
b.emp,
b.boss,
cast (a.chain + ', ' + rtrim(b.emp) as varchar(400))
from organization a
inner join tree b
on a.emp = b.boss
)
insert into #chain (seq, emp, chain)
select row_number () over (order by chain) as seq,
emp,
chain
from organization

update #chain
set lft = 1 + 2 * ( select count(*) from #chain b
where b.seq < a.seq
)
- ( select count(*) from #chain c
where c.seq < a.seq
and ( c.chain = rtrim(c.emp) or
a.chain like '%, ' + rtrim(c.emp) + '%'
)
)
from #chain a

update #chain
set rgt = lft + 1
+ 2 * ( select count(*) from #chain b
where b.seq <> a.seq
and ( b.chain like '%, ' + rtrim(a.emp) + '%' or
a.seq = 1
) )
from #chain a


print ' '
select datediff (ms, @.begDt, getdate()) as [Elapsed Time]

select emp, lft, rgt from #chain

-- - Output: -

-- emp lft rgt
-- -- -- --
-- Albert 1 12
-- Bert 2 3
-- Chuck 4 11
-- Donna 5 6
-- Eddie 7 8
-- Fred 9 10


-- Elapsed Time
--
-- 46


go

drop table #chain
go

|||

Thanks for the post. Can you please let me know the structure of your table Organization so that I can re-create it.

When I run the code at my SQL Server, I see the 2 errors:

Incorrect syntax near the keyword 'with'.

at the line:

with organization (depth, emp, boss, chain)

So I guess that that I do not have the Organization table and hence the error.

2nd error:

'row_number' is not a recognized function name.

at the line below:

select row_number () over (order by chain) as seq,

THanks.

Monday, March 12, 2012

Question about encrypting a current App.

When we convert an existing app to start encryption and decrypting data as needed, I am required to make changes to atleast 50 stored procs. Within each of these SPs I do:

-- OPEN SYMMETRIC KEY

-- do the ecrypt or decrypt operation

-- CLOSE SYMMETRIC KEY

Is it possible to not use these steps in each of my SP's? i.e, can we generalize the open and close key calls somewhere else? Any tips are greatly appreciated. TIA.

You can open the symmetric keys as part of connecting to the database and close them as you log out. Anything executing in that context will have access to the keys and be able to encrypt and decrypt with them.

If your application opens/uses multiple connections on behalf of a user and is supposed to encrypt/decrypt on all of them, you would need to open the keys when you open each connection and close them when you cease using a connection. If connections are not pooled, you don't need to close the keys if you're going to kill the connection anyway.

Thanks
Laurentiu

Saturday, February 25, 2012

Question

Why does this view work with a compatibility level of 65 but not when 80 is
selected?
SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' - '
+ tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
tblLibraryBookCopies.Copy) + CASE WHEN
tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
tblLibraryBookCopies.Volume ELSE NULL
END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
tblLibraryBookStatusTypes.BookStatus) ELSE NULL END +
')'
FROM tblLibraryBooks INNER JOIN
tblLibraryBookCopies ON tblLibraryBooks.BookID =
tblLibraryBookCopies.BookID LEFT OUTER JOIN
tblLibraryBookStatusTypes ON
tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
Thanks,
Tony
What is the error you are getting?
|||Your logic depends on certain connection settings (whose defaults have
apparently changed between versions) - a string + null = NULL. Therefore,
the answer is to concatenate an empty string, not NULL.
"Tony Schlak" <tony@.optics.arizona.edu> wrote in message
news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> Why does this view work with a compatibility level of 65 but not when 80
is
> selected?
> SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' -
'
> + tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
> tblLibraryBookCopies.Copy) + CASE WHEN
> tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
> tblLibraryBookCopies.Volume ELSE NULL
> END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
> IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
> tblLibraryBookStatusTypes.BookStatus) ELSE NULL END
+
> ')'
> FROM tblLibraryBooks INNER JOIN
> tblLibraryBookCopies ON tblLibraryBooks.BookID =
> tblLibraryBookCopies.BookID LEFT OUTER JOIN
> tblLibraryBookStatusTypes ON
> tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
>
> Thanks,
> Tony
>
|||Unfortunately, this has already been answered in .programming. Please do
not independently post the same message to multiple newsgroups. You only
increase the work for you and everyone who donates their time to help you.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:uzXZFv6hFHA.1052@.TK2MSFTNGP10.phx.gbl...[vbcol=seagreen]
> Your logic depends on certain connection settings (whose defaults have
> apparently changed between versions) - a string + null = NULL. Therefore,
> the answer is to concatenate an empty string, not NULL.
> "Tony Schlak" <tony@.optics.arizona.edu> wrote in message
> news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> is
' -[vbcol=seagreen]
> '
tblLibraryBookStatusTypes.BookStatus[vbcol=seagreen]
END[vbcol=seagreen]
> +
tblLibraryBookStatusTypes.BookStatusID
>

Monday, February 20, 2012

Question

Why does this view work with a compatibility level of 65 but not when 80 is
selected?
SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' - '
+ tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
tblLibraryBookCopies.Copy) + CASE WHEN
tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
tblLibraryBookCopies.Volume ELSE NULL
END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
tblLibraryBookStatusTypes.BookStatus) ELSE NULL END +
')'
FROM tblLibraryBooks INNER JOIN
tblLibraryBookCopies ON tblLibraryBooks.BookID = tblLibraryBookCopies.BookID LEFT OUTER JOIN
tblLibraryBookStatusTypes ON
tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
Thanks,
TonyWhat is the error you are getting?|||Your logic depends on certain connection settings (whose defaults have
apparently changed between versions) - a string + null = NULL. Therefore,
the answer is to concatenate an empty string, not NULL.
"Tony Schlak" <tony@.optics.arizona.edu> wrote in message
news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> Why does this view work with a compatibility level of 65 but not when 80
is
> selected?
> SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' -
'
> + tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
> tblLibraryBookCopies.Copy) + CASE WHEN
> tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
> tblLibraryBookCopies.Volume ELSE NULL
> END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
> IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
> tblLibraryBookStatusTypes.BookStatus) ELSE NULL END
+
> ')'
> FROM tblLibraryBooks INNER JOIN
> tblLibraryBookCopies ON tblLibraryBooks.BookID => tblLibraryBookCopies.BookID LEFT OUTER JOIN
> tblLibraryBookStatusTypes ON
> tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
>
> Thanks,
> Tony
>|||Unfortunately, this has already been answered in .programming. Please do
not independently post the same message to multiple newsgroups. You only
increase the work for you and everyone who donates their time to help you.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:uzXZFv6hFHA.1052@.TK2MSFTNGP10.phx.gbl...
> Your logic depends on certain connection settings (whose defaults have
> apparently changed between versions) - a string + null = NULL. Therefore,
> the answer is to concatenate an empty string, not NULL.
> "Tony Schlak" <tony@.optics.arizona.edu> wrote in message
> news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> > Why does this view work with a compatibility level of 65 but not when 80
> is
> > selected?
> >
> > SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) +
' -
> '
> > + tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
> > tblLibraryBookCopies.Copy) + CASE WHEN
> > tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
> > tblLibraryBookCopies.Volume ELSE NULL
> > END + CASE WHEN
tblLibraryBookStatusTypes.BookStatus
> > IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
> > tblLibraryBookStatusTypes.BookStatus) ELSE NULL
END
> +
> > ')'
> > FROM tblLibraryBooks INNER JOIN
> > tblLibraryBookCopies ON tblLibraryBooks.BookID => > tblLibraryBookCopies.BookID LEFT OUTER JOIN
> > tblLibraryBookStatusTypes ON
> > tblLibraryBookCopies.BookStatusID =tblLibraryBookStatusTypes.BookStatusID
> >
> >
> > Thanks,
> > Tony
> >
> >
>

Question

Why does this view work with a compatibility level of 65 but not when 80 is
selected?
SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' - '
+ tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
tblLibraryBookCopies.Copy) + CASE WHEN
tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
tblLibraryBookCopies.Volume ELSE NULL
END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
tblLibraryBookStatusTypes.BookStatus) ELSE NULL END +
')'
FROM tblLibraryBooks INNER JOIN
tblLibraryBookCopies ON tblLibraryBooks.BookID =
tblLibraryBookCopies.BookID LEFT OUTER JOIN
tblLibraryBookStatusTypes ON
tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
Thanks,
TonyWhat is the error you are getting?|||Your logic depends on certain connection settings (whose defaults have
apparently changed between versions) - a string + null = NULL. Therefore,
the answer is to concatenate an empty string, not NULL.
"Tony Schlak" <tony@.optics.arizona.edu> wrote in message
news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> Why does this view work with a compatibility level of 65 but not when 80
is
> selected?
> SELECT AccessionNumber, Info = CONVERT(varchar, AccessionNumber) + ' -
'
> + tblLibraryBooks.Title + ' (Copy ' + CONVERT(varchar,
> tblLibraryBookCopies.Copy) + CASE WHEN
> tblLibraryBookCopies.Volume IS NOT NULL THEN ', Vol. ' +
> tblLibraryBookCopies.Volume ELSE NULL
> END + CASE WHEN tblLibraryBookStatusTypes.BookStatus
> IS NOT NULL THEN ', Status: ' + CONVERT(varchar,
> tblLibraryBookStatusTypes.BookStatus) ELSE NULL END
+
> ')'
> FROM tblLibraryBooks INNER JOIN
> tblLibraryBookCopies ON tblLibraryBooks.BookID =
> tblLibraryBookCopies.BookID LEFT OUTER JOIN
> tblLibraryBookStatusTypes ON
> tblLibraryBookCopies.BookStatusID = tblLibraryBookStatusTypes.BookStatusID
>
> Thanks,
> Tony
>|||Unfortunately, this has already been answered in .programming. Please do
not independently post the same message to multiple newsgroups. You only
increase the work for you and everyone who donates their time to help you.
"Scott Morris" <bogus@.bogus.com> wrote in message
news:uzXZFv6hFHA.1052@.TK2MSFTNGP10.phx.gbl...
> Your logic depends on certain connection settings (whose defaults have
> apparently changed between versions) - a string + null = NULL. Therefore,
> the answer is to concatenate an empty string, not NULL.
> "Tony Schlak" <tony@.optics.arizona.edu> wrote in message
> news:%23FC5uPzhFHA.2156@.TK2MSFTNGP14.phx.gbl...
> is
' -[vbcol=seagreen]
> '
tblLibraryBookStatusTypes.BookStatus[vbcol=seagreen]
END[vbcol=seagreen]
> +
tblLibraryBookStatusTypes.BookStatusID[vbcol=seagreen]
>