Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Sunday, March 11, 2012

Assigning extra information to all database table fields

I'm an experienced desktop app programmer, but fairly new to database programming. I'm working with C#/SQL right now. I understand the concept of a lookup table, as in storing a two-character state (such as CA) in a field in one table, and then being able to get the full state name (such as California) from a second table:

Table: Address
AddressID int <PK>
StateCode char(2) <FK>
...

Table: State
StateCode char(2) <PK>
StateName varchar(25)

My question: is there some similar way to be able to provide just about every field of every table with a means of holding common, extra information? For instance, let's say I wanted to store, say, an extended name and some special code with every field in three tables. As in this simplistic example:

Table: A
Field1 int
Field2 varchar(20)

Table: B
Field1 bit
Field2 int

Table: C
Field1 int

Table: ExtraInfo
LongName varchar(50)
TypeCode int

If I wanted to be able to have the information in the ExtraInfo table associated with each field in Table A, Table B and Table C, how would I do that?

Thanks!

I'm not sure that I understand exactly what you are trying to do.

Might you want a table "ExtraInfo" with columns

Table_Name varchar(50),

Column_Name varchar(50),

Extra_Info varchar(50),

TypeCode int

Maybe Extra_Info could be something like a column description?

Is that the right idea?

Dan

|||I'm not totally clear about the concept. Perhaps looking in Books Online for 'Extended Properties' would prove to be helpful.|||

Just to add my 3 cents worth to the interrogation, are you wanting to store metadata? Or are you trying to store information for use by other users? That would make a big difference.

Also, if you are trying to be generic, like having a base class, it is seldom a good idea, even if it seems like a good idea at this point. SQL is centered around implementing a specific design, not genericness (genericism, genericisticy?)

|||

Louis -

Yes, metadata. No user-entered information.

And no, I'm not looking to be generic.

The information would be very specific. As in, say, being able to provide every field of any (or all) tables with a string title (that differs from the field's column name). Or any other bit of information that might be useful for all fields. For the sake of a solution, ignore the exact type of information (metadata) that's to be tracked. It could be anything. Let's say I want to assign one of three colors (red, yellow or green) to every field of every table. The data can be anything. I just want to know how I could store some common, non-generic data to every field. To, in a sense, make every field of any table a structure (to put it in app programming terms). It seems like there should be some standard way of doing this. It's probably my desktop app programming background limiting my explanation.

Thanks!

|||

DanR1 -

I think this might be a solution. I'll look into it. If the one table had TableName and ColumnName fields as you suggest, then it could have any other fields for the metadata, and the TableName and ColumnName fields provide a way back to each particular field of any table. That might work. I'm not sure if that's a "legitimate" solution as far as database design goes (as mentioned, I'm not a big DB programmer), but it is certainly on the right track for what I'm trying to do.

Thanks

|||

Arnie -

I've googled, searched Safari Online, everything. Nothing.

From a programming perspective (which I think in terms of), this is easy. For instance, in OOP there could be a base class Animal that keeps track of an animal's color. Then, any classes of particular animals could inherit the Animal class (and thus each animal could hold color information) as well as keep track of information particular to that animal:

class Animal
{
int color;
}

class Dog : Animal
{
bool chasesCars;
}

class Pig : Animal
{
bool likesMud;
}

The above, in DB terms, would be a table Dog with one bit field and a table Pig that also had a bit field. Besides that one piece of information though, each of these fields could also keep track of a color. That's the kind of "extra" information I'm trying to tie to each field.

Thanks

|||

It sounds like you just need to add additional columns to the tables. Such as [DescriptiveTitle], [DefaultValue], [MyMetaData], etc.

It is very common for additional metadata columns to be added to tables. For example, in databases I create, all tables have columns [InsertedBy], InsertedDate], [ChangeBy], [ChangeDate]. These columns are rarely used by applications, but are for administrative and auditing purposes. You have different needs, but the solution is similar.

Creating a 'metadata' table seems overly cumbersome and frought with peril.

|||

Dan Parks,

I'd have to agree with Arnie that it sounds like the database solution for what you are wanting to do would be to add columns to the table, and allow them to have NULL entries for cases where you did not set a value (such as the COLOR of your DOG or PIG).

Although it would be possible to use the Binary column type (I don't remember the exact name for it, but it is what allows JPG, BMP, etc., data to be stored in a table -- almost always by storing a LINK to the place where the database actually stores the large binary data values -- almost never in the row of data in the table) and store therein a Structure such as you might use in C++, you would then have to write your own procedures to unpack that structure if you want to search it for certain values, or if you want to change certain values. That seems to be avoiding many of the benefits of having a database with your ability to add columns as needed, and to JOIN tables on columns, and search for values in columns, etc.

Before I became involved with SQL and databases most of my programming had been in FORTRAN. Our data were stored in sorted fixed-record-length data files. We would have to perform binary searches to find the records we needed to perform a computation. We would have to create our own accumulator variables to sum quantities. If a coworker wanted to find all entries wherein a data column had a certain value, we would have to write a special program to search the entire file for that value, and display the records where it was found. If he wanted to search for multiple potential values in that column, we would have to write the program to allow for multiple entries. Now it is so much easier:

select *

from TABLE

where COLUMN1 in (value1, value2, value3)

order by 1,2,3,4,5

SQL has made much of my earlier programming rather trivial in terms of the SQL necessary to perform the same tasks. And because of the simpler SQL code (simpler to write, simpler to maintain) we were able easily to improve the computations to deal with nuances in the data that seemed awfully ugly in the FORTRAN solution. (Imagine a complex WHERE clause, relying on a number of subqueries of the same data -- easy in SQL, rather ugly in FORTRAN.)

Dan

Thursday, March 8, 2012

Assigning 1-row & multi-column query result to local variable

Hello,
I am just new to T-SQL programming on SQL server 2000. I would like to
ask you if it is possible (like in VBA for example) to save the result
of query (1 row, but multi columns) to some "array" or "object"
variable in order to reference the concrete components (fields) of
this variable in the future code.
For example in VBA:
Dim query1 as Recordset
Set query1 = CurrentDb.OpenRecordset("ABC")
MsgBox query1!ID
MsgBox query1!Comment
Or is the only way to save the results to the temp table and use other
queries to select the required fields? This means more rows of code
and slowing of the whole calculation process.
Thank you very much for your answer.
MilanMilan,
What are you trying to accomplish?
AMB
"Milan" wrote:

> Hello,
> I am just new to T-SQL programming on SQL server 2000. I would like to
> ask you if it is possible (like in VBA for example) to save the result
> of query (1 row, but multi columns) to some "array" or "object"
> variable in order to reference the concrete components (fields) of
> this variable in the future code.
> For example in VBA:
> Dim query1 as Recordset
> Set query1 = CurrentDb.OpenRecordset("ABC")
> MsgBox query1!ID
> MsgBox query1!Comment
>
> Or is the only way to save the results to the temp table and use other
> queries to select the required fields? This means more rows of code
> and slowing of the whole calculation process.
> Thank you very much for your answer.
> Milan
>|||No arrays in SQL but you could do something like this:
select @.var1 = col1, @.var2 = col2 ... from tableName where IDcol = ...
The query must return just 1 row though for this to work. If I remember
correctly, if it returns more then 1 row, then the variables will receive
the values of the last row.
Maybe if you described what you are trying to do, then someone could find a
better solution.
"Milan" <milan_vaclavik@.centrum.cz> wrote in message
news:b4cdce36.0503090705.1f5d824e@.posting.google.com...
> Hello,
> I am just new to T-SQL programming on SQL server 2000. I would like to
> ask you if it is possible (like in VBA for example) to save the result
> of query (1 row, but multi columns) to some "array" or "object"
> variable in order to reference the concrete components (fields) of
> this variable in the future code.
> For example in VBA:
> Dim query1 as Recordset
> Set query1 = CurrentDb.OpenRecordset("ABC")
> MsgBox query1!ID
> MsgBox query1!Comment
>
> Or is the only way to save the results to the temp table and use other
> queries to select the required fields? This means more rows of code
> and slowing of the whole calculation process.
> Thank you very much for your answer.
> Milan|||SQL is a declarative language not a procedural one like VB. Storing
values from rows to variables is something you should generally try to
avoid. Instead of retrieving values and then referencing them in future
code, aim to write declarative, set-based code that operates on the
whole set of data at once. Your SQL code will be much cleaner, more
efficient and more maintainable that way. Don't try to use TSQL like it
was VB.
It is in fact possible to assign column values to variables, using a
SET or SELECT statement but variable assignment should be the exception
rather than the rule. Frequent use of variable assignment from tables
implies that you'll be using cursor based processing - a common error
made by programmers new to SQL. If you have an actual problem (the code
you posted already doesn't do anything useful that can't be achieved
with a SELECT statement) then please come back with more information so
that we can suggest an alternative.
David Portas
SQL Server MVP
--|||I would like to thank you for your replies.
I have the table with columns named like "A_01", "A_02", ..., "A_30",
"B_01", "B_02", ..., "B_30", "C_01" etc. I know it is badly designed but
I inherited it from my colleague. I have created a complex (and slow)
query which returns 1 row from this table. What I have to do now
(separately for A, B, C...) is to insert some calculated values (based
on concrete values of 01, 02,..., 30) to some other tables. For example
if A_01 = 6, I have to input A_02/6 to the A_03th, (A_03+1)th, ...,
(A_03+5)th column of some concrete table. The calculation is really very
complex.
In VBA this is a trivial task but i can not manage it easily in T-SQL.
Thank you for your ideas!
Milan
*** Sent via Developersdex http://www.examnotes.net ***
Don't just participate in USENET...get rewarded for it!|||Hello Milan,
If it's easy for you to do in VBA, then why not do it in VBA? What requirem
ent
is there that you must do this in T-SQL?
Craig

> I would like to thank you for your replies.
> I have the table with columns named like "A_01", "A_02", ..., "A_30",
> "B_01", "B_02", ..., "B_30", "C_01" etc. I know it is badly designed
> but I inherited it from my colleague. I have created a complex (and
> slow) query which returns 1 row from this table. What I have to do now
> (separately for A, B, C...) is to insert some calculated values (based
> on concrete values of 01, 02,..., 30) to some other tables. For
> example if A_01 = 6, I have to input A_02/6 to the A_03th, (A_03+1)th,
> ..., (A_03+5)th column of some concrete table. The calculation is
> really very complex.
> In VBA this is a trivial task but i can not manage it easily in T-SQL.
> Thank you for your ideas!
> Milan
> *** Sent via Developersdex http://www.examnotes.net *** Don't just
> participate in USENET...get rewarded for it!
>|||I think you've realised that the root of your problem is the poor
design. I'm not sure why you would perpetuate this by creating another
table rather than do it in a view or query but anyway you may be able
to use something like this:
INSERT INTO Garbage_Out (a_01, a_o2, a_03)
SELECT I.a_01, NULL, I.a_02/I.a_01,
CASE I.a_01
WHEN 1 THEN I.a_0?
WHEN 2 THEN I.a_0?
..
END,
CASE I.a_01
WHEN 1 THEN I.a_0?
WHEN 2 THEN I.a_0?
..
END
FROM Garbage_In AS I
Fill in the question marks yourself - I wasn't clear from your
narrative which columns you would want to refer to. This "design" is
probably beyond redemption. Tables are not arrays.
David Portas
SQL Server MVP
--|||Hello Milan,

> In VBA this is a trivial task but i can not manage it easily in T-SQL.
Why not do it in VBA, then? What requirement is there that you do it in
T-SQL?
Craig

Sunday, February 12, 2012

ASP.NET connecting to MSDE

G'day,
I'm very new to ASP.NET (but not to programming) and I'm having some trouble with connecting to a database.

I am using C#.NET and I dragged the SqlConnection component onto the form, set the connection string to (local) and to use integrated security, and on that data link properties page I can press the test connection and it works fine, and I can see the databases in "select database on server" drop down box... but when I try and Open() the connection I get a

Exception Details: System.Data.SqlClient.SqlException: Login failed for user 'AMD2500\ASPNET'.

Can anyone help me?

Thanks,
RobboIf you want your web app to impersonate the user, you need to have the following two sections in web.config:

<configuration>
<system.web>
<identity impersonate="true"/>
<authentication mode="Windows" />
</system.web>
</configuration>