Thursday, February 01, 2007

SQL SERVER: How can we find whether a table is existing in our database or not?

Use pubs
select * from information_schema.tables -- to get details about all tables.
select * from information_schema.columns -- to get columns of all tables.

Most of the DBA level info can be extracted from information_schema views.
Some more format:-
View Name Description
CHECK_CONSTRAINTS Holds information about constraints in the database
COLUMN_DOMAIN_USAGE Identifies which columns in which tables are user-defined datatypes
COLUMN_PRIVILEGES Has one row for each column level permission granted to or by the current user
COLUMNS Lists one row for each column in each table or view in the database
CONSTRAINT_COLUMN_USAGE Lists one row for each column that has a constraint defined on it
CONSTRAINT_TABLE_USAGE Lists one row for each table that has a constraint defined on it
DOMAIN_CONSTRAINTS Lists the user-defined datatypes that have rules bound to them
DOMAINS Lists the user-defined datatypes
KEY_COLUMN_USAGE Lists one row for each column that's defined as a key
PARAMETERS Lists one row for each parameter in a stored procedure or user-defined function
REFERENTIAL_CONSTRAINTS Lists one row for each foreign constraint
ROUTINES Lists one row for each stored procedure or user-defined function
ROUTINE_COLUMNS Contains one row for each column returned by any table-valued functions
SCHEMATA Contains one row for each database
TABLE_CONSTRAINTS Lists one row for each constraint defined in the current database
TABLE_PRIVILEGES Has one row for each table level permission granted to or by the current user
TABLES Lists one row for each table or view in the current database
VIEW_COLUMN_USAGE Lists one row for each column in a view including the base table of the column where possible
VIEW_TABLE_USAGE Lists one row for each table used in a view
VIEWS Lists one row for each view




---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com

Labels:

Wednesday, January 31, 2007

SQL SERVER:-WHAT IS THE DIFFERENCE BETWEEN ISQL AND OSQL

Both are command prompt utilities but as per HELP:-

The isql utility allows you to enter Transact-SQL statements, system procedures, and script files; and uses DB-Library to communicate with Microsoft® SQL Server™ 2000.

And

The osql utility allows you to enter Transact-SQL statements, system procedures, and script files. This utility uses ODBC to communicate with the server.

Use OSQL whenever possible as it supports UNICODE which ISQL doesnt.
---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com

Labels:

SQL SERVER:HOW TO RUN A SQL SCRIPT WHICH IS IN YOUR LOCAL DRIVE?

a/
Create a file in C:\ drive Hasim.sql is as follows:-

SELECT t1.empname [Employee], COALESCE(t2.empname, 'No manager') [Manager] FROM emp t1 LEFT OUTER JOIN emp t2 ON t1.mgrid = t2.empid;

b/
Create a table emp in tempdb


use tempdb;

CREATE TABLE emp
(
empid int,
mgrid int,
empname char(10)
);

c/
Populate the table


INSERT emp SELECT 1,2,'Hasim'
INSERT emp SELECT 2,3,'Arun'
INSERT emp SELECT 3,NULL,'Divya'
INSERT emp SELECT 4,2,'Parthiban'
INSERT emp SELECT 5,2,'Priyanka'

d/
Run that sql file in C:\ drive as follows:-


EXEC master..xp_cmdshell 'isql -SAHASIM -Usa -Psa -ic:\Hasim.sql -n'

---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com

Labels:

SQL SERVER: Maximum number of columns per table

Ans:-
========

1,024

Interested to find out maximum number of columns possible in a select statement et all?
Check the MSDN page:-
Maximum Capacity Specifications

---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com

Labels:

SQL SERVER: What is CHECKSUM

FROM HELP FILE

CHECKSUM computes a hash value, called the checksum, over its list of arguments. The hash value is intended for use in building hash indices. If the arguments to CHECKSUM are columns, and an index is built over the computed CHECKSUM value, the result is a hash index, which can be used for equality searches over the columns.

We can easily detect if there is a changes in any rows in any places ( in any particular fields)

-----------Example---------

use pubs

SELECT au_id , au_lname ,
CHECKSUM ( au_id , au_lname ) AS chk
FROM authors

--409-56-7008 Bennet 271639220

update authors set au_lname = 'Bennet' where au_id = '409-56-7008'

SELECT au_id , au_lname ,
CHECKSUM ( au_id , au_lname ) AS chk
FROM authors

--409-56-7008 Bennet 271639220( No changes in checksum )

update authors set au_lname = 'Bennnet' where au_id = '409-56-7008'
SELECT au_id , au_lname ,
CHECKSUM ( au_id , au_lname ) AS chk
FROM authors
--409-56-7008 Bennnet -1628839244( Checksum changed )

update authors set au_lname = 'Bennet' where au_id = '409-56-7008'
SELECT au_id , au_lname ,
CHECKSUM ( au_id , au_lname ) AS chk
FROM authors
--409-56-7008 Bennet 271639220 ( Again the check sum is the same )


---: I am not responsible for any damages happened from the suggestion of my blog :---
Reach me at : m.a.hasim@inbox.com

Labels: