Saturday, November 27, 2010

List all the columns in the database which are used as identity key

-- List all columns which are used as identity key
-- sample1
SELECT SCHEMA_NAME(schema_id) AS schema_name,
t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
JOIN sys.identity_columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;



-- On/Off to disable identity key constrain
SET IDENTITY_INSERT [MyTableName] ON;
....
SET IDENTITY_INSERT [MyTableName] OFF;


source from: http://blog.sqlauthority.com/2008/03/29/sql-server-2005-list-all-column-with-indentity-key-in-specific-database/

Thursday, November 25, 2010

Query Data Type, Length and other information of table columns



--Query Data Type, Length and other information of table columns
SELECT
OBJECT_NAME(c.OBJECT_ID) TableName
,c.name AS ColumnName
,SCHEMA_NAME(t.schema_id) AS SchemaName
,t.name AS TypeName
,t.is_user_defined
,t.is_assembly_type
,c.max_length
,c.PRECISION
,c.scale
FROM sys.columns AS c
JOIN sys.types AS t ON c.user_type_id=t.user_type_id
WHERE OBJECT_NAME(c.OBJECT_ID) = 'myTableName'
ORDER BY c.OBJECT_ID;

-- Check Table Row Size and Sort by maximum size
-- SQL Server 2000 the Maximum Row Size limit is 8060 Bytes
SELECT OBJECT_NAME (sc.[id]) tablename
     , COUNT (1) nr_columns
     , SUM (sc.length) maxrowlength
  FROM syscolumns sc join sysobjects so on sc.[id] = so.[id]
 WHERE so.xtype = 'U'
 GROUP BY OBJECT_NAME (sc.[id])
 ORDER BY SUM (sc.length) desc





Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

Query to Display Foreign Key Relationships and Name of the Constraint for Each Table in Database

From: http://blog.sqlauthority.com/2006/11/01/sql-server-query-to-display-foreign-key-relationships-and-name-of-the-constraint-for-each-table-in-database/

-- Query foreign keys from a table

SELECT CONS.CONSTRAINT_CATALOG AS DB
     , CONS.CONSTRAINT_SCHEMA AS [SCHEMA]
     , CONS.TABLE_NAME AS [TABLE]
     , CONS.CONSTRAINT_TYPE AS [TYPE]
     , CONS.CONSTRAINT_NAME AS CONST_NAME
     , COLS.COLUMN_NAME AS [COLUMN]
  FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS CONS
       LEFT JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE COLS 
           ON CONS.TABLE_NAME=COLS.TABLE_NAME
           AND CONS.CONSTRAINT_SCHEMA=COLS.CONSTRAINT_SCHEMA
           AND CONS.CONSTRAINT_NAME=COLS.CONSTRAINT_NAME
 WHERE CONS.TABLE_NAME='MyTableName'


--This sample have some issue so that some tables is missing in the query result when they without a foreign key.
SELECT
CONSTRAINT_NAME = REF_CONST.CONSTRAINT_NAME,
TABLE_CATALOG = FK.TABLE_CATALOG,
TABLE_SCHEMA = FK.TABLE_SCHEMA,
TABLE_NAME = FK.TABLE_NAME,
COLUMN_NAME = FK_COLS.COLUMN_NAME,
REFERENCED_TABLE_CATALOG = PK.TABLE_CATALOG,
REFERENCED_TABLE_SCHEMA = PK.TABLE_SCHEMA,
REFERENCED_TABLE_NAME = PK.TABLE_NAME,
REFERENCED_COLUMN_NAME = PK_COLS.COLUMN_NAME
FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS REF_CONST
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON REF_CONST.CONSTRAINT_CATALOG = FK.CONSTRAINT_CATALOG
AND REF_CONST.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA
AND REF_CONST.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
AND FK.CONSTRAINT_TYPE = 'FOREIGN KEY'
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON REF_CONST.UNIQUE_CONSTRAINT_CATALOG = PK.CONSTRAINT_CATALOG
AND REF_CONST.UNIQUE_CONSTRAINT_SCHEMA = PK.CONSTRAINT_SCHEMA
AND REF_CONST.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
AND PK.CONSTRAINT_TYPE = 'PRIMARY KEY'
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE FK_COLS ON REF_CONST.CONSTRAINT_NAME = FK_COLS.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE PK_COLS ON PK.CONSTRAINT_NAME = PK_COLS.CONSTRAINT_NAME
---- optional:
ORDER BY
1,2,3,4
WHERE PK.TABLE_NAME='something' WHERE FK.TABLE_NAME='something'
WHERE PK.TABLE_NAME IN ('one_thing', 'another')
WHERE FK.TABLE_NAME IN ('one_thing', 'another')


--Sample 2  this sample will list all tables with constrain keys name.
SELECT OBJECT_NAME(parent_obj) table_name, name as key_name
FROM sysobjects WHERE xtype in('F', 'PK')



Thursday, November 18, 2010

XML DOM

XMLHttpRequest
http://www.w3.org/TR/XMLHttpRequest/

// Sample about  XMLHttpRequest
jQuery.ajax(...,
    success: function(data, textStatus, XMLHttpRequest)...

XMLDocument
http://www.w3schools.com/Xml/xml_parser.asp 


Convert iframe content to XMLDocument
// for IE
var xDoc = iframe1.contentWindow.document.XMLDocument;
// for Firefox
var parser = new DOMParser();
var xmlString = (new XMLSerializer()).serializeToString(iframe1.contentDocument.documentElement);
var xDoc = parser.parseFromString(xmlString, "text/xml");

Thursday, November 4, 2010

Manipulate XML string from iFrame

// get iframe
var iframe = document.getElementById("myIframeId");
var xDoc = null;
// get XmlDocument
// for IE
xDoc = iframe.contentWindow.document.XMLDocument;
// for firefox, chrome, safari,
xDoc = iframe.contentDocument.documentElement;

// Manipulation XmlDocument or XML element node
xDoc.getElementById("elmId");
xDoc.getElementsByTagName("div")[0];
xDoc.getAttribute("name");
xDoc.innerHtml;
xDoc.getElementById("inputTxtId").value;

// for IE
xDoc.innerText;


// for firefox, chrome...
xDoc.textContent;