Tuesday, December 15, 2009

Row_Number

In older SQL versions , to create row numbers we had to create a temprari tble with identity column

SELECT ROWID=IDENTITY(int,1,1) , OrderNo
INTO Order2 FROM Orders ORDER BY OrderDate


SQL 2005 and later support Row_Number that returns a squential number of a row in specifed order.

SELECT OrderNo, ROW_NUMBER() OVER(ORDER BY OrderDate DESC) AS 'RowNumber'
FROM Orders

Partition clause takes us one step further and numbers Rows in squential order for each patition.

SELECT OrderNo, State, ROW_NUMBER() OVER(Partition by State ORDER BY OrderDate DESC) AS 'RowNumber'
FROM Orders

1001, IL, 1
1005, IL, 2
1007, IL, 3
1002, IN, 1
1003, IN, 2

Sunday, November 8, 2009

Modify productName, manufacturer in VS 2008 deployment project

Productname and manufacturer for deployment project can be modified through properties from setup project but if you right click the setup project and select Properties it pops up Property Pages that have only Build configuration properties. and no option to change product name or manufacturer.

A workaround for this is after selecting the Setup Project to open Property Window from Main menu select View->"Property Window" and it shows different set of properties to edit that contain product name and manufacturer.

Monday, September 21, 2009

IP Subnet Calculator

Very useful tool while configuring a subnet.

http://www.subnet-calculator.com/subnet.php

Thursday, August 27, 2009

T-SQL to Rename a Column Name or Table Name

renaming any column :
sp_RENAME 'TableName.[OldColumnName]' , '[NewColumnName]', 'COLUMN'

renaming any object (table, stored procedure etc) :
sp_RENAME '[OldTableName]' , '[NewTableName]'

Wednesday, July 8, 2009

HTTP Response Status Code Group

1xx: Informational. The request was received and server is continuing to process.
2xx: Success. The action was successfully received, understood and accepted.
3xx: Redirect Command. The client must access a different resource.
4xx: Client Error. The server does not know how to fulfill the request. The request may have syntax error.
5xx: Server Error. The server failed to fulfill the request that appears to be valid.

Thursday, May 28, 2009

Use sp_refreshview to refresh view

When a table schema is updated the view does not update on its own.

sp_refreshview updates metadata of the specified view

To update a specifiec view

EXECUTE sp_refreshview '[VIEWNAME]'

To update all views that are dependent on a specified object

SELECT DISTINCT 'EXEC sp_refreshview ''' + name + ''''
FROM sys.objects AS so
INNER JOIN sys.sql_expression_dependencies AS sed
ON so.object_id = sed.referencing_id
WHERE so.type = 'V' AND sed.referenced_id = OBJECT_ID('[OBJECTNAME]');

Tuesday, May 12, 2009

Get List of All Triggers in a Database

SELECT trigger_name = name, trigger_owner = USER_NAME(uid), table_name = OBJECT_NAME(parent_obj),
isupdate = OBJECTPROPERTY( id, 'ExecIsUpdateTrigger'), isdelete = OBJECTPROPERTY( id, 'ExecIsDeleteTrigger'),
isinsert = OBJECTPROPERTY( id, 'ExecIsInsertTrigger'), isafter = OBJECTPROPERTY( id, 'ExecIsAfterTrigger'),
isinsteadof = OBJECTPROPERTY( id, 'ExecIsInsteadOfTrigger'),
status = CASE OBJECTPROPERTY(id, 'ExecIsTriggerDisabled') WHEN 1 THEN 'Disabled' ELSE 'Enabled' END
FROM sysobjects
WHERE type = 'TR'

Wednesday, April 29, 2009

Using Apply: CROSS APPLY and OUTER APPLY

The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query.

http://msdn.microsoft.com/en-us/library/ms175156.aspx

CRL Table Valued Functions

http://msdn.microsoft.com/en-us/library/ms131103.aspx

Thursday, April 23, 2009

Save (Not Permitted) Dialog Box in MS SQL 2008

The Save (Not Permitted) dialog box warns you that saving changes is not permitted because the changes you have made require the listed tables to be dropped and re-created.

The following actions might require a table to be re-created:

Adding a new column to the middle of the table

Dropping a column

Changing column nullability

Changing the order of the columns

Changing the data type of a column

To change this option, on the Tools menu, click Options, expand Designers, and then click Table and Database Designers. Select or clear the Prevent saving changes that require the table to be re-created check box.


http://msdn.microsoft.com/en-us/library/bb895146.aspx

Friday, April 17, 2009

Add leading zeros to a number in a MS SQL query

To add leading '0's to a column when length is less than 5


SELECT CASE WHEN LEN(rtrim(COLUMN)) < 5 THEN REPLICATE('0', 5 - LEN(rtrim(COLUMN))) + rtrim(COLUMN) ELSE rtrim(COLUMN) END

OR

SELECT RIGHT('00000' + RTRIM(COLUMN), 5)

OR

SELECT RIGHT(REPLICATE('0',5) + CONVERT(varchar( 6 ) , 1.2) ,5)

Saturday, February 28, 2009

Get all dependent objects on a specified object in SQL

Following query lists all dependent object names and their type that are dependent on object [OBJECTNAME]

SELECT DISTINCT name, so.type
FROM sys.objects AS so
INNER JOIN sys.sql_expression_dependencies AS sed
ON so.object_id = sed.referencing_id
WHERE sed.referenced_id = OBJECT_ID('[OBJECTNAME]');


Where type can be one of the object types below:

C = CHECK constraint
D = Default or DEFAULT constraint
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
IF = Inlined table-function
P = Stored procedure
PK = PRIMARY KEY constraint (type is K)
RF = Replication filter stored procedure
S = System table
TF = Table function
TR = Trigger
U = User table
UQ = UNIQUE constraint (type is K)
V = View
X = Extended stored procedure