Showing posts with label int. Show all posts
Showing posts with label int. Show all posts

Wednesday, March 21, 2012

Interesting Problem

Imagine a table in Microsoft Sql Server 2000 named Pictures with three
fields. A primary key ID(int), a Name(nvarchar) and a Picture(image)
field.

Lets put some records into the table. The first two fields as expected
would take an int and a string. The third field is of type image.
Instead of putting a bitmap in this field lets place an xml document
that has been streamed into a byte array. The xml document would
describe the Picture using say lines. If the picture was that of a
square we would have four lines in the xml document. You could think of
the xml document as something similar to vector graphics but the
details are not relevant. The important fact is that the contents of
the image field is NOT a bitmap but a binary stream of an xml document.

Now imagine we have a reporting tool like Crystal Reports that can be
used to report on this database table. Imagine we create a report by
using the three fields mentioned above. As far as Crystal is concerned
the first field is an int, the second a string and the third an image.
If our table had ten records and we preview the report we like to see
10 entries each consisting of an ID, Name and a Picture.

This can only happen if the image field contains a Bitmap, but as
mentioned above the field contains an xml document.

Now my question...

Can we write something in SQL Server 2000 (not 2005) to sit between the
table and Crystal Reports so to convert the XML document to a bitmap.
The restriction is that we cannot use anything but sql server itself.
The client in the above case has been Crystal Reports but it could be
anything.

I know SQL Server 2005 supports C# with access to the .NET framework
within the database. Unfortunately, I am not using SQL server 2005.

Some people have suggested the use of User Defined Functions and TSQL.
I like to know from the more experienced SQL Server people if what I am
trying to achieve is possible. Maybe it has not been done but is it
possible?

Any suggestions would be greatly appreciated...

Many RegardsTranslating an XML byte stream to a bitmap dynamically sounds like
something which is probably beyond pure TSQL. Even if it is possible
somehow, I suspect that the code would be complex and slow - TSQL isn't
really a general purpose language, and its support for manipulating
binary data is limited.

Having said that, there are a couple of ways you might be able to
approach this - extended stored procedures, and COM support. An
extended proc is an external DLL which can be called from TSQL, rather
like a more basic version of the .NET support in 2005. Alternatively,
you can use the sp_OA% procs to instantiate COM objects, so if your
logic can be written as a COM object, then you can use it from TSQL.
Check Books Online for more details on both these options.

Although I don't have much experience with extended procs, the COM
support is probably not a good solution, because of security and
performance issues. The best approach is almost certainly to do this in
client code, rather than the database - perhaps you can look at
embedding something in Crystal, instead of in the database?

Simon|||Perhaps it would be possible to write an extended stored proc and call
that from a function. That of course assumes that Crystal Reports
provides some method of rendering a bitmap returned from a query.

I can't think of a good reason to do this in SQL Server. It's obviously
better suited for the client or middle-tier.

--
David Portas
SQL Server MVP
--|||Hi Simon

Thanks for your suggestions. I think I like the Extented Stored
Procedure(xp) route more and I can certainly create a dll in C++ to
convert an XML document containing the description of the square to a
bitmap of the square (say 100x100 pixels by default). Once I add this
xp to sql server I need to attach it to my xml field in some way so
that when Crystal requests the content of the image field in the
Pictures table instead of it returning the binary array representing
the xml document it returns the on-the-fly generated bitmap.

Is there a way I could intervene in what sql sends to crystal in
respect to the Picture field using the xp? Do I need to use triggers?
Basically I am trying to fool crystal that the Picture field contains a
Bitmap. This needs to happen when the Crystal does a select I
suppose...

Probably its worth mentioning why I am trying to do this at all...

We have a drawing package that allows users to draw shapes. We can
store these as an XML document and restore them. Whats more we have a
control that can render these documents directly. Thus in order to
preview the XML document all that is needed is the control which is
self contained. The control also scales the preview of the shape.

We therefore can store the XML for this document in the database and
not worry about also storing a preview bitmap of the shape in the same
row. This means there is less danger of the preview bitmap field going
out of syn with the xml document and also avoids data redundancy. Whats
more preview bitmaps are fixed in size and dont scale well. This
approach solves all these problems.

However, as databases are often reported on and that Crystal Reports is
a leading reporting tool I like our database to work well with Crystal
when wanting to create reports that need the picture field (that
contains the xml document).

This is exactly why I need to the conversion at SQL as our customers
could do reporting directly from the database using Crystal Reports.
Embedding within Crystal is also not an option as the reporting tool
could change.

Many Thanks in Advance|||Thanks Dave

Please look at my reply to Simon for reasons why I am trying to do
this...
Problem now is how to fool Crystal to get the Bitmap from the xp as
oppose the xml from the field...

Any comments would be appreciated...

Regards..|||> Is there a way I could intervene in what sql sends to crystal in
> respect to the Picture field using the xp? Do I need to use triggers?

You could call your XP from a user-defined function and return the
result as a binary column. Put the UDF in a view and query the view
from Crystal. There are no triggers on SELECT so this is the only
method I can think of.

--
David Portas
SQL Server MVP
--

Interesting interview question...........

Hi all,
One small question.
I am having a table named maverick and having 2 fields, tkno(int) and
name(varchar(20)).
The table is filled with 10 entries.
tkno name
1 Zamsheer
2 Thushara
3 Kori
4 Attu
................etc
I want to retrieve the 6th record(any specified) from the table
........that's it.......
But the interesting factor is
U should not mention any field name in the query
don't use a cursor......
no stored procedure, no function... nothing
One single query should give the result......
Regards,
JaisonThe sixth row according to what order? tkno?
select top 1 *
from (
select top 6 *
from <table>
order by tkno asc
) TopSix
order by TopSix.tkno desc
Close enough?
ML
http://milambda.blogspot.com/|||If you really 'cant' use column names, you could use ordinal number of the
column. So, instead of order by tkno you can use order by 1
Offcourse, dont do that in a 'normal' environment.
MC
"ML" <ML@.discussions.microsoft.com> wrote in message
news:622ED66D-6581-4744-9D3B-C9C53BED400B@.microsoft.com...
> The sixth row according to what order? tkno?
> select top 1 *
> from (
> select top 6 *
> from <table>
> order by tkno asc
> ) TopSix
> order by TopSix.tkno desc
> Close enough?
>
> ML
> --
> http://milambda.blogspot.com/|||USe pubs
--2000
DECLARE @.param int
SET @.param = 3
Select j.* FROM Jobs J
WHERE @.param>=(SELECT Count(*) FROM Jobs B
WHERE B.Job_Id <= J.Job_Id )
ORDER By Job_Id
--2005
SELECT * FROM
(SELECT ROW_NUMBER() OVER (ORDER BY ContactID DESC) As Rno, * FROM
Person.Contact) T
WHERE T.Rno = 5
Regards
Roji. P. Thomas
http://toponewithties.blogspot.com
"Jaison Jose" <JaisonJose@.discussions.microsoft.com> wrote in message
news:FA6190B9-9A9C-4308-BD9E-91B91F52FD08@.microsoft.com...
> Hi all,
> One small question.
> I am having a table named maverick and having 2 fields, tkno(int) and
> name(varchar(20)).
> The table is filled with 10 entries.
> tkno name
> 1 Zamsheer
> 2 Thushara
> 3 Kori
> 4 Attu
> ................etc
> I want to retrieve the 6th record(any specified) from the table
> ........that's it.......
>
> But the interesting factor is
> U should not mention any field name in the query
> don't use a cursor......
> no stored procedure, no function... nothing
> One single query should give the result......
> Regards,
> Jaison
>|||Thanks...........
"ML" wrote:

> The sixth row according to what order? tkno?
> select top 1 *
> from (
> select top 6 *
> from <table>
> order by tkno asc
> ) TopSix
> order by TopSix.tkno desc
> Close enough?
>
> ML
> --
> http://milambda.blogspot.com/|||The ordinal position, yes. That would fully comply with the requirement. And
I agree - to vulnerable to be of any real use.
ML
http://milambda.blogspot.com/

Monday, March 19, 2012

Interesting Behavior of Alter Table

We've found something that seems odd for which we'd like an explanation.
We have a table defined as follows:
create table tester
(col1 int, col2 char(2), col3 int)
Our script executes something like this:
begin
alter table tester add id int not null constraint df_test default 0
alter table tester add id2 int identity
update tester
set id = id2
alter table tester drop column id2
end
The first time we run the script above, the following error occurs:
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'id'.
Server: Msg 207, Level 16, State 1, Line 1
Invalid column name 'id2'.
All subsequent times in the same database, the code works as expected. Even
after the table is dropped and recreated, or we just drop the columns we
added and rerun it. Even after restarting query analyzer.
If I change the table name used to "tester2" and rerun the script, the error
is back. If I then drop that table and rerun the script, the errors
disappear.
Any ideas? We have worked around it by putting the schema altering
statements in their own batch, which is easy enough. But the behavior sure
seems erratic.
Thanks!
Steven Bras
Tessitura Network, Inc.have you tried it with GO in between the various sub steps , so you can send
each one as a transaction.?
--
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
___________________________________
"StevenBr" <sbras@.community.nospam> wrote in message
news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> We've found something that seems odd for which we'd like an explanation.
> We have a table defined as follows:
> create table tester
> (col1 int, col2 char(2), col3 int)
> Our script executes something like this:
> begin
> alter table tester add id int not null constraint df_test default 0
> alter table tester add id2 int identity
> update tester
> set id = id2
> alter table tester drop column id2
> end
> The first time we run the script above, the following error occurs:
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id'.
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id2'.
> All subsequent times in the same database, the code works as expected.
Even
> after the table is dropped and recreated, or we just drop the columns we
> added and rerun it. Even after restarting query analyzer.
> If I change the table name used to "tester2" and rerun the script, the
error
> is back. If I then drop that table and rerun the script, the errors
> disappear.
> Any ideas? We have worked around it by putting the schema altering
> statements in their own batch, which is easy enough. But the behavior sure
> seems erratic.
> Thanks!
> Steven Bras
> Tessitura Network, Inc.|||When I run this in SQL server management studio I get the errors
everytime. After the first unsuccessful run has the table schema been
altered?|||I forgot to mention, as well as using GO , test it without the begin / end
--
Jack Vamvas
___________________________________
Receive free SQL tips - www.ciquery.com/sqlserver.htm
___________________________________
"StevenBr" <sbras@.community.nospam> wrote in message
news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> We've found something that seems odd for which we'd like an explanation.
> We have a table defined as follows:
> create table tester
> (col1 int, col2 char(2), col3 int)
> Our script executes something like this:
> begin
> alter table tester add id int not null constraint df_test default 0
> alter table tester add id2 int identity
> update tester
> set id = id2
> alter table tester drop column id2
> end
> The first time we run the script above, the following error occurs:
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id'.
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id2'.
> All subsequent times in the same database, the code works as expected.
Even
> after the table is dropped and recreated, or we just drop the columns we
> added and rerun it. Even after restarting query analyzer.
> If I change the table name used to "tester2" and rerun the script, the
error
> is back. If I then drop that table and rerun the script, the errors
> disappear.
> Any ideas? We have worked around it by putting the schema altering
> statements in their own batch, which is easy enough. But the behavior sure
> seems erratic.
> Thanks!
> Steven Bras
> Tessitura Network, Inc.|||Yes, that's essentially our workaround. Thanks.
--
Steven Bras
Tessitura Network, Inc.
"Jack Vamvas" wrote:

> have you tried it with GO in between the various sub steps , so you can se
nd
> each one as a transaction.?
>
> --
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
> ___________________________________
>
> "StevenBr" <sbras@.community.nospam> wrote in message
> news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> Even
> error
>
>|||Hmm. We're using Query Analyzer against SQL 2k. And no, the alterations have
not been made.
This is very strange; to go back and check what you asked, I reran the same
script with a table name I've used before, and it worked without error.
Tried the same thing on a brand new table, and got the errors again.
Thanks!
--
Steven Bras
Tessitura Network, Inc.
"Will" wrote:

> When I run this in SQL server management studio I get the errors
> everytime. After the first unsuccessful run has the table schema been
> altered?
>|||Same problem, and we need those because it's in an IF block in the productio
n
script. I can leave that out and still repro the problem.
--
Steven Bras
Tessitura Network, Inc.
"Jack Vamvas" wrote:

> I forgot to mention, as well as using GO , test it without the begin / end
> --
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
> ___________________________________
>
> "StevenBr" <sbras@.community.nospam> wrote in message
> news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> Even
> error
>
>|||This is what I have learned to do as well. I think that semicolons
following each statement also works, although I have not tested it in every
situation.
"Jack Vamvas" <delete_this_bit_jack@.ciquery.com_delete> wrote in message
news:9f2dneo6CqO3jv_ZRVnyiw@.bt.com...
> have you tried it with GO in between the various sub steps , so you can
send
> each one as a transaction.?
>
> --
> --
> Jack Vamvas
> ___________________________________
> Receive free SQL tips - www.ciquery.com/sqlserver.htm
> ___________________________________
>
> "StevenBr" <sbras@.community.nospam> wrote in message
> news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> Even
> error
sure
>|||This is because how SQL Server parses the queries. When the parses sees the
text, the ALTER TABLE
haven't been executed yet. So this is why the parses throws the errors (as i
t tries to validate that
the columns exists).
Execute them in separate batches. Although the big question is why you creat
e columns at run-time...
Tibor Karaszi, SQL Server MVP
http://www.karaszi.com/sqlserver/default.asp
http://www.solidqualitylearning.com/
"StevenBr" <sbras@.community.nospam> wrote in message
news:F99CBBE7-4AF4-449A-B5EF-372115F37509@.microsoft.com...
> We've found something that seems odd for which we'd like an explanation.
> We have a table defined as follows:
> create table tester
> (col1 int, col2 char(2), col3 int)
> Our script executes something like this:
> begin
> alter table tester add id int not null constraint df_test default 0
> alter table tester add id2 int identity
> update tester
> set id = id2
> alter table tester drop column id2
> end
> The first time we run the script above, the following error occurs:
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id'.
> Server: Msg 207, Level 16, State 1, Line 1
> Invalid column name 'id2'.
> All subsequent times in the same database, the code works as expected. Eve
n
> after the table is dropped and recreated, or we just drop the columns we
> added and rerun it. Even after restarting query analyzer.
> If I change the table name used to "tester2" and rerun the script, the err
or
> is back. If I then drop that table and rerun the script, the errors
> disappear.
> Any ideas? We have worked around it by putting the schema altering
> statements in their own batch, which is easy enough. But the behavior sure
> seems erratic.
> Thanks!
> Steven Bras
> Tessitura Network, Inc.|||I don't see it working when doing your steps:
create table tester
(col1 int, col2 char(2), col3 int)
go
--run the statement and get the error
begin
alter table tester add id int not null constraint df_test
default 0
alter table tester add id2 int identity
update tester
set id = id2
alter table tester drop column id2
end
go
--Step 1 run each statement manually
alter table tester add id int not null constraint df_test default 0
go
alter table tester add id2 int identity
go
update tester
set id = id2
go
alter table tester drop column id2
go
--Step 2 manually remove the default and ID
alter table tester drop constraint df_test
alter table tester drop column id
go
--Step 3 - rerun the code and the error appears for me
begin
alter table tester add id int not null constraint df_test
default 0
alter table tester add id2 int identity
update tester
set id = id2
alter table tester drop column id2
end
go
drop table tester
go