Jun
24
2010

I posted some useful scripts to count your database’s Tables, Views and Stored Procedures.
Count Tables
SELECT COUNT(*) AS 'Tables Count' FROM sys.objects WHERE TYPE ='U' AND name NOT LIKE 'Sy%'
SELECT name AS 'Tables Name' FROM sys.objects WHERE TYPE ='U' AND name NOT LIKE 'Sy%'
Count Views
SELECT COUNT(*) AS 'Views Count' FROM sys.objects WHERE TYPE ='V' AND name NOT LIKE 'Sy%'
SELECT name AS 'View Name' FROM sys.objects WHERE TYPE ='V' AND name NOT LIKE 'Sy%'
Count Stored Procedures
SELECT COUNT(*) AS 'Stored Procs Count' FROM sys.objects WHERE TYPE ='P' AND name NOT LIKE 'Sy%'
SELECT name AS 'Stored Procs Name' FROM sys.objects WHERE TYPE ='P' AND name NOT LIKE 'Sy%'
no comments | posted in SQL
Dec
2
2009
Problem
Adding constraints such as check constraints or foreign keys to a table are best practices to keep your data as clean as possible with minimal data enforcement rules performed at the database level. Unfortunately sometimes issues may occur where the data becomes out of synch and one of these constraints has been violated. This may be due to disabled constraints or constraints that are later added with the NOCHECK option. Finding these issues can be done by running queries to check each of the constraints, but is there any easier way to determine if the data the constraints support has been violated?
Solution
As mentioned already, one approach would be to write queries for each of the constraints and check the data to see if the constraints are being enforced. This is probably not all that difficult, but it could be time consuming. Another approach to tackle this issue is to use the DBCC CHECKCONSTRAINTS command. This command allows you to check the constraints to ensure that no data is violating the constraints that have been setup.
This command can be run as follows:
DBCC CHECKCONSTRAINTS (TableName) - checks an individual table
DBCC CHECKCONSTRAINTS (ConstraintName) - checks an individual constraint
DBCC CHECKCONSTRAINTS WITH ALL_CONSTRAINTS - checks all constraints in the database
DBCC CHECKCONSTRAINTS WITH ALL_ERRORMSGS - returns all rows that violate constraints
DBCC CHECKCONSTRAINTS WITH NO_INFOMSGS - suppress messages when query runs
no comments | posted in SQL, Tips
Nov
23
2009
This is a SQL Script that Cleans your Database Records & resets Identity Columns, and it is all in 6 lines!
/*Disable Constraints & Triggers*/
exec sp_MSforeachtable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? DISABLE TRIGGER ALL'
/*Perform delete operation on all table for cleanup*/
exec sp_MSforeachtable 'DELETE ?'
/*Enable Constraints & Triggers again*/
exec sp_MSforeachtable 'ALTER TABLE ? CHECK CONSTRAINT ALL'
exec sp_MSforeachtable 'ALTER TABLE ? ENABLE TRIGGER ALL'
/*Reset Identity on tables with identity column*/
exec sp_MSforeachtable 'IF OBJECTPROPERTY(OBJECT_ID(''?''), ''TableHasIdentity'') = 1 BEGIN DBCC CHECKIDENT (''?'',RESEED,0) END'
If you dont want to miss any saved Diagrams… Continue reading
no comments | posted in SQL, Tips
Sep
9
2009

1. Save as excel document in CSV format. (found as CSV Comma Delimited file)
2. Connect to Mysql through command line
Start Cmd
cd c:\mysql\bin\ (or wherever the mysql directory is installed…)
3. Connect to MySQL command console
use
mysql -u root
to connect as root user to be able to do changes to your db
4. Remember to Save the CSV File in the mysql\bin directory
5. Enter the following commands
SELECT THE DATABASE TO USE
use yourdbname;
ENTER THE INSERT COMMAND
LOAD DATA LOCAL INFILE '/yourCSVfilename.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
For Greek or non-unicode Remember 3 Things
1. mysql> set character_set_database =utf8;
2. CSV File saved as UTF8 encoding
3. database Table character set to greek_unicode_ci;
no comments | posted in Programming, SQL
Jul
6
2009
This is a SQL Script that returns a list of all constrains in a database..
SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id, fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id, fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id
no comments | posted in SQL, Tips
Dec
4
2008
When you try to restore a database there are some very tricky stuff you need to know.
Continue reading
2 comments | posted in SQL, Windows
Dec
2
2008
A list of widely used SQL settings Explained
- ANSI NULLS
- QUOTED_IDENTIFIER
- ARITHMABORT
- ANSI_DEFAULTS
- ANSI_WARNINGS
- DATEFIRST
- DATEFORMAT
- NOCOUNT
- NOEXEC
- IDENTITY_INSERT
- IMPLICITY_TRANSACTIONS
- LANGUAGE
ANSI NULLS
- Syntax
SET ANSI_NULLS {ON | OFF}
- Explanation
The SQL-92 standard requires that an equals (=) or not equal to (<>) comparison against a null value evaluates to FALSE. When SET ANSI_NULLS is ON, a SELECT statement using WHERE column_name = NULL returns zero rows even if there are null values in column_name. A SELECT statement using WHERE column_name <> NULL returns zero rows even if there are nonnull values in column_name.
When SET ANSI_NULLS is OFF, the Equals (=) and Not Equal To (<>) comparison operators do not follow the SQL-92 standard. A SELECT statement using WHERE column_name = NULL returns the rows with null values in column_name. A SELECT statement using WHERE column_name <> NULL returns the rows with nonnull values in the column.
Continue reading
no comments | posted in SQL, Tutorials
Nov
26
2008
no comments | posted in SQL, Tips
Nov
1
2008

If you work with SQL Server on a regular basis, either writing custom queries, designing databases, or optimizing queries, this is a must see video as it covers a number of the enhancements to SQL Server 2008 including:
- SQL Management Studio improvements including IntelliSense
- New Data Types for just dates or times (no more storing time when you only need the date)
- New Hierarchical data support .IsDescendent(), that can automatically pull a hierarchical view of data (no more custom recursive queries)
- New Grouping Sets statement which enables you to automatically group dimensions in a query for easy aggregation and reporting
- New Merge statement which provides automatic insert/update semantics for keeping multiple data sources in sync
- New FileStream attribute that enables you to include files that are stored in the server file system, but can be managed by SQL Server
- New data types and support for Geodata and geometry
- New support for optimizing “empty” or row/column tables using the sparse keyword
no comments | posted in SQL
Nov
1
2008

Lets consider a simple table called employee( emp_id, name, salary). Now, suppose that we need to create a gridview with paging. One option is to bring all the data from database and do the paging in client-side and another one is to do selective fetch. In both cases we can use a stored procedure that takes some parameter and returns a resultset.
Let’s see the following…
CREATE PROCEDURE GetEmployees
@Status int,
@StartIndex int,
@PageSize int
AS
WITH FilteredList( [emp_id],[name], [salary], [RowNumber])
AS
(
SELECT
[emp_id],
[name],
[salary],
ROW_NUMBER() OVER ( ORDER BY [ID] DESC) AS [RowNumber]
FROM
Employee
)
SELECT
*
FROM
FilteredList
WHERE
RowNumber BETWEEN (@StartIndex + 1) AND (@StartIndex + @PageSize)
no comments | posted in SQL