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

Sunday, November 30, 2008

Change Logical Filenames

Change logical filenames of MS SQL databse

USE MASTER
GO
ALTER DATABASE [DatabaseName]
MODIFY FILE
(NAME = [OldFileName_Data], NEWNAME='NewFileName_Data')
GO

ALTER DATABASE [DatabaseName]
MODIFY FILE
(NAME = OldFileName_Log, NEWNAME=''NewFileName_Log')
GO

Monday, August 4, 2008

ObjectDataSource could not find a non-generic method

I got this error"ObjectDataSource could not find a non-generic method" when trying to update or delete a record from GridView that uses ObjectDataSourse whose data source is configured to a tableadapter of a dataset.

I fixed this by specifying parameter "DataKeyNames" in a gridView control and changing OldValuesParameterFormatString="original_{0}" to OldValuesParameterFormatString="{0}" parameter of ObjectDataSource.