Author |
Topic |
urzsuresh
Starting Member
30 Posts |
Posted - 2010-09-17 : 06:40:14
|
Hi,For Eg. Table Name: Location Fileds: LocationID,LocationName,..etc Primary Key is LocationID. In this, if user try to delete the location master records, here we need to find referrrence value in all table. In case referrence table(LocatoinID) contains arount 40 Tables. here i need to find each and every table, whether LocationId exists or not. Is there any short cut procedures or funtions to return referenced table (i.e LocationID value refferd in particular table).Some tables contains:Single PrimaryKey or Composite Primary Key Can any please guide me through sample codeSuri |
|
Sachin.Nand
2937 Posts |
Posted - 2010-09-17 : 07:04:10
|
You want to do that using a SQL query?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH |
 |
|
urzsuresh
Starting Member
30 Posts |
Posted - 2010-09-17 : 07:13:09
|
Thats what only i need. can you please give me sample code for this. how can i achieve thisquote: Originally posted by Idera You want to do that using a SQL query?Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless. PBUH
Suri |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-09-17 : 08:55:37
|
this ought to point you in the right directionSELECT object_name(fk.parent_object_id) parentTable, pc.name parentColumn, object_name(fk.referenced_object_id) referencedTable, rc.name referencedColumnfrom sys.foreign_keys fkjoin sys.foreign_key_columns fcon fc.constraint_object_id = fk.object_idjoin sys.columns pcon pc.object_id = fk.parent_object_idand pc.column_id = fc.parent_column_idjoin sys.columns rcon rc.object_id = fk.referenced_object_idand rc.column_id = fc.referenced_column_id |
 |
|
urzsuresh
Starting Member
30 Posts |
Posted - 2010-09-17 : 09:07:41
|
Hi, My requirement isFor eg, LocationID contains Value 12 Here LocationId reffered in multiple table. so we need to search all table (i.e LocationID 12).If its exists, we need to return the referred table.How can i achieve thisquote: Originally posted by russell this ought to point you in the right directionSELECT object_name(fk.parent_object_id) parentTable, pc.name parentColumn, object_name(fk.referenced_object_id) referencedTable, rc.name referencedColumnfrom sys.foreign_keys fkjoin sys.foreign_key_columns fcon fc.constraint_object_id = fk.object_idjoin sys.columns pcon pc.object_id = fk.parent_object_idand pc.column_id = fc.parent_column_idjoin sys.columns rcon rc.object_id = fk.referenced_object_idand rc.column_id = fc.referenced_column_id
Suri |
 |
|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2010-09-17 : 09:09:51
|
You can set up Cascade delete foreign keys from this table to the other child tables. Then when a record is deleted from this table, the database will automatically delete all the other associated records. The tricky thing with this is if yor applications don't insert into the parent table first, then you will get a FK error. Likewise, if there are some orphan records, then when trying to create the key, you will get errors, so there mat be some cleanup involved before setting this up.For Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-09-17 : 09:26:32
|
Cascading deletes is always a bad idea. We don't allow it in any circumstances in our databases.It introduces difficult to track down bugs, causes inadvertant data loss.And it is the lazy way out. |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-09-17 : 09:30:23
|
urzsuresh, let's see what you've tried so far |
 |
|
MSquared
Yak Posting Veteran
52 Posts |
Posted - 2010-09-17 : 10:38:39
|
quote: Originally posted by russell Cascading deletes is always a bad idea. We don't allow it in any circumstances in our databases.It introduces difficult to track down bugs, causes inadvertant data loss.And it is the lazy way out.
Russell, I don't agree. Obviously you don't put it on tables where you don't want inadvertant data lost (although I'm not sure how good the data will be if the parent has been deleted). This will insure data integrity. In this case, 40 tables might be excessive for a FK on each, but what if a developer creates a proc and it deletes on 38 tables? How about if the original proc deletes all 40 tables, but then new child tables are created? I'm just trying to maintain the integrity of the database.For Faster results please follow the posting guidelines herehttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-09-17 : 11:03:05
|
the foreign key already enforces the integrity. allowing cascades doesn't enhanance that in any way.it does allow people to delete data that they had no idea they were deleting though. |
 |
|
urzsuresh
Starting Member
30 Posts |
Posted - 2010-09-17 : 11:59:19
|
Actually i have used following query to get list of refernce. Select cast(c.name as varchar(255)) as foreign_table , cast(fc.name as varchar(255)) as foreign_column_1 , cast(p.name as varchar(255)) as primary_table , cast(rc.name as varchar(255)) as primary_column_1 from sysobjects f inner join sysobjects c on f.parent_obj = c.id inner join sysreferences r on f.id = r.constid inner join sysobjects p on r.rkeyid = p.id inner join syscolumns rc on r.rkeyid = rc.id and r.rkey1 = rc.colid inner join syscolumns fc on r.fkeyid = fc.id and r.fkey1 = fc.colid left join syscolumns rc2 on r.rkeyid = rc2.id and r.rkey2 = rc.colid left join syscolumns fc2 on r.fkeyid = fc2.id and r.fkey2 = fc.colid where f.type = 'F' and p.name=@TableName ORDER BY cast(p.name as varchar(255)) I have movet the aboove result to some table variable and manipulate and return whethere LocationId value is referred or not. I can able to achieve, If its single primary key.I dont know how to proceed for composite primary key. In above query, i didnt get proper result for composite primary key. Can you please guide through sample code.Suri |
 |
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2010-09-17 : 12:19:30
|
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, data types, etc. in your schema are. Please learn to follow ISO-11179 data element naming conventions and formatting rules. Temporal data should use ISO-8601 formats. Code should be in Standard SQL as much as possible and not local dialect. Sample data is also a good idea, along with clear specifications. It is very hard to debug code when you do not let us see it. If you want to learn how to ask a question on a Newsgroup, look at: http://www.catb.org/~esr/faqs/smart-questions.html --CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|