Site Wide Message: (current site time 9/6/2010 12:48:52 PM EDT)
  • We want your input! One of our sponsors wants to know your opinion about development related issues. Click here to tell us what you think.
  • Are you an emerging/young developer (aged 18-30)? If so, would you like the chance to affect future developer tools and products?
    If so, then click here to give your feedback.
 

Know metadata sql server Connociendo metadata de sql server

Print
Email
VB icon
Submitted on: 1/8/2010 8:53:17 AM
By: Alejo Carrilllo  
Level: Advanced
User Rating: By 1 Users
Compatibility:SQL Server 2000, SQL Server 7.0, SQL Server 6.5 and earlier, Other

Users have accessed this code  2698 times.
 
(About the author)
 
     Here there is a miscellaneus of querys that let know the structure of the objects and relationships of any BD in SQL Server These querys can be used in another BD that use the INFORMATION_SCHEMA. Aqui se encuentra una miscelania de consultas que permiten conocer la estructura de cualquier objeto y sus relaciones en SQL Server. Estas consultas pueden ser aplicadas a cualquier motor de BD que maneje la BD INFORMATION_SCHEMA VOTE FOR ME!!!! VOTEN POR MI!!!
 
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
--**************************************
-- for :Know metadata sql server Connociendo metadata de sql server
--**************************************
Una pagina de internet que no recuerdo el nombre. Lo siento
A page of internet that I dont remember the name. Im sorry
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: Know metadata sql server Connociendo metadata de sql server
-- Description:Here there is a miscellaneus of querys that let know the structure of the objects and relationships of any BD in SQL Server
These querys can be used in another BD that use the INFORMATION_SCHEMA.
Aqui se encuentra una miscelania de consultas que permiten conocer la estructura de cualquier objeto y sus relaciones en SQL Server. 
Estas consultas pueden ser aplicadas a cualquier motor de BD que maneje la BD INFORMATION_SCHEMA
VOTE FOR ME!!!!
VOTEN POR MI!!!
-- By: Alejo Carrilllo
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=1341&lngWId=5--for details.--**************************************

USE AdventureWorks 
GO
-- TABLAS QUE CONTIENEN UNA COLUMNA
SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
WHERE COLUMN_NAME = 'CUSTOMERID'
-- COLUMNAS DE UNA TABLA
SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
WHERE TABLE_NAME = 'CUSTOMER'
-- LISTADO DE TABLAS
SELECT * FROM INFORMATION_SCHEMA.TABLES 
-- CAMPOS DE LA PRIMARY KEY EN UNA TABLA DADA
SELECT 
TC.TABLE_NAME, CU.COLUMN_NAME 
FROM 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
INNER JOIN 
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON TC.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 
AND TC.TABLE_NAME = 'CUSTOMER' -- TABLA 
ORDER BY TC.TABLE_NAME
-- RELACION DE TABLA (DEPENDENCIAS)
SELECT 
FK_Table = FK.TABLE_NAME, 
FK_Column = CU.COLUMN_NAME, 
PK_Table = PK.TABLE_NAME, 
PK_Column = PT.COLUMN_NAME, 
ConstraintName = C.CONSTRAINT_NAME 
FROM 
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
INNER JOIN 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
INNER JOIN 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
INNER JOIN 
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
INNER JOIN 
( 
 SELECT 
TC.TABLE_NAME, CU.COLUMN_NAME 
 FROM 
 INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
 INNER JOIN 
 INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
 ON TC.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
 WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 
) PT 
ON PT.TABLE_NAME = PK.TABLE_NAME 
WHERE 
PK.TABLE_NAME = 'SALESORDERHEADER' -- TU TABLA
-- RELACION DE TABLA (PADRES)
SELECT 
FK_Table = FK.TABLE_NAME, 
FK_Column = CU.COLUMN_NAME, 
PK_Table = PK.TABLE_NAME, 
PK_Column = PT.COLUMN_NAME, 
ConstraintName = C.CONSTRAINT_NAME 
FROM 
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C 
INNER JOIN 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK 
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME 
INNER JOIN 
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK 
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME 
INNER JOIN 
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU 
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
INNER JOIN 
( 
 SELECT 
TC.TABLE_NAME, CU.COLUMN_NAME 
 FROM 
 INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC 
 INNER JOIN 
 INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
 ON TC.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
 WHERE TC.CONSTRAINT_TYPE = 'PRIMARY KEY' 
) PT 
ON PT.TABLE_NAME = PK.TABLE_NAME 
WHERE 
FK.TABLE_NAME = 'clientesp' -- TU TABLA
-- CHECK DE UNA TABLA DADA
SELECT TC.TABLE_SCHEMA,TC.TABLE_NAME,CU.COLUMN_NAME,
CK.CHECK_CLAUSE 
 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CU
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
ON TC.TABLE_NAME = CU.TABLE_NAME 
AND
TC.CONSTRAINT_NAME = CU.CONSTRAINT_NAME 
INNER JOIN
INFORMATION_SCHEMA.CHECK_CONSTRAINTS CK
ON
CK.CONSTRAINT_NAME = TC.CONSTRAINT_NAME 
WHERE CONSTRAINT_TYPE = 'CHECK'
AND
TC.TABLE_NAME = 'CUSTOMER' -- TU TABLA
-- PARAMETROS DE UNA FUNCION O STORE
SELECT *
FROM INFORMATION_SCHEMA.PARAMETERS 
WHERE SPECIFIC_NAME ='ufnGetProductDealerPrice'


Other 2 submission(s) by this author

 

 
 Report Bad Submission
Use this form to notify us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:
 
Your Vote!

What do you think of this code(in the Advanced category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor See Voting Log
 
Other User Comments

 There are no comments on this submission.
 
Add Your Feedback!

Note:Not only will your feedback be posted, but an email will be sent to the code's author from the email account you registered on the site, so you can correspond directly.

NOTICE: The author of this code has been kind enough to share it with you.  If you have a criticism, please state it politely or it will be deleted.

For feedback not related to this particular code, please click here.
 
To post feedback, first please login.