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