Showing posts with label located. Show all posts
Showing posts with label located. Show all posts

Monday, March 19, 2012

composing a reference from fields located in mutiple tables

Consider a situation. There is a table of submitted 'documents'. They have some attributes. There are assignments to process the things, which have a date they were created. Finally there is a price list which specifies the price according to document features and date, so that the assignment to process a document created at different time will have a different cost. In other words, there is a relation
(assignment->document.attribute(s) + assignment.date) -> pricelist.price

Creating relations has the integrity advantages: it is not possible to create an assignment, which price is not defined in the pricelist; precludes the pricelist entry removal if it is referred by any assignments.

Should a view, which combines all the foreign fields into one virtual table, be created to make establishing the reference possible?

Not quite sure I understand the entire request. However, it seems you should be able to enforce referential integrity via Instead Of trigger.

Perhaps, you could give us some sample DDL and desired output to better describe your issue. We might be able to help further then.

|||Normally, you have all the tables interrelated. The reference (a foreign key) points to an object in another table specifying the "container" it belongs to. For instance, many books refer a single author.

Sometimes, you need to establish a complex reference consisting of multiple fields. For instance, a job refers to pricelist. The options in the print job specify a "service id", which has a unique price in the pricelist.

Suppose now that the pricelist can be updated. When job is created, it fixes the latest service cost in int field, the pricelist date. So the cost is uniquely identified by the job options (some fields) and the date. This is a complex key.

What I have faced is that nobody addresses the possibility of having the job attributes fixed in a separate table (say, documents to be processed always have the same settings). A job refers a document, from which the attributes are derived and coupled with the pricelist date identifies the job cost in the pricelist table. Effectively, the complex key is composed from fields located in different tables. A record contains only a part of complex key plus a reference to another entity, which has a rest of the key.

One way to create a relation would be to produce a view joining the key field tables. However, views are not enabled in diagrams. I suppose the reason is because the views are not allowed to participate in data relations.

Actually, I have decided that in my case I do not need to fix the job settings in the referred document, so I'll have all the key fields in one table. Yet, the topic is quite general to be interesting for me and others.

Sunday, March 11, 2012

Complicated Inserts

Hello everyone, im looking for some advice or maybe examples.

How is the best method of checking a table for a specific IP address located as an element value of a xml data type.

For example

xmlNode = Table in use

myXmlData = Document being stored as XML datatype

I would like to first check the table of existing XML entrys for an ip address that is within the current xmldata in the reader.

If the ip address in the reader matches any of the ones in the database it will update that row.

If the ip address can not be found in any existing row, a new row is created and myXmlData is stored.

At the moment data is stored regardless of existing entrys.

myQuery = "INSERT INTO T4(xmlNode) VALUES(@.myXMLData)"

If only things were simple eh !

INSERT INTO T4(xmlNode) VALUES(@.myXMLData) WHERE xmlNode.Childvalue = (?)

I know the above is totally wrong and syntaticlly rubbish but its along that idea.

Excause my SQL ignorance but im a bit of a newbie.

Examples are few to none, and few do not do anything like what i need.
Is it even possible ?

Regards

Russ

Since you want the logic "insert if the IP does not exist", your statement will go as follows:

IF EXISTS (SELECT * FROM T4 WHERE xmlNode.exist ('//ip[. = sql:variable(@.searchIPValue)]') = 1)
UPDATE T4 SET xmlNode = @.myXmlData
WHERE xmlNode.exist ('//ip[. = sql:variable(@.searchIPValue)]') = 1
ELSE
INSERT INTO T4 (xmlNode) VALUES (@.myXmlData)

Note that table name = T4 and XML column name = xmlNode (unlike what you have mentioned above).

Since the lookup on IP seems to be common in your workload and only a single IP occurs in each XML instance, you may want to promote it into a column to simplify the statment above. See the MSDN whitepaper for http://msdn.microsoft.com/xml/default.aspx?pull=/library/en-us/dnsql90/html/sql25xmlbp.asp for more information